-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
105 lines (91 loc) · 2.27 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
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"os"
"strconv"
"time"
)
type Config struct {
Secret string
Access string
SQSURL string
Region string
ConnectionTimeout time.Duration
WaitTime int64
PostEndpoint string
PostHost string
MaxSleep time.Duration
ContentType string
Connections int64
}
// default values
const (
defaultContentType = "application/json"
defaultRegion = "us-east-1"
defaultSleep = time.Duration(300) * time.Second
defaultConnectionTimeout = time.Duration(300) * time.Second
defaultConnections = 1
defaultWaitTime = 20
)
// Convert an Env var into an int64 or thrown error
func getEnvVarAsNum(envVar string) (int64, error) {
val := os.Getenv(envVar)
if val == "" {
return 0, fmt.Errorf("The ENV variable, %s, was not found", envVar)
}
num, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, err
}
return num, nil
}
func initConfig() {
cfg.Access = os.Getenv("ACCESS")
cfg.Secret = os.Getenv("SECRET")
cfg.SQSURL = os.Getenv("SQS_URL")
if cfg.SQSURL == "" {
fmt.Println("Error SQS_URL not found.")
os.Exit(1)
}
cfg.PostEndpoint = os.Getenv("POST_ENDPOINT")
if cfg.PostEndpoint == "" {
fmt.Println("Error: POST_ENDPOINT not found.")
os.Exit(1)
}
cfg.PostHost = os.Getenv("POST_HOST")
if cfg.PostHost == "" {
cfg.PostHost = "http://127.0.0.1:80"
}
cfg.Region = os.Getenv("REGION")
if cfg.Region == "" {
cfg.Region = defaultRegion
}
cfg.ContentType = os.Getenv("CONTENT_TYPE")
if cfg.ContentType == "" {
cfg.ContentType = defaultContentType
}
waitTime, err := getEnvVarAsNum("WAIT_TIME")
if err != nil {
cfg.WaitTime = defaultWaitTime
} else {
cfg.WaitTime = waitTime
}
sleep, err := getEnvVarAsNum("MAX_SLEEP")
if err != nil {
cfg.MaxSleep = defaultSleep
} else {
cfg.MaxSleep = time.Duration(sleep) * time.Second
}
connectionTimeout, err := getEnvVarAsNum("CONNECTION_TIMEOUT")
if err != nil {
cfg.ConnectionTimeout = defaultConnectionTimeout
} else {
cfg.ConnectionTimeout = time.Duration(connectionTimeout) * time.Second
}
connections, err := getEnvVarAsNum("CONNECTIONS")
if err != nil {
cfg.Connections = defaultConnections
} else {
cfg.Connections = connections
}
}