-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (103 loc) · 2.81 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
package main
import (
"math/rand"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
logger "log"
log "github.com/Sirupsen/logrus"
nullformat "github.com/axiomzen/null/format"
"github.com/axiomzen/zenauth/config"
"github.com/axiomzen/zenauth/constants"
"github.com/axiomzen/zenauth/data"
"github.com/axiomzen/zenauth/grpc"
pg "gopkg.in/pg.v4"
"gopkg.in/tylerb/graceful.v1"
)
func main() {
log.Infoln(os.Getenv("ZENAUTH_ENVIRONMENT"))
// set just in case someone has go 1.4
runtime.GOMAXPROCS(runtime.NumCPU())
nullformat.SetTimeFormat(constants.TimeFormat)
log.SetFormatter(&log.TextFormatter{})
// apparently this is the default now (we really should fork this repo)
//uuid.SwitchFormat(uuid.CleanHyphen)
conf, err := config.Get()
if err != nil {
// die, we are not configured properly
log.Fatal(err.Error())
}
log.Infoln("Migrating DB ...")
switch conf.Environment {
case constants.EnvironmentStaging, constants.EnvironmentProduction, constants.EnvironmentDevelopment:
data.Migrate(conf)
log.SetFormatter(&log.JSONFormatter{})
fallthrough
case constants.EnvironmentTest:
// set seed
rand.Seed(time.Now().UTC().UnixNano())
default:
}
// set query logger
if conf.LogQueries {
pg.SetQueryLogger(logger.New(os.Stdout, "", logger.LstdFlags))
}
// set logging level
log.Infoln("Log Level: " + conf.LogLevel)
switch strings.ToLower(conf.LogLevel) {
default:
fallthrough
case log.DebugLevel.String():
log.SetLevel(log.DebugLevel)
case log.InfoLevel.String():
log.SetLevel(log.InfoLevel)
case log.WarnLevel.String():
log.SetLevel(log.WarnLevel)
case log.ErrorLevel.String():
log.SetLevel(log.ErrorLevel)
case log.FatalLevel.String():
log.SetLevel(log.FatalLevel)
case log.PanicLevel.String():
log.SetLevel(log.PanicLevel)
}
log.Infoln("Connecting to DB ...")
// database
dataP, dataErr := data.Get(conf)
if dataErr != nil {
// die, we can't connect to the database
log.Fatal(dataErr.Error())
}
// make sure to close the database connection pool when we exit
defer dataP.Close()
// Error channel for multiple servers
errChn := make(chan error)
router := InitRouter(conf)
srv := &graceful.Server{
// Time to allow for active requests to complete
Timeout: conf.DrainAndDieTimeout,
Server: &http.Server{
Addr: ":" + strconv.FormatInt(int64(conf.Port), 10),
Handler: router,
ReadTimeout: conf.TransportReadTimeout,
WriteTimeout: conf.TransportWriteTimeout,
//MaxHeaderBytes: 1 << 20,
},
}
log.Infoln("Starting Server on Port " + strconv.FormatInt(int64(conf.Port), 10))
go func() {
errChn <- srv.ListenAndServe()
}()
// Runs the GRPC server
grpcServer := grpc.Server{
Config: conf,
DAL: dataP,
Log: log.WithField("server", "grpc"),
}
go func() {
errChn <- grpcServer.ListenAndServe()
}()
log.Fatal(<-errChn)
}