This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconfig.go
88 lines (70 loc) · 1.92 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 config
import (
"fmt"
"os"
"strconv"
"strings"
"whapp-irc/maps"
"whapp-irc/whapp"
)
// Config contains all the possible configuration options and their values
type Config struct {
FileServerHost string
FileServerPort string
FileServerHTTPS bool
IRCPort string
LogLevel whapp.LoggingLevel
MapProvider maps.Provider
AlternativeReplay bool
}
func getEnvDefault(env, def string) string {
res := os.Getenv(env)
if res == "" {
return def
}
return res
}
// ReadEnvVars reads environment variables and returns a Config instance
// containing the parsed values, or an error.
func ReadEnvVars() (Config, error) {
host := getEnvDefault("HOST", "localhost")
fileServerPort := getEnvDefault("FILE_SERVER_PORT", "3000")
fileServerUseHTTPS := getEnvDefault("FILE_SERVER_HTTPS", "false")
ircPort := getEnvDefault("IRC_SERVER_PORT", "6060")
logLevelRaw := getEnvDefault("LOG_LEVEL", "normal")
mapProviderRaw := getEnvDefault("MAP_PROVIDER", "google-maps")
replayMode := getEnvDefault("REPLAY_MODE", "normal")
useHTTPS, err := strconv.ParseBool(fileServerUseHTTPS)
if err != nil {
return Config{}, err
}
var logLevel whapp.LoggingLevel
switch strings.ToLower(logLevelRaw) {
case "verbose":
logLevel = whapp.LogLevelVerbose
case "normal":
logLevel = whapp.LogLevelNormal
default:
err := fmt.Errorf("no log level %s found", logLevelRaw)
return Config{}, err
}
var mapProvider maps.Provider
switch strings.ToLower(mapProviderRaw) {
case "openstreetmap", "open-street-map":
mapProvider = maps.OpenStreetMap
case "googlemaps", "google-maps":
mapProvider = maps.GoogleMaps
default:
err := fmt.Errorf("no map provider %s found", mapProviderRaw)
return Config{}, err
}
return Config{
FileServerHost: host,
FileServerPort: fileServerPort,
FileServerHTTPS: useHTTPS,
IRCPort: ircPort,
LogLevel: logLevel,
MapProvider: mapProvider,
AlternativeReplay: replayMode == "alternative",
}, nil
}