-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (56 loc) · 1.49 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
package main
import (
"net/http"
"time"
"github.com/Sirupsen/logrus"
"github.com/spf13/viper"
"github.com/tylerb/graceful"
"github.com/claisne/snippetdb/application"
)
func newConfig() (*viper.Viper, error) {
c := viper.New()
c.SetDefault("database_driver", "postgres")
c.SetDefault("dsn", "postgres://postgres:postgres@localhost:5432/snippetdb?sslmode=disable")
c.SetDefault("cookie_secret", "uwlUl3JoVtz4fpDM")
c.SetDefault("http_addr", ":8888")
c.SetDefault("http_cert_file", "")
c.SetDefault("http_key_file", "")
c.SetDefault("http_drain_interval", "1s")
c.AutomaticEnv()
return c, nil
}
func main() {
config, err := newConfig()
if err != nil {
logrus.Fatal(err)
}
app, err := application.New(config)
if err != nil {
logrus.Fatal(err)
}
middle, err := app.MiddlewareStruct()
if err != nil {
logrus.Fatal(err)
}
serverAddress := config.Get("http_addr").(string)
certFile := config.Get("http_cert_file").(string)
keyFile := config.Get("http_key_file").(string)
drainIntervalString := config.Get("http_drain_interval").(string)
drainInterval, err := time.ParseDuration(drainIntervalString)
if err != nil {
logrus.Fatal(err)
}
srv := &graceful.Server{
Timeout: drainInterval,
Server: &http.Server{Addr: serverAddress, Handler: middle},
}
logrus.Infoln("Running HTTP server on " + serverAddress)
if certFile != "" && keyFile != "" {
err = srv.ListenAndServeTLS(certFile, keyFile)
} else {
err = srv.ListenAndServe()
}
if err != nil {
logrus.Fatal(err)
}
}