diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-10-06 03:12:14 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-10-06 03:12:14 -0700 |
commit | 698173a6176762f966be0abd1dc77b85e482a03a (patch) | |
tree | 9613cb11d115bbf644b80a84ad12d1b953d574c7 /system/db/config.go | |
parent | 179cb469c9204a463284c5823251e8cea31a4a1e (diff) |
adding config, user and refactored some content query code. all in their own files for better maintainability
Diffstat (limited to 'system/db/config.go')
-rw-r--r-- | system/db/config.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/system/db/config.go b/system/db/config.go new file mode 100644 index 0000000..e421ff5 --- /dev/null +++ b/system/db/config.go @@ -0,0 +1,77 @@ +package db + +import ( + "bytes" + "encoding/json" + "net/url" + + "github.com/boltdb/bolt" + "github.com/gorilla/schema" + "github.com/nilslice/cms/system/admin/config" +) + +// 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")) + + cfg := &config.Config{} + dec := schema.NewDecoder() + dec.SetAliasTag("json") // allows simpler struct tagging when creating a content type + dec.IgnoreUnknownKeys(true) // will skip over form values submitted, but not in struct + err := dec.Decode(cfg, data) + if err != nil { + return err + } + + j, err := json.Marshal(cfg) + if err != nil { + return err + } + + err = b.Put([]byte("settings"), j) + if err != nil { + return err + } + + return nil + }) + if err != nil { + return err + } + + return nil +} + +// Config gets the value of a key in the configuration from the db +func Config(key string) ([]byte, error) { + kv := make(map[string]interface{}) + + cfg, err := ConfigAll() + if err != nil { + return nil, err + } + + err = json.Unmarshal(cfg, &kv) + if err != nil { + return nil, err + } + + return []byte(kv[key].(string)), nil +} + +// ConfigAll gets the configuration from the db +func ConfigAll() ([]byte, error) { + val := &bytes.Buffer{} + err := store.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte("_config")) + val.Write(b.Get([]byte("settings"))) + + return nil + }) + if err != nil { + return nil, err + } + + return val.Bytes(), nil +} |