-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
54 lines (45 loc) · 1007 Bytes
/
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
package config
import (
"golang.org/x/exp/slog"
)
type HTTP struct {
Host string
Port int
}
type Database struct {
DSN string
}
type Platform struct {
SitesDirectory string
MaxUploadSizeMegabytes int
}
type Config struct {
Debug bool
HTTP *HTTP
Database *Database
Platform *Platform
}
func Load() (*Config, error) {
cl := new(configLoader)
if err := cl.load("config.yml"); err != nil {
return nil, err
}
conf := &Config{
Debug: asBool(cl.withDefault("debug", false)),
HTTP: &HTTP{
Host: asString(cl.withDefault("http.host", "127.0.0.1")),
Port: asInt(cl.withDefault("http.port", 8080)),
},
Database: &Database{
DSN: asString(cl.withDefault("database.dsn", "website.db")),
},
Platform: &Platform{
SitesDirectory: asString(cl.required("platform.sitesDirectory")),
MaxUploadSizeMegabytes: asInt(cl.withDefault("platform.maxUploadSizeMegabytes", 512)),
},
}
if conf.Debug {
slog.Debug("debug mode enabled")
}
return conf, nil
}