master
Evan Chen 2022-01-09 16:31:12 +08:00
parent c1348da5bd
commit 838490e841
3 changed files with 38 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package attribute package attribute
import ( import (
"sync"
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -16,6 +17,7 @@ type Attribute struct {
Description string Description string
Value string Value string
Default string Default string
NeedRestart bool
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
@ -34,7 +36,7 @@ func Init() {
} }
} }
func Add(Key, Name, Description, Default string) error { func Add(Key, Name, Description, Default string, NeedRestart bool) error {
ctr := 0 ctr := 0
store.DB.Raw(`select count(*) from "attributes" where "key" = ?`, Key). store.DB.Raw(`select count(*) from "attributes" where "key" = ?`, Key).
Scan(&ctr) Scan(&ctr)
@ -54,9 +56,7 @@ func Add(Key, Name, Description, Default string) error {
} }
func RestoreDefault(key string) error { func RestoreDefault(key string) error {
result := store.DB.Exec(`update "attributes" set value = ( result := store.DB.Exec(`update "attributes" set value = "default" where "key" = ?`, key)
select "default" from "attributes" where "key" = ?
) where "key" = ?`, key, key)
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
return ErrorAttributeNotFound return ErrorAttributeNotFound
} }
@ -79,3 +79,20 @@ func Set(key, value string) error {
} }
return result.Error return result.Error
} }
var load []*func()
var loadLock sync.Mutex
func AddSoftLoad(f *func()) {
loadLock.Lock()
load = append(load, f)
loadLock.Unlock()
}
func Load() {
for _, f := range load {
if f != nil {
(*f)()
}
}
}

View File

@ -13,10 +13,10 @@ func TestAttr(t *testing.T) {
viper.Set("db.type", "sqlite") viper.Set("db.type", "sqlite")
store.Setup() store.Setup()
Init() Init()
fmt.Println(Add("test", "test", "", "1")) fmt.Println(Add("test", "test", "", "1", false))
fmt.Println(Add("test1", "test1", "", "2")) fmt.Println(Add("test1", "test1", "", "2", false))
fmt.Println(Add("test1", "test2", "", "2")) fmt.Println(Add("test1", "test2", "", "2", false))
fmt.Println(Add("test1", "test3", "", "2")) fmt.Println(Add("test1", "test3", "", "2", false))
fmt.Println(Set("test", "reset")) fmt.Println(Set("test", "reset"))
fmt.Println(Get("test")) fmt.Println(Get("test"))
} }

View File

@ -12,7 +12,6 @@ type Service struct{}
func (srv Service) GetName() string { return "attribute.Service" } func (srv Service) GetName() string { return "attribute.Service" }
func (srv Service) GetDependencies() []string { return []string{} } func (srv Service) GetDependencies() []string { return []string{} }
func (srv Service) IsService() bool { return false } func (srv Service) IsService() bool { return false }
func (srv Service) Main() error { return nil }
func (srv Service) Del() {} func (srv Service) Del() {}
func (srv Service) Health() error { return nil } func (srv Service) Health() error { return nil }
@ -59,11 +58,10 @@ func (srv Service) Load() error {
if err := c.ShouldBindJSON(&data); err != nil { if err := c.ShouldBindJSON(&data); err != nil {
panic(err) panic(err)
} }
result := store.DB.Exec(`update "attributes" set value = "default" where "key" = ?`, data.Key) if err := RestoreDefault(data.Key); err != nil {
if result.Error != nil { panic(err)
panic(result.Error)
} }
server.OK(c, result.RowsAffected) server.OK(c, "ok")
}) })
api.POST("restore_all", func(c *gin.Context) { api.POST("restore_all", func(c *gin.Context) {
@ -74,5 +72,15 @@ func (srv Service) Load() error {
server.OK(c, result.RowsAffected) server.OK(c, result.RowsAffected)
}) })
api.POST("reload", func(c *gin.Context) {
Load()
server.OK(c, "ok")
})
return nil
}
func (srv Service) Main() error {
Load()
return nil return nil
} }