-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverConfig.go
75 lines (66 loc) · 2.04 KB
/
serverConfig.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
package util
import (
"encoding/hex"
"encoding/json"
"log"
"os"
"strconv"
)
type ServerConfig struct {
Host string
WwwPort string
ServerPort string
ClientAssets string
PersistEnabled bool
WorldBaseDir string
DbPass string
MailPass string
CookieDomain string
CookieSecure bool
CookieKeyPairs [][]byte
}
type serverConfigJson struct {
Host string
Port int
ClientAssets string `json:"client_assets"`
PersistEnabled bool `json:"persist_enabled"`
WorldBaseDir string `json:"world_base_dir"`
DbPass string `json:"db_pass"`
MailPass string `json:"mail_pass"`
CookieDomain string `json:"cookie_domain"`
CookieSecure bool `json:"cookie_secure"`
CookieKeyPairs []string `json:"cookie_key_pairs"`
}
func LoadServerConfig(path string) *ServerConfig {
file, err := os.Open(path)
if err != nil {
log.Fatalln("Couldn't open server config file:", err)
}
defer file.Close()
var configJson serverConfigJson
err = json.NewDecoder(file).Decode(&configJson)
if err != nil {
log.Fatalln("Couldn't decode server config file:", err)
}
serverConfig := new(ServerConfig)
serverConfig.Host = configJson.Host
serverConfig.WwwPort = strconv.Itoa(configJson.Port)
serverConfig.ServerPort = strconv.Itoa(configJson.Port + 1)
serverConfig.ClientAssets = configJson.ClientAssets
serverConfig.PersistEnabled = configJson.PersistEnabled
serverConfig.WorldBaseDir = configJson.WorldBaseDir
serverConfig.DbPass = configJson.DbPass
serverConfig.MailPass = configJson.MailPass
serverConfig.CookieDomain = configJson.CookieDomain
serverConfig.CookieSecure = configJson.CookieSecure
serverConfig.CookieKeyPairs = make([][]byte, 2)
for i, v := range configJson.CookieKeyPairs {
byteKey, err := hex.DecodeString(v)
keylen := len(byteKey)
if err != nil && !(i%2 == 0 || keylen == 16 || keylen == 24 || keylen == 32) {
log.Fatalln("Encryption key must be a 16, 24 or 32 byte hex string")
}
serverConfig.CookieKeyPairs[i] = byteKey
}
return serverConfig
}