/
config.go
49 lines (41 loc) · 1.01 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
package config
import (
"encoding/json"
"fmt"
"github.com/PasteUs/PasteMeGoBackend/common/flag"
"io/ioutil"
"os"
)
type Database struct {
Type string `json:"type"`
Username string `json:"username"`
Password string `json:"password"`
Server string `json:"server"`
Port uint16 `json:"port"`
Database string `json:"database"`
}
type config struct {
Address string `json:"address"`
Port uint16 `json:"port"`
Secret string `json:"secret"`
LogFile string `json:"log_file"`
Database Database `json:"database"`
}
var Config config
func init() {
load(flag.Config)
}
func load(filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
pwd, _ := os.Getwd()
panic(fmt.Sprintf("open %s under %s failed, error = \"%s\"\n", filename, pwd, err.Error()))
}
if flag.Debug {
fmt.Printf("read %s done, got %s\n", filename, string(data))
}
err = json.Unmarshal(data, &Config)
if err != nil {
panic(fmt.Sprintf("parse Config failed, error = \"%s\"\n", err.Error()))
}
}