forked from bestwpw/kolide
/
config.go
93 lines (80 loc) · 1.77 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package config
import (
"fmt"
"time"
"github.com/BurntSushi/toml"
"github.com/mephux/common"
)
// Duration type
type Duration struct {
time.Duration
}
// UnmarshalText custom duration type
func (d *Duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
// Config for database, session and other information
type Config struct {
Session *session
Database *database
Server *server
}
type session struct {
Key string
EncryptionKey string `toml:"encryption_key"`
Size int
Network string
Address string
Password string
}
type database struct {
Address string
Username string
Password string
Database string
SSL string
Crt string
Key string
}
type server struct {
QueryTimeout Duration `toml:"query_timeout"`
Production bool
Debug bool
Address string
Crt string
Key string
EnrollSecret string `toml:"enroll_secret"`
}
// Default will return a default configuration
func Default(debug, production bool) *Config {
return &Config{
Session: &session{
Size: 10,
Network: "tcp",
Address: ":6379",
Password: "",
Key: string(common.RandomCreateBytes(50)),
},
Database: &database{},
Server: &server{
// QueryTimeout: duration("30s").Duration,
Production: production,
Debug: debug,
Address: "127.0.0.1:8000",
Crt: "",
Key: "",
EnrollSecret: "kolidedev",
},
}
}
// Load a given configuration by path
func Load(configPath string) (*Config, error) {
var config Config
_, err := toml.DecodeFile(configPath, &config)
if err != nil {
return nil, fmt.Errorf("error loading configuration file - %s", err.Error())
}
return &config, nil
}