-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
59 lines (52 loc) · 1.78 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
package main
import (
"errors"
"io/ioutil"
"log"
"github.com/criteo/http-proxy-exporter/proxyclient"
yaml "gopkg.in/yaml.v2"
)
// Config is a configuration file
type Config struct {
AuthMethods map[string]*proxyclient.AuthMethod `yaml:"auth_methods,omitempty"`
Proxies []string `yaml:"proxies"`
Targets []Target `yaml:"targets"`
SourceAddress string `yaml:"source_address,omitempty"`
ListenPort int `yaml:"listen_port,omitempty"`
Interval int `yaml:"interval,omitempty"`
Debug bool `yaml:"debug,omitempty"`
}
// Target is a HTTP(S) service that will be probed
type Target struct {
URL string `yaml:"url"`
Insecure bool `yaml:"insecure,omitempty"`
}
// loadConfig loads a configuration file and returns the corresponding struct pointer
func loadConfig(filename string) (*Config, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Printf("could not open config file : %s", err)
return &config, err
}
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Printf("error while parsing config file : %s", err)
return &config, err
}
// fill the type of AuthMethod using the key in configuration
for authName := range config.AuthMethods {
config.AuthMethods[authName].Type = authName
}
return &config, nil
}
// verifyConfig ensure that the provided configuration is valid
func verifyConfig(config *Config) []error {
var errs []error
if len(config.Proxies) < 1 {
errs = append(errs, errors.New("at least one proxy must be provided"))
}
if len(config.Targets) < 1 {
errs = append(errs, errors.New("at least one target must be provided"))
}
return errs
}