-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
88 lines (73 loc) · 2.02 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
package main
import (
"os"
"strings"
)
// Config represents central configuration of this app. Should only be used in
// the app entrypoint and not handed down to individual components / functions.
//
// To instantiate a Config use the NewConfig function.
type Config struct {
// Core configuration.
serverPort string
// Token extraction.
tokenHeaderNames []string
addTokenHeaderNames []string
fallbackToken string
// User interface.
uiTarget string
uiTitle string
uiDesc1 string
uiDesc2 string
uiMisc string
}
// NewConfig inits config struct. Values are retrieved from environments
// variables. Includes internal defaults.
func NewConfig() Config {
c := Config{}
// Core configuration.
c.serverPort = GetEnv("SERVER_PORT", "8080")
// Token extraction.
c.tokenHeaderNames = SplitToSlice(GetEnv("TOKEN_HEADER_NAMES",
strings.Join([]string{
"Access-Token",
"Authorization",
"Token",
"X-Auth-Request-Access-Token",
"X-Forwarded-Access-Token",
}, ","),
))
c.addTokenHeaderNames = SplitToSlice(GetEnv("ADD_TOKEN_HEADER_NAMES", ""))
c.fallbackToken = GetEnv("FALLBACK_TOKEN", "")
// User interface.
c.uiTarget = GetEnv("UI_TARGET", "")
c.uiTitle = GetEnv("UI_TITLE", "")
c.uiDesc1 = GetEnv("UI_DESC1", "")
c.uiDesc2 = GetEnv("UI_DESC2", "")
c.uiMisc = GetEnv("UI_MISC", "")
return c
}
// GetEnv gets environment variable value after prefixing the key. Default value
// in case of absence must be provided.
//
// Used for retrieving configuration provided by environment variables and
// enforcing a common prefix.
func GetEnv(key, def string) string {
v := os.Getenv("T2G_" + key)
if v == "" {
return def
}
return v
}
// SplitToSlice splits string by commas into a slice. Resulting items are space
// trimmed. Empty string items are removed. Finally, the slice is returned.
func SplitToSlice(str string) []string {
var r []string
for _, e := range strings.Split(str, ",") {
trimmed := strings.TrimSpace(e)
if len(trimmed) > 0 {
r = append(r, strings.TrimSpace(e))
}
}
return r
}