-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig.go
68 lines (56 loc) · 1.1 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
package goatee
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
type configuration struct {
Redis Redis
Web Web
}
type Redis struct {
Host string
}
type Web struct {
Host string
}
var (
DEBUG = false
Config = new(configuration)
)
func getEnv() string {
env := os.Getenv("GO_ENV")
if env == "" || env == "development" {
DEBUG = true
return "development"
}
return env
}
func LoadConfig(path string) *configuration {
var file []byte
var err error
var paths = []string{os.Getenv("HOME") + "/.config/goatee", "/etc/goatee"}
// If path is defined, prepend it to paths
if len(path) > 0 {
paths = append([]string{path}, paths...)
}
// Try to find a config file to use
found := false
for _, path := range paths {
file, err = ioutil.ReadFile(path + string(os.PathSeparator) + getEnv() + ".json")
if err == nil {
log.Printf("Reading configuration from: %s", path)
found = true
break
}
}
if !found {
log.Fatalf("Error reading config file.")
}
err = json.Unmarshal(file, &Config)
if err != nil {
log.Fatalf("Error parsing JSON: %s", err.Error())
}
return Config
}