-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
49 lines (45 loc) · 1.43 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
package main
import (
"encoding/json"
"fmt"
"os"
)
type Configuration struct {
BindAddress string `json:"bind_address"`
ServerID string `json:"server_id"`
FirstUserMode bool `json:"first_user_mode"`
FQDN string `json:"fqdn"`
Services []ServiceType `json:"services"`
HTTPPort string `json:"http_port"`
HTTPsPort string `json:"https_port"`
HTTPToo bool `json:"http_too"`
TLSCert string `json:"tls_cert"`
TLSKey string `json:"tls_key"`
CertAuth string `json:"cert_auth"`
DBLocation string `json:"db_location"`
SessionTokenTTL int `json:"session_token_ttl"`
ResponseCacheExpiry int `json:"response_cache_expiry"`
StatCacheTickRate int `json:"stat_cache_tick_rate"`
}
func (c *Configuration) PopulateFromJSONFile(fh string) error {
if !FileExists(fh) {
return fmt.Errorf("file does not exist: %s", fh)
}
file, err := os.Open(fh)
if err != nil {
return fmt.Errorf("could not open file: %v", err)
}
defer file.Close()
d := json.NewDecoder(file)
if err := d.Decode(c); err != nil {
return fmt.Errorf("could not decode file: %v", err)
}
return nil
}
func FileExists(fh string) bool {
info, err := os.Stat(fh)
if os.IsNotExist(err) {
return false
}
return info.Mode().IsRegular()
}