-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_config.go
83 lines (66 loc) · 1.66 KB
/
global_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
72
73
74
75
76
77
78
79
80
81
82
83
// redis databases config
package main
import (
"encoding/json"
"io/ioutil"
)
const (
Db_addr = "127.0.0.1"
Db_port = "6379"
Db_password = "random_foo"
Description = "description"
Gplus = "google+ url"
title = "No Title"
)
type BlogConfig struct {
Description string `json:"description"`
Gplus string `json:"gplus"`
BlogTitle string `json:"title"`
}
type DbConfig struct {
Db_addr string `json:"dburl"`
Db_port string `json:"dbport"`
Db_password string `json:"dbpass"`
Bconfig BlogConfig `json:"BlogConfig"`
}
var GlobalCfg *DbConfig
func InitDbConfigWithPath(config_path string) (*DbConfig, error) {
cfg := new(DbConfig)
_cfg, err := ioutil.ReadFile(config_path)
if err != nil {
return nil, err
}
if err := json.Unmarshal(_cfg, &cfg); err != nil {
return nil, err
}
GlobalCfg = cfg
return cfg, nil
}
func InitDbConfig() (*DbConfig, error) {
cfg := new(DbConfig)
_cfg, err := ioutil.ReadFile("config.json")
if err != nil {
err := WriteConfigJsonToLocal()
if err != nil {
return nil, err
} else {
_cfg, _ = ioutil.ReadFile("config.json")
}
}
if err := json.Unmarshal(_cfg, &cfg); err != nil {
return nil, err
}
GlobalCfg = cfg
return cfg, nil
}
func WriteConfigJsonToLocal() error {
DefaultConfig := &DbConfig{}
DefaultConfig.Db_addr = Db_addr
DefaultConfig.Db_port = Db_port
DefaultConfig.Db_password = Db_password
DefaultConfig.Bconfig.Description = Description
DefaultConfig.Bconfig.Gplus = Gplus
DefaultConfig.Bconfig.BlogTitle = title
DefaultJson, _ := json.MarshalIndent(&DefaultConfig, "\n", "\t")
return ioutil.WriteFile("config.json", []byte(DefaultJson), 0644)
}