/
config.go
61 lines (53 loc) · 1.22 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
package go_conf
import (
"flag"
"github.com/kylelemons/go-gypsy/yaml"
"io/ioutil"
"log"
"os"
)
var (
exitHandler ExitHandler
config *yaml.File
environment string
config_file = flag.String("config", "./config/database.yml", "the database.yml")
log_file_name = flag.String("log", "./log/server.log", "where does the log go?")
)
func init() {
exitHandler = &StandardHandler{}
flag.Parse()
setEnv()
initlogAndConfig()
startSignalCatcher()
}
func initlogAndConfig() {
//create log
log_file, err := os.OpenFile(*log_file_name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
panic("cannot write log")
}
log.SetOutput(log_file)
log.SetFlags(5)
//read the config and build config stuff
c_file, err := ioutil.ReadFile(*config_file)
if err != nil {
log.Panic("no config file found")
}
config = yaml.Config(string(c_file))
}
func GetEnv() string {
return environment
}
func setEnv() {
environment = os.Getenv("GO_ENV")
if environment == "" {
environment = "development"
}
}
func getConfigParameter(prefix, name string) string {
param, err := config.Get(prefix + "_" + environment + "." + name)
if err != nil {
log.Panic("missing config parameter: " + prefix + " " + name)
}
return param
}