jit default

This commit is contained in:
Daniel Ponte 2025-02-24 08:48:44 -05:00
parent 31d50f1191
commit b275881697
2 changed files with 12 additions and 32 deletions

View file

@ -17,7 +17,6 @@ import (
"dynatron.me/x/stillbox/pkg/nexus" "dynatron.me/x/stillbox/pkg/nexus"
"dynatron.me/x/stillbox/pkg/notify" "dynatron.me/x/stillbox/pkg/notify"
"dynatron.me/x/stillbox/pkg/rbac" "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/rbac/policy"
"dynatron.me/x/stillbox/pkg/rest" "dynatron.me/x/stillbox/pkg/rest"
"dynatron.me/x/stillbox/pkg/services" "dynatron.me/x/stillbox/pkg/services"
@ -113,7 +112,7 @@ func New(ctx context.Context, cfg *config.Configuration) (*Server, error) {
incidents: incstore.NewStore(), incidents: incstore.NewStore(),
rbac: rbacSvc, rbac: rbacSvc,
stats: statsSvc, stats: statsSvc,
settings: settings.New(), settings: settings.New(settings.ConfigDefaults),
} }
if cfg.DB.Partition.Enabled { 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 = shares.CtxWithStore(ctx, s.share)
ctx = rbac.CtxWithRBAC(ctx, s.rbac) ctx = rbac.CtxWithRBAC(ctx, s.rbac)
ctx = stats.CtxWithStats(ctx, s.stats) ctx = stats.CtxWithStats(ctx, s.stats)
ctx = settings.CtxWithStore(ctx, s.settings)
return ctx return ctx
} }
@ -192,11 +190,6 @@ func (s *Server) Go(ctx context.Context) error {
ctx = s.fillCtx(ctx) ctx = s.fillCtx(ctx)
err := s.settings.PrimeDefaults(entities.CtxWithServiceSubject(ctx, "settings"), settings.ConfigDefaults)
if err != nil {
return err
}
httpSrv := &http.Server{ httpSrv := &http.Server{
Addr: s.conf.Listen, Addr: s.conf.Listen,
Handler: s.r, Handler: s.r,
@ -210,6 +203,7 @@ func (s *Server) Go(ctx context.Context) error {
go pm.Go(ctx) go pm.Go(ctx)
} }
var err error
go func() { go func() {
err = httpSrv.ListenAndServe() err = httpSrv.ListenAndServe()
}() }()

View file

@ -24,9 +24,6 @@ type Store interface {
// Set sets a setting. // Set sets a setting.
Set(ctx context.Context, name string, val Setting) error 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 removes a setting.
Delete(ctx context.Context, name string) error Delete(ctx context.Context, name string) error
} }
@ -36,6 +33,7 @@ type Setting interface {
type postgresStore struct { type postgresStore struct {
c cache.Cache[string, Setting] c cache.Cache[string, Setting]
defaults Defaults
} }
type storeCtxKey string type storeCtxKey string
@ -55,9 +53,10 @@ func FromCtx(ctx context.Context) Store {
return s return s
} }
func New() *postgresStore { func New(defaults Defaults) *postgresStore {
s := &postgresStore{ s := &postgresStore{
c: cache.New[string, Setting](), c: cache.New[string, Setting](),
defaults: defaults,
} }
return s return s
@ -78,6 +77,11 @@ func (s *postgresStore) Get(ctx context.Context, name string) (Setting, error) {
cBytes, err := db.GetSetting(ctx, name) cBytes, err := db.GetSetting(ctx, name)
if err != nil { if err != nil {
if database.IsNoRows(err) { if database.IsNoRows(err) {
def, hasDefault := s.defaults[name]
if hasDefault {
return def, nil
}
return nil, ErrNoSetting return nil, ErrNoSetting
} }
@ -141,21 +145,3 @@ func (s *postgresStore) Delete(ctx context.Context, name string) error {
s.c.Delete(name) s.c.Delete(name)
return database.FromCtx(ctx).DeleteSetting(ctx, 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
}