-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
93 lines (80 loc) · 2.01 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
package main
import (
"net/url"
"os"
"strings"
"github.com/rs/zerolog"
)
type config struct {
aasaamWebServer bool
tokenSecret string
clientSecret string
baseURL string
cdnStatic bool
staticURL string
restCaptchaURL string
defaultLanguage string
supportedLanguages []string
localePath string
logger *zerolog.Logger
}
func newConfig(
logLevel string,
aasaamWebServer bool,
defaultLanguage string,
supportedLanguages string,
tokenSecret string,
clientSecret string,
baseURL string,
staticURL string,
restCaptchaURL string,
localePath string,
) *config {
c := config{
aasaamWebServer: aasaamWebServer,
tokenSecret: tokenSecret,
clientSecret: clientSecret,
baseURL: strings.TrimRight(baseURL, "/"),
staticURL: "",
cdnStatic: false,
restCaptchaURL: strings.TrimRight(restCaptchaURL, "/"),
defaultLanguage: "en",
supportedLanguages: []string{"en"},
localePath: localePath,
}
// logger config
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.WarnLevel)
logConfigLevel, errLogLevel := zerolog.ParseLevel(logLevel)
if errLogLevel == nil {
zerolog.SetGlobalLevel(logConfigLevel)
}
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
c.logger = &logger
_, err := url.ParseRequestURI(staticURL)
if err == nil {
c.staticURL = staticURL
c.cdnStatic = true
} else {
c.staticURL = c.baseURL + "/challenge/static"
}
if supportedLanguages != "" {
langs := strings.Split(strings.TrimSpace(supportedLanguages), ",")
c.supportedLanguages = []string{}
for _, l := range langs {
if isSupportedLanguage(l) {
c.supportedLanguages = append(c.supportedLanguages, l)
}
}
}
if isSupportedLanguage(defaultLanguage) {
c.defaultLanguage = defaultLanguage
}
if len(c.supportedLanguages) == 0 {
c.supportedLanguages = []string{c.defaultLanguage}
}
return &c
}
func (c *config) getLogger() *zerolog.Logger {
return c.logger
}