-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
107 lines (96 loc) · 3.25 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
package config
import (
"github.com/cristalhq/aconfig"
"github.com/cristalhq/aconfig/aconfigtoml"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/rotisserie/eris"
"github.com/rs/zerolog"
)
// Config describes all configuration options
type Config struct {
Database string `default:"postgres://localhost/nebula" usage:"PostgreSQL DSN to connect to (i.e. postgres://localhost/nebula)"`
Log struct {
Level string `default:"info"`
File string
JSON bool `default:"false" usage:"Output JSONND instead of pretty console messages"`
}
HTTP struct {
Address string `default:"127.0.0.1:8080" usage:"Adress to listen on"`
BaseURL string `default:"http://example.com" usage:"Public URL for this server"`
StaticRoot string `default:"./front" usage:"Path to the directory containing the compiled front package"`
SyncRoot string `default:"./sync" usage:"Path to the directory containing the modsync files"`
}
Argon2 struct {
Memory uint32 `default:"65536"`
Iterations uint32 `default:"3"`
Parallelism uint8 `default:"2"`
SaltLength uint32 `default:"16"`
KeyLength uint32 `default:"32"`
}
Keys struct {
VersionUpdateKey string `usage:"The token used by the CI to update release versions"`
}
Mail struct {
From string `usage:"Mail sender"`
Server string `usage:"SMTP server"`
Port int
Encryption string `default:"STARTTLS" usage:"Transport encryption (STARTTLS, SSL or None)"`
Username string
Password string
Register struct {
Subject string `default:"[FSNebula] Welcome!" usage:"Registration mail subject"`
Text string `usage:"Text template for the registration mail"`
HTML string `usage:"HTML template for the registration mail"`
}
Reset struct {
Subject string `default:"[FSNebula] Password reset" usage:"Password reset subject"`
Text string `usage:"Text template for the password reset mail"`
HTML string `usage:"HTML template for the password reset mail"`
}
}
}
var logLevels = map[string]zerolog.Level{
"debug": zerolog.DebugLevel,
"info": zerolog.InfoLevel,
"warn": zerolog.WarnLevel,
"warning": zerolog.WarnLevel,
"error": zerolog.ErrorLevel,
"fatal": zerolog.FatalLevel,
}
// Loader initializes an empty config object and returns a new Loader for this object
func Loader() (*Config, *aconfig.Loader) {
cfg := Config{}
return &cfg, aconfig.LoaderFor(&cfg, aconfig.Config{
EnvPrefix: "NEBULA",
FlagPrefix: "cfg",
Files: []string{"config.toml"},
FileDecoders: map[string]aconfig.FileDecoder{
".toml": aconfigtoml.New(),
},
})
}
// Validate verifies that all config fields have valid values
func (cfg *Config) Validate() error {
_, err := pgxpool.ParseConfig(cfg.Database)
if err != nil {
return eris.Wrapf(err, `Invalid value for database`)
}
_, ok := logLevels[cfg.Log.Level]
if !ok {
return eris.Errorf(`Invalid value for log.level: %s`, cfg.Log.Level)
}
switch cfg.Mail.Encryption {
case "STARTTLS":
case "SSL":
case "None":
// valid
break
default:
return eris.Errorf(`Invalid value for mail.encryption: %s (must be one of STARTTLS, SSL or None)`, cfg.Mail.Encryption)
}
return nil
}
// LogLevel converts the .Log.Level field to a zerolog.Level
func (cfg *Config) LogLevel() zerolog.Level {
return logLevels[cfg.Log.Level]
}