-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
76 lines (55 loc) · 1.28 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
69
70
71
72
73
74
75
76
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
)
type Config struct {
HttpPort int
DatabaseFileLocation string
}
func (c Config) GetDatabaseFileLocation() string {
if c.DatabaseFileLocation == "" {
return "./games.db"
}
return c.DatabaseFileLocation
}
func ReadConfig() (Config, error) {
var c Config
var e error
rawConfigData, e := ioutil.ReadFile("config.json")
if e!=nil {
e = nil
e = writeDefaultConfig()
if e != nil {
errMsg := fmt.Sprintf("config.json was not found. Addtionally the following error was encountered while trying to generate a default config.json: %s", e)
log.Fatal(errMsg)
}
log.Fatal("config.json not found. A default one has been generated. Please update its values appropriately.")
}
e = json.Unmarshal(rawConfigData, &c)
if (e!=nil) {
log.Fatal(e)
}
e = verifyConfig(c)
return c, e
}
func writeDefaultConfig() error {
var c Config
var e error
c.HttpPort = -1
j, e := json.Marshal(&c)
if e != nil {
return e
}
ioutil.WriteFile("config.json", j, 0664)
return e
}
func verifyConfig(c Config) error {
var e error
if c.HttpPort == -1 {
log.Fatal(fmt.Sprintf("Change the defaults supplied in config.json (HttpPort is still -1)"))
}
return e
}