-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
71 lines (61 loc) · 1.42 KB
/
config.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
const (
configFileName = "rfb.json"
)
// Settings is a main struct for settings
type Settings struct {
APIKey string `json:"api-key"`
Addr string `json:"addr"`
Couchbase CouchbaseSettings `json:"couchbase"`
StaticDirPath string `json:"static-dir-path"`
}
// CouchbaseSettings is a sub truct for couchbase settings
type CouchbaseSettings struct {
Cluster string `json:"cluster"`
Bucket string `json:"bucket"`
Secret string `json:"secret"`
}
// LoadConfig function load a config file
func LoadConfig() {
settings.APIKey = ""
settings.Addr = ":8088"
settings.Couchbase.Cluster = "couchbase://couchbase"
settings.Couchbase.Bucket = "default"
settings.Couchbase.Secret = ""
f, err := os.Open(configFileName)
if err != nil {
// not found config file or config file don't be a read
return
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return
}
err = json.Unmarshal(data, &settings)
if err != nil {
log.Printf("Config file unmarshal failled! Use default settings.")
}
}
// SaveConfig function save a config file
func SaveConfig() {
f, err := os.Create(configFileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
data, err := json.MarshalIndent(&settings, "", " ")
if err != nil {
log.Fatal(err)
}
_, err = f.Write(data)
if err != nil {
log.Fatal(err)
}
}