diff options
Diffstat (limited to 'system/db')
-rw-r--r-- | system/db/config.go | 32 | ||||
-rw-r--r-- | system/db/init.go | 10 |
2 files changed, 36 insertions, 6 deletions
diff --git a/system/db/config.go b/system/db/config.go index e421ff5..5bee0b3 100644 --- a/system/db/config.go +++ b/system/db/config.go @@ -2,19 +2,32 @@ package db import ( "bytes" + "encoding/base64" "encoding/json" + "fmt" "net/url" + "time" "github.com/boltdb/bolt" "github.com/gorilla/schema" "github.com/nilslice/cms/system/admin/config" ) +var configCache url.Values + +func init() { + configCache = make(url.Values) +} + // SetConfig sets key:value pairs in the db for configuration settings func SetConfig(data url.Values) error { err := store.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("_config")) + if data.Get("cache") == "invalidate" { + data.Set("etag", NewEtag()) + } + cfg := &config.Config{} dec := schema.NewDecoder() dec.SetAliasTag("json") // allows simpler struct tagging when creating a content type @@ -40,6 +53,8 @@ func SetConfig(data url.Values) error { return err } + configCache = data + return nil } @@ -52,6 +67,10 @@ func Config(key string) ([]byte, error) { return nil, err } + if len(cfg) < 1 { + return nil, nil + } + err = json.Unmarshal(cfg, &kv) if err != nil { return nil, err @@ -75,3 +94,16 @@ func ConfigAll() ([]byte, error) { return val.Bytes(), nil } + +// ConfigCache is a in-memory cache of the Configs for quicker lookups +func ConfigCache(key string) string { + return configCache.Get(key) +} + +// NewEtag generates a new Etag for response caching +func NewEtag() string { + now := fmt.Sprintf("%d", time.Now().Unix()) + etag := base64.StdEncoding.EncodeToString([]byte(now)) + + return etag +} diff --git a/system/db/init.go b/system/db/init.go index 7d6005e..008449f 100644 --- a/system/db/init.go +++ b/system/db/init.go @@ -52,13 +52,10 @@ func Init() { } } - clientSecret, err := Config("client_secret") - if err != nil { - return err - } + clientSecret := ConfigCache("client_secret") - if clientSecret != nil { - jwt.Secret(clientSecret) + if clientSecret != "" { + jwt.Secret([]byte(clientSecret)) } return nil @@ -76,6 +73,7 @@ func SystemInitComplete() bool { err := store.View(func(tx *bolt.Tx) error { users := tx.Bucket([]byte("_users")) + err := users.ForEach(func(k, v []byte) error { complete = true return nil |