-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
71 lines (56 loc) · 1.61 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
package geoipfix
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/pkg/errors"
)
type corsConfig struct {
AllowedOrigins []string `json:"allowed_origins"`
AllowedMethods []string `json:"allowed_methods"`
AllowedHeaders []string `json:"allowed_headers"`
AllowCredentials bool `json:"allow_credentials"`
ExposedHeaders []string `json:"exposed_headers"`
MaxAge int `json:"max_age"`
}
type serverRPCConfig struct {
Port int `json:"port"`
}
type serverHTTPConfig struct {
Port int `json:"port"`
Cors corsConfig `json:"cors"`
}
type serverConfig struct {
HTTP *serverHTTPConfig `json:"http"`
RPC *serverRPCConfig `json:"rpc"`
}
// config is geoipfix config
type config struct {
Debug bool `json:"debug"`
DatabasePath string `json:"database_path"`
Server serverConfig `json:"server"`
}
// loadConfig return a jsonq instance from a config path
func loadConfig(path string) (*config, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "Config file %s cannot be loaded", path)
}
return loadConfigFromContent(string(content))
}
// loadConfigFromContent returns a jsonq instance from a config content
func loadConfigFromContent(content string) (*config, error) {
cfg := &config{}
dec := json.NewDecoder(strings.NewReader(content))
err := dec.Decode(cfg)
if err != nil {
return nil, errors.Wrapf(err, "Config file %s cannot be parsed", content)
}
if cfg.DatabasePath == "" {
cfg.DatabasePath = DatabaseURL
}
if cfg.Server.HTTP.Port == 0 {
cfg.Server.HTTP.Port = DefaultPort
}
return cfg, nil
}