From 074d1d55826fd3e16af71fe3c5419983ce4862d8 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sun, 19 Dec 2021 01:37:44 +0800 Subject: [PATCH] update --- attribute/attribute.go | 71 ++++++++++++++++++++++++++++++++++++++++++ attribute/errors.go | 19 +++++++++++ attribute/service.go | 15 +++++++++ auth/group.go | 2 +- helper.go | 2 ++ main_test.go | 1 - system/system.go | 18 ++++++----- 7 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 attribute/attribute.go create mode 100644 attribute/errors.go create mode 100644 attribute/service.go diff --git a/attribute/attribute.go b/attribute/attribute.go new file mode 100644 index 0000000..64cb925 --- /dev/null +++ b/attribute/attribute.go @@ -0,0 +1,71 @@ +package attribute + +import ( + "time" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "kumoly.io/kumoly/app/store" +) + +type Attribute struct { + ID uint `gorm:"primaryKey"` + CreatedAt time.Time + UpdatedAt time.Time + + Key string `gorm:"unique;not null"` + Description string + Default string + Value string +} + +var l zerolog.Logger + +func Init() error { + l = log.With().Str("mod", "attribute").Logger() + + l.Debug().Str("service", "attribute.Service"). + Msg("Migrating database for attribute.Service ...") + err := store.Migrate(&Attribute{}) + if err != nil { + return err + } + return nil +} + +func Add(Key, Description, Default, Value string) error { + a := &Attribute{ + Key: Key, + Description: Description, + Default: Default, + Value: Value, + } + return store.DB.Create(a).Error +} + +func RestoreDefault(key string) error { + result := store.DB.Exec(`update "attributes" set value = ( + select "default" from "attributes" where "key" = ? +) where "key" = ?`, key, key) + if result.RowsAffected == 0 { + return ErrorAttributeNotFound + } + return result.Error +} + +func Get(key string) string { + var value string + store.DB. + Raw(`select "value" from "attributes" where "key" = ?`, key). + Scan(&value) + return value +} + +func Set(key, value string) error { + result := store.DB.Exec(`update "attributes" set value = ? where "key" = ?`, + value, key) + if result.RowsAffected == 0 { + return ErrorAttributeNotFound + } + return result.Error +} diff --git a/attribute/errors.go b/attribute/errors.go new file mode 100644 index 0000000..78342ba --- /dev/null +++ b/attribute/errors.go @@ -0,0 +1,19 @@ +package attribute + +import ( + "net/http" + + "kumoly.io/kumoly/app/errors" +) + +var ErrorAttributeNotFound = errors.Error{ + Code: http.StatusNotFound, + ID: "ErrorTokenNotValid", + Message: "Attribute not found", +} + +var ErrorInitializationFail = errors.Error{ + Code: http.StatusNotFound, + ID: "ErrorInitializationFail", + Message: "Database not connected", +} diff --git a/attribute/service.go b/attribute/service.go new file mode 100644 index 0000000..2bc1809 --- /dev/null +++ b/attribute/service.go @@ -0,0 +1,15 @@ +package attribute + +type Service struct{} + +func (srv Service) GetName() string { return "attribute.Service" } +func (srv Service) IsService() bool { return true } +func (srv Service) GetDependencies() []string { return []string{} } +func (srv Service) Main() error { return nil } +func (srv Service) Del() {} +func (srv Service) Load() error { return nil } +func (srv Service) Health() error { return nil } + +func (srv Service) Init() error { + return Init() +} diff --git a/auth/group.go b/auth/group.go index 3d625ee..019cceb 100644 --- a/auth/group.go +++ b/auth/group.go @@ -24,7 +24,7 @@ func (grp *Group) BeforeSave(tx *gorm.DB) (err error) { // set displayname if grp.DisplayName == "" { - grp.DisplayName = strings.TrimPrefix(grp.Name, "*") + grp.DisplayName = strings.TrimPrefix(grp.Name, SYS_AUTH_PREFIX) } return } diff --git a/helper.go b/helper.go index a98b345..31e48e0 100644 --- a/helper.go +++ b/helper.go @@ -2,6 +2,7 @@ package app import ( "github.com/spf13/viper" + "kumoly.io/kumoly/app/attribute" "kumoly.io/kumoly/app/auth" "kumoly.io/kumoly/app/control" "kumoly.io/kumoly/app/email" @@ -26,6 +27,7 @@ func Default() *system.System { &history.Service{}, &email.Service{}, &control.Service{}, + &attribute.Service{}, ) return sys } diff --git a/main_test.go b/main_test.go index 16e4d53..17bcdb3 100644 --- a/main_test.go +++ b/main_test.go @@ -7,7 +7,6 @@ import ( func TestMain(t *testing.T) { sys := Default() go sys.Start() - <-sys.Done() } diff --git a/system/system.go b/system/system.go index 6156027..cdd58d4 100644 --- a/system/system.go +++ b/system/system.go @@ -49,6 +49,7 @@ type System struct { isService bool status state lock sync.Mutex + started chan struct{} term chan struct{} done chan struct{} notifyDone chan struct{} @@ -91,6 +92,7 @@ func (sys *System) order() { func New() *System { sys := &System{} sys.done = make(chan struct{}, 1) + sys.started = make(chan struct{}, 1) sys.term = make(chan struct{}, 1) sys.notifyDone = make(chan struct{}, 1) sys.quit = make(chan os.Signal, 1) @@ -117,20 +119,22 @@ func (sys *System) Start() { } l.Info().Msg("Starting...") - if e := l.Trace(); e.Enabled() { - config, err := ConfigShow("") - if err == nil { - e.Str("config", config).Msg("config") - } - } + // if e := l.Trace(); e.Enabled() { + // config, err := ConfigShow("") + // if err == nil { + // e.Str("config", config).Msg("config") + // } + // } sys.status = sys_main sys.main() sys.status = sys_wait + sys.started <- struct{}{} if !sys.isService { sys.quit <- syscall.SIGTERM } }() + <-sys.started history.Send(history.Info().Nm("SysUP").Msg("system started")) <-sys.quit isRestart := false @@ -138,8 +142,8 @@ func (sys *System) Start() { isRestart = true } - l.Info().Msg("Terminating...") history.Send(history.Info().Nm("SysDown").Msg("system stoped")) + l.Info().Msg("Terminating...") sys.status = sys_term go func() { time.Sleep(time.Second * time.Duration(viper.GetInt("system.terminate_timeout")))