forked from moznion/resque_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
63 lines (53 loc) · 1.47 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
package resqueExporter
import (
"fmt"
"os"
"strconv"
)
type Config struct {
GuardIntervalMillis int64 `env:"GUARD_INTERVAL_MILLIS"`
ResqueNamespace string `env:"RESQUE_NAMESPACE"`
Redis *RedisConfig
}
type RedisConfig struct {
Host string `env:"REDIS_HOST"`
Port int `env:"REDIS_PORT"`
Password string `env:"REDIS_PASSWORD"`
DB int64 `env:"REDIS_DB"`
}
func loadConfigFromEnv() (*Config, error) {
var config Config
// Load GuardIntervalMillis
guardIntervalMillisStr := os.Getenv("GUARD_INTERVAL_MILLIS")
if guardIntervalMillisStr != "" {
guardIntervalMillis, err := strconv.ParseInt(guardIntervalMillisStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse GUARD_INTERVAL_MILLIS; err: %s", err)
}
config.GuardIntervalMillis = guardIntervalMillis
}
// Load ResqueNamespace
config.ResqueNamespace = os.Getenv("RESQUE_NAMESPACE")
// Load RedisConfig
config.Redis = &RedisConfig{
Host: os.Getenv("REDIS_HOST"),
Password: os.Getenv("REDIS_PASSWORD"),
}
portStr := os.Getenv("REDIS_PORT")
if portStr != "" {
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, fmt.Errorf("Failed to parse REDIS_PORT; err: %s", err)
}
config.Redis.Port = port
}
dbStr := os.Getenv("REDIS_DB")
if dbStr != "" {
db, err := strconv.ParseInt(dbStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse REDIS_DB; err: %s", err)
}
config.Redis.DB = db
}
return &config, nil
}