Settings defaults

This commit is contained in:
Daniel Ponte 2025-02-23 18:15:59 -05:00
parent 051bae5dd2
commit 31d50f1191
3 changed files with 62 additions and 20 deletions

View file

@ -17,6 +17,7 @@ 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"
@ -191,6 +192,11 @@ 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,
@ -204,7 +210,6 @@ 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()
}() }()

7
pkg/settings/defaults.go Normal file
View file

@ -0,0 +1,7 @@
package settings
type Defaults map[string]Setting
var ConfigDefaults = Defaults{
"calls.view.showSourceAlias": false,
}

View file

@ -19,17 +19,23 @@ var (
type Store interface { type Store interface {
// Get gets a setting and unmarshals it into dst. // Get gets a setting and unmarshals it into dst.
Get(ctx context.Context, name string, dest interface{}) error Get(ctx context.Context, name string) (Setting, error)
// Set sets a setting. // Set sets a setting.
Set(ctx context.Context, name string, val interface{}) 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
} }
type Setting interface {
}
type postgresStore struct { type postgresStore struct {
c cache.Cache[string, []byte] c cache.Cache[string, Setting]
} }
type storeCtxKey string type storeCtxKey string
@ -51,38 +57,44 @@ func FromCtx(ctx context.Context) Store {
func New() *postgresStore { func New() *postgresStore {
s := &postgresStore{ s := &postgresStore{
c: cache.New[string, []byte](), c: cache.New[string, Setting](),
} }
return s return s
} }
func (s *postgresStore) Get(ctx context.Context, name string, dest interface{}) error { func (s *postgresStore) Get(ctx context.Context, name string) (Setting, error) {
_, err := rbac.Check(ctx, rbac.UseResource(entities.ResourceSetting), rbac.WithActions(entities.ActionRead)) _, err := rbac.Check(ctx, rbac.UseResource(entities.ResourceSetting), rbac.WithActions(entities.ActionRead))
if err != nil { if err != nil {
return err return nil, err
} }
ci, has := s.c.Get(name) ci, has := s.c.Get(name)
if !has { if has {
return ci, nil
}
db := database.FromCtx(ctx) db := database.FromCtx(ctx)
ci, 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) {
return ErrNoSetting return nil, ErrNoSetting
} }
return err return nil, err
}
err = json.Unmarshal(cBytes, ci)
if err != nil {
return nil, err
} }
s.c.Set(name, ci) s.c.Set(name, ci)
}
return json.Unmarshal(ci, dest) return ci, nil
} }
func (s *postgresStore) Set(ctx context.Context, name string, val interface{}) error { func (s *postgresStore) Set(ctx context.Context, name string, val Setting) error {
subj, err := rbac.Check(ctx, rbac.UseResource(entities.ResourceSetting), rbac.WithActions(entities.ActionCreate, entities.ActionUpdate)) subj, err := rbac.Check(ctx, rbac.UseResource(entities.ResourceSetting), rbac.WithActions(entities.ActionCreate, entities.ActionUpdate))
if err != nil { if err != nil {
return err return err
@ -115,7 +127,7 @@ func (s *postgresStore) Set(ctx context.Context, name string, val interface{}) e
return err return err
} }
s.c.Set(name, b) s.c.Set(name, val)
return nil return nil
} }
@ -129,3 +141,21 @@ 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
}