This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.go
127 lines (107 loc) · 2.79 KB
/
conf.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Package goconf implements simple concurency-safe configuration tool.
All configuration parameters are read from json encoded file.
Default file name is "config.json", but can be changed easily (see LoadConfig)
File is re-read periodically, with default interval of 5 seconds.
It can be configured via "refresh" parameter in the same config file.
Example config.json is located in the repository.
Created as a helper library for my other projects, not tested well,
use at your own risk, no guarantees!
*/
package goconf
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
"time"
)
const default_refresh_interval float64 = 5
var c = &config{
data: make(map[string]interface{}),
path: "config.json",
}
type config struct {
sync.Mutex
data map[string]interface{}
path string
modTime time.Time
}
func init() {
go func() {
for {
LoadConfig(c.path)
ri, err := Get("refresh")
if err != nil {
log.Println("'refresh' not set, unsing default :", default_refresh_interval)
ri = default_refresh_interval
}
ref_interval, ok := ri.(float64)
if !ok {
log.Println("invalid 'refresh', unsing default :", default_refresh_interval)
ref_interval = default_refresh_interval
}
time.Sleep(time.Duration(ref_interval) * time.Second)
}
}()
}
//Function Get is used to retrieve values from config.
//Can recieve multiple args in which case it tries to get deeper
//into the config json structure.
func Get(keys ...string) (res interface{}, err error) {
c.Lock()
defer c.Unlock()
res = c.data
for i := 0; i < len(keys); i++ {
t, ok := map[string]interface{}{}, false
if t, ok = res.(map[string]interface{}); !ok {
return nil, errors.New(fmt.Sprint("can't get value for ", keys))
}
if res, ok = t[keys[i]]; !ok {
return nil, errors.New(fmt.Sprint("can't get value for ", keys))
}
}
return
}
//Function LoadConfig reloads configuration data from file named by path
//path can be set to "" (empty string) in which case the last used file name
//is kept. The last successive file is always used for auto-update.
func LoadConfig(path string) error {
if path == "" {
path = c.path
}
file, err := os.Open(path)
if err != nil {
log.Println("failed to read config file :", err)
return err
}
defer file.Close()
c.Lock()
defer c.Unlock()
// Check if file changed since last read
if path == c.path {
if stat, err := file.Stat(); err != nil {
log.Println("failed to read config file :", err)
return err
} else {
if stat.ModTime().After(c.modTime) {
c.modTime = stat.ModTime()
} else {
return nil
}
}
}
data, err := ioutil.ReadAll(file)
if err != nil {
log.Println("failed to read config file :", err)
return err
}
if err := json.Unmarshal(data, &c.data); err != nil {
return err
}
c.path = path
return nil
}