/
appengineconfig.go
50 lines (43 loc) · 1.27 KB
/
appengineconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package appengineconfig
import (
"fmt"
"sync"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
var ConfigName = "AppEngineConfig" // default key-prefix to utilize
type Value struct {
Val string
}
var configCache map[string]string
var configLock sync.Mutex
func init() {
configCache = make(map[string]string)
}
func Get(c context.Context, key, def string, values ...interface{}) string {
configLock.Lock()
defer configLock.Unlock()
val, ok := configCache[key]
if ok {
return fmt.Sprintf(val, values...)
}
value := Value{}
dskey := datastore.NewKey(c, ConfigName, key, 0, nil)
err := datastore.Get(c, dskey, &value)
if err == datastore.ErrNoSuchEntity {
value.Val = def
log.Infof(c, "Creating default config for key - %s - default value is - %s", key, value.Val)
_, err = datastore.Put(c, dskey, &value)
if err != nil {
log.Errorf(c, "Error creating default config for key - %s - error is - %v", key, err)
}
return fmt.Sprintf(def, values...) // return default, totally new config setting
}
if err != nil {
log.Errorf(c, "Error fetching config for key - %s - error is - %v", key, err)
return def // error, return the default
}
configCache[key] = value.Val
return fmt.Sprintf(value.Val, values...)
}