jit default
This commit is contained in:
parent
31d50f1191
commit
b275881697
2 changed files with 12 additions and 32 deletions
|
@ -17,7 +17,6 @@ import (
|
|||
"dynatron.me/x/stillbox/pkg/nexus"
|
||||
"dynatron.me/x/stillbox/pkg/notify"
|
||||
"dynatron.me/x/stillbox/pkg/rbac"
|
||||
"dynatron.me/x/stillbox/pkg/rbac/entities"
|
||||
"dynatron.me/x/stillbox/pkg/rbac/policy"
|
||||
"dynatron.me/x/stillbox/pkg/rest"
|
||||
"dynatron.me/x/stillbox/pkg/services"
|
||||
|
@ -113,7 +112,7 @@ func New(ctx context.Context, cfg *config.Configuration) (*Server, error) {
|
|||
incidents: incstore.NewStore(),
|
||||
rbac: rbacSvc,
|
||||
stats: statsSvc,
|
||||
settings: settings.New(),
|
||||
settings: settings.New(settings.ConfigDefaults),
|
||||
}
|
||||
|
||||
if cfg.DB.Partition.Enabled {
|
||||
|
@ -180,7 +179,6 @@ func (s *Server) fillCtx(ctx context.Context) context.Context {
|
|||
ctx = shares.CtxWithStore(ctx, s.share)
|
||||
ctx = rbac.CtxWithRBAC(ctx, s.rbac)
|
||||
ctx = stats.CtxWithStats(ctx, s.stats)
|
||||
ctx = settings.CtxWithStore(ctx, s.settings)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
@ -192,11 +190,6 @@ func (s *Server) Go(ctx context.Context) error {
|
|||
|
||||
ctx = s.fillCtx(ctx)
|
||||
|
||||
err := s.settings.PrimeDefaults(entities.CtxWithServiceSubject(ctx, "settings"), settings.ConfigDefaults)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
httpSrv := &http.Server{
|
||||
Addr: s.conf.Listen,
|
||||
Handler: s.r,
|
||||
|
@ -210,6 +203,7 @@ func (s *Server) Go(ctx context.Context) error {
|
|||
go pm.Go(ctx)
|
||||
}
|
||||
|
||||
var err error
|
||||
go func() {
|
||||
err = httpSrv.ListenAndServe()
|
||||
}()
|
||||
|
|
|
@ -24,9 +24,6 @@ type Store interface {
|
|||
// Set sets a setting.
|
||||
Set(ctx context.Context, name string, val Setting) error
|
||||
|
||||
// PrimeDefaults primes the cache with defaults and sets them in the database if they do not exist.
|
||||
PrimeDefaults(ctx context.Context, def Defaults) error
|
||||
|
||||
// Delete removes a setting.
|
||||
Delete(ctx context.Context, name string) error
|
||||
}
|
||||
|
@ -35,7 +32,8 @@ type Setting interface {
|
|||
}
|
||||
|
||||
type postgresStore struct {
|
||||
c cache.Cache[string, Setting]
|
||||
c cache.Cache[string, Setting]
|
||||
defaults Defaults
|
||||
}
|
||||
|
||||
type storeCtxKey string
|
||||
|
@ -55,9 +53,10 @@ func FromCtx(ctx context.Context) Store {
|
|||
return s
|
||||
}
|
||||
|
||||
func New() *postgresStore {
|
||||
func New(defaults Defaults) *postgresStore {
|
||||
s := &postgresStore{
|
||||
c: cache.New[string, Setting](),
|
||||
c: cache.New[string, Setting](),
|
||||
defaults: defaults,
|
||||
}
|
||||
|
||||
return s
|
||||
|
@ -78,6 +77,11 @@ func (s *postgresStore) Get(ctx context.Context, name string) (Setting, error) {
|
|||
cBytes, err := db.GetSetting(ctx, name)
|
||||
if err != nil {
|
||||
if database.IsNoRows(err) {
|
||||
def, hasDefault := s.defaults[name]
|
||||
if hasDefault {
|
||||
return def, nil
|
||||
}
|
||||
|
||||
return nil, ErrNoSetting
|
||||
}
|
||||
|
||||
|
@ -141,21 +145,3 @@ func (s *postgresStore) Delete(ctx context.Context, name string) error {
|
|||
s.c.Delete(name)
|
||||
return database.FromCtx(ctx).DeleteSetting(ctx, name)
|
||||
}
|
||||
|
||||
func (s *postgresStore) PrimeDefaults(ctx context.Context, def Defaults) error {
|
||||
for k, v := range def {
|
||||
_, err := s.Get(ctx, k)
|
||||
switch err {
|
||||
case nil:
|
||||
case ErrNoSetting:
|
||||
err = s.Set(ctx, k, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue