/
config.go
114 lines (97 loc) · 3.09 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
106
107
108
109
110
111
112
113
114
package config
import (
"github.com/afex/hystrix-go/hystrix"
"github.com/gojek/darkroom/pkg/storage"
"sync"
)
type config struct {
logLevel string
app app
debugMode bool
port int
cacheTime int
source source
enableConcurrentImageProcessing bool
}
var instance *config
var once sync.Once
func getConfig() *config {
once.Do(func() {
instance = newConfig()
})
return instance
}
func newConfig() *config {
v := Viper()
port := v.GetInt("port")
if port == 0 {
port = 3000 // Fallback to default port
}
s := source{
Kind: v.GetString("source.kind"),
HystrixCommand: storage.HystrixCommand{
Name: v.GetString("source.hystrix.commandName"),
Config: hystrix.CommandConfig{
Timeout: v.GetInt("source.hystrix.timeout"),
MaxConcurrentRequests: v.GetInt("source.hystrix.maxConcurrentRequests"),
RequestVolumeThreshold: v.GetInt("source.hystrix.requestVolumeThreshold"),
SleepWindow: v.GetInt("source.hystrix.sleepWindow"),
ErrorPercentThreshold: v.GetInt("source.hystrix.errorPercentThreshold")},
},
PathPrefix: v.GetString("source.pathPrefix"),
}
s.readValue()
return &config{
logLevel: v.GetString("log.level"),
app: app{
name: v.GetString("app.Name"),
version: v.GetString("app.version"),
description: v.GetString("app.description"),
},
debugMode: v.GetBool("debug"),
port: port,
cacheTime: v.GetInt("cache.time"),
source: s,
enableConcurrentImageProcessing: v.GetBool("enableConcurrentImageProcessing"),
}
}
// Update creates a new instance of the configuration and reads all values again
func Update() {
instance = newConfig()
}
// LogLevel returns the log level for logger from the environment
func LogLevel() string {
return getConfig().logLevel
}
// AppName returns the application name from the environment
func AppName() string {
return getConfig().app.name
}
// AppVersion returns the application version from the environment
func AppVersion() string {
return getConfig().app.version
}
// AppDescription returns the application description from the environment
func AppDescription() string {
return getConfig().app.description
}
// DebugModeEnabled returns the debug mode bool from the environment
func DebugModeEnabled() bool {
return getConfig().debugMode
}
// Port returns the application port to be used from the environment
func Port() int {
return getConfig().port
}
// CacheTime returns the time to set the cache-time in image handler from the environment
func CacheTime() int {
return getConfig().cacheTime
}
// Source returns the source struct after it is initialised from the environment values
func Source() *source {
return &getConfig().source
}
// ConcurrentImageProcessingEnabled returns true if we want to process image using multiple cores (checking isOpaque)
func ConcurrentImageProcessingEnabled() bool {
return getConfig().enableConcurrentImageProcessing
}