-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
55 lines (50 loc) · 1.14 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
package main
import (
"os"
"github.com/kelseyhightower/envconfig"
"github.com/naoina/toml"
"github.com/putdotio/pas/internal/event"
"github.com/putdotio/pas/internal/property"
)
// Config for application
type Config struct {
// Listen address for HTTP server.
ListenAddress string
// Give some time to unfinished HTTP requests before shutting down the server (milliseconds).
ShutdownTimeout uint
// MySQL database DSN.
MySQLDSN string
// Secret for signing user IDs.
Secret string
// Corresponds to the schema of user table.
User property.Types
// Corresponds to the schema of event tables.
Events map[event.Name]property.Types
}
func NewConfig() (*Config, error) {
c := new(Config)
f, err := os.Open(*configPath)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
dec := toml.NewDecoder(f)
err = dec.Decode(&c)
if err != nil {
return nil, err
}
err = envconfig.Process("PAS", c)
if err != nil {
return nil, err
}
c.setDefaults()
return c, nil
}
func (c *Config) setDefaults() {
if c.ListenAddress == "" {
c.ListenAddress = "127.0.0.1:8080"
}
if c.ShutdownTimeout == 0 {
c.ShutdownTimeout = 5000
}
}