diff options
Diffstat (limited to 'system/db/config.go')
-rw-r--r-- | system/db/config.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/system/db/config.go b/system/db/config.go index b5a07e4..4bbf29b 100644 --- a/system/db/config.go +++ b/system/db/config.go @@ -3,6 +3,7 @@ package db import ( "bytes" "encoding/json" + "fmt" "net/url" "strings" @@ -117,7 +118,50 @@ func ConfigAll() ([]byte, error) { return val.Bytes(), nil } +// PutConfig updates a single k/v in the config +func PutConfig(key string, value interface{}) error { + kv := make(map[string]interface{}) + + c, err := ConfigAll() + if err != nil { + return err + } + + err = json.Unmarshal(c, &kv) + if err != nil { + return err + } + + // set k/v from params to decoded map + kv[key] = value + + data := make(url.Values) + for k, v := range kv { + switch v.(type) { + case string: + data.Set(k, v.(string)) + + case []string: + vv := v.([]string) + for i := range vv { + data.Add(k, vv[i]) + } + + default: + data.Set(k, fmt.Sprintf("%v", v)) + } + } + + err = SetConfig(data) + if err != nil { + return err + } + + return nil +} + // ConfigCache is a in-memory cache of the Configs for quicker lookups +// 'key' is the JSON tag associated with the config field func ConfigCache(key string) string { return configCache.Get(key) } |