-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.go
76 lines (58 loc) · 1.6 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(
"flag"
"log"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type Config struct {
MaxRequests int
Port int
Username string
Password string
YamlConfigFile string
UIEnabled bool
}
type yamlHubConfig struct {
ForwardURL string `yaml:"forward_url,omitempty"`
}
type yamlConfig struct {
Hubs map[string]yamlHubConfig `yaml:"hubs"`
}
func NewConfig() *Config {
var parseReq = flag.Int("r", DEFAULT_MAX_REQUESTS, "max requests to store")
var parsePort = flag.Int("p", DEFAULT_PORT, "which port to bind to")
var parseUser = flag.String("username", "", "HTTP Basic Auth Username for accessing hub")
var parsePass = flag.String("password", "", "HTTP Basic Auth Password for accessing hub")
var configFile = flag.String("config", "", "YAML Configuration File")
var noWeb = flag.Bool("noweb", false, "Disable the web UI")
flag.Parse()
return &Config{ *parseReq, *parsePort, *parseUser, *parsePass, *configFile, !(*noWeb) }
}
func (c *Config) AuthEnabled() bool {
return c.Username != "" && c.Password != ""
}
func (c *Config) HasYAMLConfig() bool {
return c.YamlConfigFile != ""
}
func (c *Config) ApplyYAMLConfig(db *HubDatabase) error {
config, err := ioutil.ReadFile(c.YamlConfigFile)
if err != nil {
return err
}
var cfg yamlConfig
err = yaml.Unmarshal(config, &cfg)
if err != nil {
return err
}
for id, val := range cfg.Hubs {
if db.Get(id) == nil {
log.Printf("Created %s from config file\n", id)
hub, _ := db.Create(id)
if val.ForwardURL != "" {
hub.ForwardURL = val.ForwardURL
}
}
}
return nil
}