update
							parent
							
								
									e3b864e29d
								
							
						
					
					
						commit
						074d1d5582
					
				| 
						 | 
					@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -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",
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -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()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -24,7 +24,7 @@ func (grp *Group) BeforeSave(tx *gorm.DB) (err error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// set displayname
 | 
						// set displayname
 | 
				
			||||||
	if grp.DisplayName == "" {
 | 
						if grp.DisplayName == "" {
 | 
				
			||||||
		grp.DisplayName = strings.TrimPrefix(grp.Name, "*")
 | 
							grp.DisplayName = strings.TrimPrefix(grp.Name, SYS_AUTH_PREFIX)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@ package app
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"github.com/spf13/viper"
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
 | 
						"kumoly.io/kumoly/app/attribute"
 | 
				
			||||||
	"kumoly.io/kumoly/app/auth"
 | 
						"kumoly.io/kumoly/app/auth"
 | 
				
			||||||
	"kumoly.io/kumoly/app/control"
 | 
						"kumoly.io/kumoly/app/control"
 | 
				
			||||||
	"kumoly.io/kumoly/app/email"
 | 
						"kumoly.io/kumoly/app/email"
 | 
				
			||||||
| 
						 | 
					@ -26,6 +27,7 @@ func Default() *system.System {
 | 
				
			||||||
		&history.Service{},
 | 
							&history.Service{},
 | 
				
			||||||
		&email.Service{},
 | 
							&email.Service{},
 | 
				
			||||||
		&control.Service{},
 | 
							&control.Service{},
 | 
				
			||||||
 | 
							&attribute.Service{},
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	return sys
 | 
						return sys
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,6 @@ import (
 | 
				
			||||||
func TestMain(t *testing.T) {
 | 
					func TestMain(t *testing.T) {
 | 
				
			||||||
	sys := Default()
 | 
						sys := Default()
 | 
				
			||||||
	go sys.Start()
 | 
						go sys.Start()
 | 
				
			||||||
 | 
					 | 
				
			||||||
	<-sys.Done()
 | 
						<-sys.Done()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -49,6 +49,7 @@ type System struct {
 | 
				
			||||||
	isService  bool
 | 
						isService  bool
 | 
				
			||||||
	status     state
 | 
						status     state
 | 
				
			||||||
	lock       sync.Mutex
 | 
						lock       sync.Mutex
 | 
				
			||||||
 | 
						started    chan struct{}
 | 
				
			||||||
	term       chan struct{}
 | 
						term       chan struct{}
 | 
				
			||||||
	done       chan struct{}
 | 
						done       chan struct{}
 | 
				
			||||||
	notifyDone chan struct{}
 | 
						notifyDone chan struct{}
 | 
				
			||||||
| 
						 | 
					@ -91,6 +92,7 @@ func (sys *System) order() {
 | 
				
			||||||
func New() *System {
 | 
					func New() *System {
 | 
				
			||||||
	sys := &System{}
 | 
						sys := &System{}
 | 
				
			||||||
	sys.done = make(chan struct{}, 1)
 | 
						sys.done = make(chan struct{}, 1)
 | 
				
			||||||
 | 
						sys.started = make(chan struct{}, 1)
 | 
				
			||||||
	sys.term = make(chan struct{}, 1)
 | 
						sys.term = make(chan struct{}, 1)
 | 
				
			||||||
	sys.notifyDone = make(chan struct{}, 1)
 | 
						sys.notifyDone = make(chan struct{}, 1)
 | 
				
			||||||
	sys.quit = make(chan os.Signal, 1)
 | 
						sys.quit = make(chan os.Signal, 1)
 | 
				
			||||||
| 
						 | 
					@ -117,20 +119,22 @@ func (sys *System) Start() {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		l.Info().Msg("Starting...")
 | 
							l.Info().Msg("Starting...")
 | 
				
			||||||
		if e := l.Trace(); e.Enabled() {
 | 
							// if e := l.Trace(); e.Enabled() {
 | 
				
			||||||
			config, err := ConfigShow("")
 | 
							// 	config, err := ConfigShow("")
 | 
				
			||||||
			if err == nil {
 | 
							// 	if err == nil {
 | 
				
			||||||
				e.Str("config", config).Msg("config")
 | 
							// 		e.Str("config", config).Msg("config")
 | 
				
			||||||
			}
 | 
							// 	}
 | 
				
			||||||
		}
 | 
							// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		sys.status = sys_main
 | 
							sys.status = sys_main
 | 
				
			||||||
		sys.main()
 | 
							sys.main()
 | 
				
			||||||
		sys.status = sys_wait
 | 
							sys.status = sys_wait
 | 
				
			||||||
 | 
							sys.started <- struct{}{}
 | 
				
			||||||
		if !sys.isService {
 | 
							if !sys.isService {
 | 
				
			||||||
			sys.quit <- syscall.SIGTERM
 | 
								sys.quit <- syscall.SIGTERM
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
						<-sys.started
 | 
				
			||||||
	history.Send(history.Info().Nm("SysUP").Msg("system started"))
 | 
						history.Send(history.Info().Nm("SysUP").Msg("system started"))
 | 
				
			||||||
	<-sys.quit
 | 
						<-sys.quit
 | 
				
			||||||
	isRestart := false
 | 
						isRestart := false
 | 
				
			||||||
| 
						 | 
					@ -138,8 +142,8 @@ func (sys *System) Start() {
 | 
				
			||||||
		isRestart = true
 | 
							isRestart = true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	l.Info().Msg("Terminating...")
 | 
					 | 
				
			||||||
	history.Send(history.Info().Nm("SysDown").Msg("system stoped"))
 | 
						history.Send(history.Info().Nm("SysDown").Msg("system stoped"))
 | 
				
			||||||
 | 
						l.Info().Msg("Terminating...")
 | 
				
			||||||
	sys.status = sys_term
 | 
						sys.status = sys_term
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		time.Sleep(time.Second * time.Duration(viper.GetInt("system.terminate_timeout")))
 | 
							time.Sleep(time.Second * time.Duration(viper.GetInt("system.terminate_timeout")))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue