diff --git a/api/setting.go b/api/setting.go index 5ce4344..6cbbc4c 100644 --- a/api/setting.go +++ b/api/setting.go @@ -1,10 +1,8 @@ package api import ( - "bytes" "encoding/json" - "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" ) @@ -18,7 +16,7 @@ type SettingHandler struct { // NewSetting creates an instance of SettingHandler func NewSetting(b biz.SettingBiz) *SettingHandler { return &SettingHandler{ - Load: settingLoad(b), + Load: settingLoad(b), Save: settingSave(b), } } @@ -42,15 +40,8 @@ func settingSave(b biz.SettingBiz) web.HandlerFunc { return func(ctx web.Context) (err error) { args := &Args{} err = ctx.Bind(args) - if err != nil { - return - } - - options := data.Map{} - d := json.NewDecoder(bytes.NewBuffer(args.Options)) - d.UseNumber() - if err = d.Decode(&options); err == nil { - err = b.Save(args.ID, options, ctx.User()) + if err == nil { + err = b.Save(args.ID, args.Options, ctx.User()) } return ajax(ctx, err) } diff --git a/biz/setting.go b/biz/setting.go index b544a3d..768e236 100644 --- a/biz/setting.go +++ b/biz/setting.go @@ -1,22 +1,21 @@ package biz import ( + "bytes" "context" "encoding/json" - "strconv" "time" "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/net/web" - "github.com/cuigh/auxo/util/cast" "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/model" ) type SettingBiz interface { - Find(id string) (options data.Map, err error) + Find(id string) (options interface{}, err error) Load() (options data.Map, err error) - Save(id string, options data.Map, user web.User) (err error) + Save(id string, options interface{}, user web.User) (err error) } func NewSetting(d dao.Interface, eb EventBiz) SettingBiz { @@ -28,17 +27,11 @@ type settingBiz struct { eb EventBiz } -func (b *settingBiz) Find(id string) (options data.Map, err error) { +func (b *settingBiz) Find(id string) (options interface{}, err error) { var setting *model.Setting setting, err = b.d.SettingGet(context.TODO(), id) - if err != nil { - return - } - - if setting != nil { - options = b.toMap(setting.Options) - } else { - options = make(data.Map) + if err == nil && setting != nil { + return b.unmarshal(setting.Options) } return } @@ -53,82 +46,45 @@ func (b *settingBiz) Load() (options data.Map, err error) { options = data.Map{} for _, s := range settings { - options[s.ID] = b.toMap(s.Options) + var v interface{} + if v, err = b.unmarshal(s.Options); err != nil { + return + } + options[s.ID] = v } return } -func (b *settingBiz) Save(id string, options data.Map, user web.User) (err error) { +func (b *settingBiz) Save(id string, options interface{}, user web.User) (err error) { setting := &model.Setting{ ID: id, - Options: b.toOptions(options), UpdatedAt: time.Now(), } if user != nil { setting.UpdatedBy = model.Operator{ID: user.ID(), Name: user.Name()} } - err = b.d.SettingUpdate(context.TODO(), setting) + + setting.Options, err = b.marshal(options) + if err == nil { + err = b.d.SettingUpdate(context.TODO(), setting) + } if err == nil && user != nil { b.eb.CreateSetting(EventActionUpdate, user) } return } -func (b *settingBiz) toOptions(m data.Map) []*model.SettingOption { - var opts []*model.SettingOption - for k, v := range m { - opt := &model.SettingOption{Name: k} - switch v.(type) { - case bool: - opt.Type = "bool" - opt.Value = strconv.FormatBool(v.(bool)) - case json.Number: - opt.Type = "number" - opt.Value = cast.ToString(v) - case string: - opt.Type = "string" - opt.Value = v.(string) - default: - opt.Type = "json" - opt.Value = b.toJSON(v) - } - opts = append(opts, opt) +func (b *settingBiz) marshal(v interface{}) (s string, err error) { + var buf []byte + if buf, err = json.Marshal(v); err == nil { + s = string(buf) } - return opts + return } -func (b *settingBiz) toMap(options []*model.SettingOption) data.Map { - m := data.Map{} - for _, opt := range options { - var v interface{} - switch opt.Type { - case "bool": - v = opt.Value == "true" - case "number": - v = cast.ToInt32(opt.Value) - case "string": - v = opt.Value - default: - v = b.fromJSON(opt.Value) - } - m[opt.Name] = v - } - return m -} - -func (b *settingBiz) toJSON(v interface{}) string { - d, err := json.Marshal(v) - if err != nil { - panic(err) - } - return string(d) -} - -func (b *settingBiz) fromJSON(v string) interface{} { - var i interface{} - err := json.Unmarshal([]byte(v), &i) - if err != nil { - panic(err) - } - return i +func (b *settingBiz) unmarshal(s string) (v interface{}, err error) { + d := json.NewDecoder(bytes.NewBuffer([]byte(s))) + d.UseNumber() + err = d.Decode(&v) + return } diff --git a/dao/dao.go b/dao/dao.go index 8d2c69c..0f107e1 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -79,7 +79,7 @@ func newInterface() (i Interface) { case "bolt": i, err = bolt.New(misc.Options.DBAddress) default: - err = errors.New("Unknown database type: " + misc.Options.DBType) + err = errors.New("unknown database type: " + misc.Options.DBType) } if err != nil { diff --git a/main.go b/main.go index 15449ff..81a2182 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "embed" + "encoding/json" "io/fs" "net/http" "strings" @@ -10,7 +11,7 @@ import ( "github.com/cuigh/auxo/app/container" "github.com/cuigh/auxo/app/flag" _ "github.com/cuigh/auxo/cache/memory" - "github.com/cuigh/auxo/config" + "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/data/valid" "github.com/cuigh/auxo/errors" "github.com/cuigh/auxo/log" @@ -102,13 +103,15 @@ func initSystem() error { func loadSetting(sb biz.SettingBiz) *misc.Setting { var ( - err error - s = &misc.Setting{} + err error + opts data.Map + b []byte + s = &misc.Setting{} ) - - config.AddSource(sb) - if err = config.Load(); err == nil { - err = config.Unmarshal(s) + if opts, err = sb.Load(); err == nil { + if b, err = json.Marshal(opts); err == nil { + err = json.Unmarshal(b, s) + } } if err != nil { log.Get("misc").Error("failed to load setting: ", err) diff --git a/misc/option.go b/misc/option.go index 65175c3..5275cef 100644 --- a/misc/option.go +++ b/misc/option.go @@ -49,28 +49,24 @@ func LoadOptions() (err error) { // Setting represents the settings of Swirl. type Setting struct { System struct { - Version string - } - Region struct { - Language string `option:"lang"` - Timezone int32 - } + Version string `json:"version"` + } `json:"system"` LDAP struct { - Enabled bool - Address string - Security int32 // 0, 1, 2 - Authentication string `option:"auth"` // simple, bind - BindDN string - BindPassword string `option:"bind_pwd"` // Bind DN password - BaseDN string // Base search path for users - UserDN string // Template for the DN of the user for simple auth - UserFilter string // Search filter for user - NameAttr string - EmailAttr string - } + Enabled bool `json:"enabled"` + Address string `json:"address"` + Security int32 `json:"security"` // 0, 1, 2 + Authentication string `json:"auth"` // simple, bind + BindDN string `json:"bind_dn"` + BindPassword string `json:"bind_pwd"` // Bind DN password + BaseDN string `json:"base_dn"` // Base search path for users + UserDN string `json:"user_dn"` // Template for the DN of the user for simple auth + UserFilter string `json:"user_filter"` // Search filter for user + NameAttr string `json:"name_attr"` + EmailAttr string `json:"email_attr"` + } `json:"ldap"` Metric struct { - Prometheus string - } + Prometheus string `json:"prometheus"` + } `json:"metric"` } func init() { diff --git a/model/model.go b/model/model.go index 982fdbe..58911a0 100644 --- a/model/model.go +++ b/model/model.go @@ -58,16 +58,10 @@ type Operator struct { // Setting represents the options of swirl. type Setting struct { - ID string `json:"id" bson:"_id"` - Options []*SettingOption `json:"options" bson:"options"` - UpdatedAt time.Time `json:"updatedAt" bson:"updated_at"` - UpdatedBy Operator `json:"updatedBy" bson:"updated_by"` -} - -type SettingOption struct { - Name string `json:"name" bson:"name"` - Value string `json:"value" bson:"value"` - Type string `json:"type" bson:"type"` + ID string `json:"id" bson:"_id"` + Options string `json:"options" bson:"options"` + UpdatedAt time.Time `json:"updatedAt" bson:"updated_at"` + UpdatedBy Operator `json:"updatedBy" bson:"updated_by"` } type Role struct { diff --git a/ui/src/api/setting.ts b/ui/src/api/setting.ts index 9b34931..09e6d75 100644 --- a/ui/src/api/setting.ts +++ b/ui/src/api/setting.ts @@ -1,14 +1,17 @@ import ajax, { Result } from './ajax' export interface Setting { - region: RegionSetting; ldap: LdapSetting; metric: MetricSetting; + deploy: DeployOptions; } -export interface RegionSetting { - lang: string; - timezone: number; +export interface DeployOptions { + keys: { + name: string; + token: string; + expiry: number; + }[]; } export interface LdapSetting { diff --git a/ui/src/pages/container/List.vue b/ui/src/pages/container/List.vue index 12935e5..c254529 100644 --- a/ui/src/pages/container/List.vue +++ b/ui/src/pages/container/List.vue @@ -1,5 +1,5 @@