forked from cenkalti/dalga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
84 lines (70 loc) · 1.39 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
package dalga
import (
"fmt"
"github.com/cenkalti/dalga/dalga/Godeps/_workspace/src/code.google.com/p/gcfg"
)
var DefaultConfig = Config{
MySQL: mysqlConfig{
Host: "127.0.0.1",
Port: 3306,
DB: "test",
Table: "dalga",
User: "root",
Password: "",
},
Listen: listenConfig{
Host: "127.0.0.1",
Port: 34006,
},
Endpoint: endpointConfig{
BaseURL: "http://127.0.0.1:5000/",
Timeout: 10,
},
}
type Config struct {
Jobs jobsConfig
MySQL mysqlConfig
Listen listenConfig
Endpoint endpointConfig
Redis redisConfig
}
func (c *Config) LoadFromFile(filename string) error {
return gcfg.ReadFileInto(c, filename)
}
type jobsConfig struct {
RandomizationFactor float64
}
type mysqlConfig struct {
Host string
Port int
DB string
Table string
User string
Password string
}
func (c mysqlConfig) DSN() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", c.User, c.Password, c.Host, c.Port, c.DB)
}
type listenConfig struct {
Host string
Port int
}
func (c listenConfig) Addr() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
type endpointConfig struct {
BaseURL string
Timeout int
}
type redisConfig struct {
Host string
Port int
DB int
}
func (c redisConfig) Zero() bool {
zero := redisConfig{}
return c == zero
}
func (c redisConfig) Addr() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}