-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
74 lines (58 loc) · 1.6 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
package config
import (
"fmt"
"os"
"sync"
"time"
"github.com/spf13/viper"
"go.uber.org/zap"
)
const (
defaultClientTimeout = 2 * time.Second
defaultHTTPPort = 9747
defaultRetryCount = 3
)
var configMutex = &sync.Mutex{}
// InitConfig initializes a config and configure viper to receive config from file and environment.
func InitConfig() (*zap.Logger, error) {
log, err := zap.NewProduction()
if err != nil {
log.Fatal("Unable to create logger", zap.Error(err))
}
// Find home directory.
home, err := os.UserHomeDir()
if err != nil {
return log, err
}
viper.AddConfigPath(home)
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName(".statuspage-exporter")
viper.AutomaticEnv() // read in environment variables that match
// If a config file found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Info(fmt.Sprintf("Using config file: %s", viper.ConfigFileUsed()))
}
return log, nil
}
// HTTPPort returns a port for http server.
func HTTPPort() int {
viper.SetDefault("http_port", defaultHTTPPort)
return viper.GetInt("http_port")
}
// ClientTimeout returns a timeout for http client.
func ClientTimeout() time.Duration {
configMutex.Lock()
viper.SetDefault("client_timeout", defaultClientTimeout)
value := viper.GetDuration("client_timeout")
configMutex.Unlock()
return value
}
// RetryCount returns amount of retries for http client.
func RetryCount() int {
configMutex.Lock()
viper.SetDefault("retry_count", defaultRetryCount)
value := viper.GetInt("retry_count")
configMutex.Unlock()
return value
}