master
Evan Chen 2021-12-19 01:37:44 +08:00
parent e3b864e29d
commit 074d1d5582
7 changed files with 119 additions and 9 deletions

71
attribute/attribute.go Normal file
View File

@ -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
}

19
attribute/errors.go Normal file
View File

@ -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",
}

15
attribute/service.go Normal file
View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}

View File

@ -7,7 +7,6 @@ import (
func TestMain(t *testing.T) {
sys := Default()
go sys.Start()
<-sys.Done()
}

View File

@ -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")))