diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-01-13 13:13:07 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-01-13 13:13:07 -0800 |
commit | ffa8654af317bfb05b1d38a17aa4a972d1c8056d (patch) | |
tree | 3f434769aab44f1437f5438eae9942871f98950b | |
parent | 59ee9fbb237673c2d0cccad42f7adbae65852b8d (diff) |
adding more precise timestamp creation algorithm, and a LoadCacheConfig func to warm the config cache vs. trying to set defaults on init so the cache exists
-rw-r--r-- | system/admin/handlers.go | 2 | ||||
-rw-r--r-- | system/admin/upload/upload.go | 2 | ||||
-rw-r--r-- | system/api/external.go | 2 | ||||
-rw-r--r-- | system/db/config.go | 36 | ||||
-rw-r--r-- | system/db/init.go | 17 |
5 files changed, 42 insertions, 17 deletions
diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 59e7a66..00add87 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -1533,7 +1533,7 @@ func editHandler(res http.ResponseWriter, req *http.Request) { // create a timestamp if one was not set if ts == "" { - ts := fmt.Sprintf("%d", time.Now().Unix()*1000) + ts := fmt.Sprintf("%d", int64(time.Nanosecond)*time.Now().UnixNano()/int64(time.Millisecond)) req.PostForm.Set("timestamp", ts) } diff --git a/system/admin/upload/upload.go b/system/admin/upload/upload.go index 486f55c..6b99dfc 100644 --- a/system/admin/upload/upload.go +++ b/system/admin/upload/upload.go @@ -20,7 +20,7 @@ func StoreFiles(req *http.Request) (map[string]string, error) { ts := req.FormValue("timestamp") // timestamp in milliseconds since unix epoch if ts == "" { - ts = fmt.Sprintf("%d", time.Now().Unix()*1000) // Unix() returns seconds since unix epoch + ts = fmt.Sprintf("%d", int64(time.Nanosecond)*time.Now().UnixNano()/int64(time.Millisecond)) // Unix() returns seconds since unix epoch } req.Form.Set("timestamp", ts) diff --git a/system/api/external.go b/system/api/external.go index 662fc07..5d4b302 100644 --- a/system/api/external.go +++ b/system/api/external.go @@ -61,7 +61,7 @@ func externalContentHandler(res http.ResponseWriter, req *http.Request) { return } - ts := fmt.Sprintf("%d", time.Now().Unix()*1000) + ts := fmt.Sprintf("%d", int64(time.Nanosecond)*time.Now().UnixNano()/int64(time.Millisecond)) req.PostForm.Set("timestamp", ts) req.PostForm.Set("updated", ts) diff --git a/system/db/config.go b/system/db/config.go index f1d1215..ecddd54 100644 --- a/system/db/config.go +++ b/system/db/config.go @@ -169,3 +169,39 @@ func PutConfig(key string, value interface{}) error { func ConfigCache(key string) interface{} { return configCache.Get(key) } + +// LoadCacheConfig loads the config into a cache to be accessed by ConfigCache() +func LoadCacheConfig() error { + c, err := ConfigAll() + if err != nil { + return err + } + + // convert json => map[string]interface{} => url.Values + var kv map[string]interface{} + err = json.Unmarshal(c, &kv) + if err != nil { + return err + } + + data := make(url.Values) + for k, v := range kv { + switch v.(type) { + case []string: + s := v.([]string) + for i := range s { + if i == 0 { + data.Set(k, s[i]) + } + + data.Add(k, s[i]) + } + default: + data.Set(k, fmt.Sprintf("%v", v)) + } + } + + configCache = data + + return nil +} diff --git a/system/db/init.go b/system/db/init.go index 98ba056..769137d 100644 --- a/system/db/init.go +++ b/system/db/init.go @@ -1,10 +1,8 @@ package db import ( - "encoding/json" "log" - "github.com/ponzu-cms/ponzu/system/admin/config" "github.com/ponzu-cms/ponzu/system/item" "github.com/boltdb/bolt" @@ -57,18 +55,9 @@ func Init() { } } - // seed db with configs structure if not present - b := tx.Bucket([]byte("__config")) - if b.Get([]byte("settings")) == nil { - j, err := json.Marshal(&config.Config{}) - if err != nil { - return err - } - - err = b.Put([]byte("settings"), j) - if err != nil { - return err - } + err := LoadCacheConfig() + if err != nil { + return err } clientSecret := ConfigCache("client_secret").(string) |