-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (118 loc) · 3.86 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//+build !test
package main
import (
"encoding/json"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/netsoc/iam/pkg/server"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var srv *server.Server
func init() {
// Config defaults
viper.SetDefault("log_level", log.InfoLevel)
viper.SetDefault("postgresql.host", "db")
viper.SetDefault("postgresql.user", "iamd")
viper.SetDefault("postgresql.password", "hunter2")
viper.SetDefault("postgresql.password_file", "")
viper.SetDefault("postgresql.database", "iamd")
viper.SetDefault("postgresql.timezone", "Europe/Dublin")
viper.SetDefault("postgresql.dsn_extra", "")
viper.SetDefault("postgresql.soft_delete", true)
viper.SetDefault("mail.from", `"Netsoc IAM" <iam@netsoc.ie>`)
viper.SetDefault("mail.reply_to", `"Netsoc Support" <support@netsoc.ie>`)
viper.SetDefault("mail.verify_url", "https://account.netsoc.ie/verify?token={{.Token}}")
viper.SetDefault("mail.reset_url", "https://account.netsoc.ie/reset?token={{.Token}}")
viper.SetDefault("smtp.host", "mail")
viper.SetDefault("smtp.port", 587)
viper.SetDefault("smtp.connect_timeout", 5*time.Second)
viper.SetDefault("smtp.username", "iam@netsoc.ie")
viper.SetDefault("smtp.password", "hunter2")
viper.SetDefault("smtp.password_file", "")
viper.SetDefault("smtp.tls", false)
viper.SetDefault("http.listen_address", ":80")
viper.SetDefault("http.cors.allowed_origins", []string{"*"})
viper.SetDefault("jwt.key", []byte{})
viper.SetDefault("jwt.key_file", "")
viper.SetDefault("jwt.issuer", "iamd")
viper.SetDefault("jwt.login_validity", 365*24*time.Hour)
viper.SetDefault("jwt.email_validity", 24*time.Hour)
viper.SetDefault("root_password", "hunter22")
viper.SetDefault("root_password_file", "")
viper.SetDefault("reserved_usernames", []string{})
viper.SetDefault("cleanup.interval", 2*time.Hour)
viper.SetDefault("cleanup.max_age", 72*time.Hour)
viper.SetDefault("ma1sd.http_address", "")
viper.SetDefault("ma1sd.base_url", "/_ma1sd/backend/api/v1")
viper.SetDefault("ma1sd.domain", "netsoc.ie")
// Config file loading
viper.SetConfigType("yaml")
viper.SetConfigName("iamd")
viper.AddConfigPath("/run/config")
viper.AddConfigPath(".")
// Config from environment
viper.SetEnvPrefix("iamd")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// Config from flags
pflag.StringP("log_level", "l", "info", "log level")
pflag.Parse()
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
log.WithError(err).Fatal("Failed to bind pflags to config")
}
if err := viper.ReadInConfig(); err != nil {
log.WithError(err).Warn("Failed to read config")
}
}
func reload() {
if srv != nil {
stop()
srv = nil
}
var config server.Config
if err := viper.Unmarshal(&config, server.ConfigDecoderOptions); err != nil {
log.WithField("err", err).Fatal("Failed to parse configuration")
}
if err := config.ReadSecrets(); err != nil {
log.WithError(err).Fatal("Failed to read config secrets from files")
}
log.SetLevel(config.LogLevel)
cJSON, err := json.Marshal(config)
if err != nil {
log.WithError(err).Fatal("Failed to encode config as JSON")
}
log.WithField("config", string(cJSON)).Debug("Got config")
srv, err = server.NewServer(config)
if err != nil {
log.WithError(err).Fatal("Failed to create server")
}
log.Info("Starting server")
go func() {
if err := srv.Start(); err != nil {
log.WithError(err).Fatal("Failed to start server")
}
}()
}
func stop() {
if err := srv.Stop(); err != nil {
log.WithError(err).Fatal("Failed to stop iamd server")
}
}
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
viper.OnConfigChange(func(e fsnotify.Event) {
log.WithField("file", e.Name).Info("Config changed, reloading")
reload()
})
viper.WatchConfig()
reload()
<-sigs
stop()
}