/
config.go
54 lines (42 loc) · 1.13 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
package conf
import (
"encoding/json"
"log"
"os"
)
type Config struct {
Port int `json:"port"`
SecuredCookies bool `json:"securedCookies"`
AuthExpiration int64 `json:"authExpiration"`
RefreshExpiration int64 `json:"refreshExpiration"`
Domain string `json:"domain"`
BaseURL string `json:"baseURL`
}
const configPath = "config/config.json"
func LoadConfig() *Config {
f, err := os.Open(configPath)
if os.IsNotExist(err) {
return checkError(err, "WARNING : No config file detected, loading default configuration")
}
if defaultConfig := checkError(err, ""); defaultConfig != nil {
return defaultConfig
}
config := &Config{}
err = json.NewDecoder(f).Decode(config)
if defaultConfig := checkError(err, "ERROR: Malformed JSON in config file"); defaultConfig != nil {
return defaultConfig
}
return config
}
func checkError(err error, message string) *Config {
if err != nil {
log.Printf(message+" (%s)", err.Error())
return &Config{
Port: 8091,
SecuredCookies: true,
AuthExpiration: 3600 * 8,
RefreshExpiration: 3600 * 24 * 15,
}
}
return nil
}