-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
70 lines (52 loc) · 1.31 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
package main
import (
"net/http"
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var log *logrus.Logger
func main() {
config, err := readInConfig("local", "./conf")
if err != nil {
panic(err)
}
log, err = createLogger()
if err != nil {
log.Errorln(err)
}
router := createRouter(config)
server := NewServer(":"+config.GetString("server.port"), router, []Middleware{
PanicLogging,
})
log.Infof("Your service is listening on port %s", config.GetString("server.port"))
panic(server.ListenAndServe())
}
func readInConfig(env string, paths ...string) (*viper.Viper, error) {
if env == "" {
env = "local"
}
config := viper.New()
for i := range paths {
config.AddConfigPath(paths[i])
}
config.SetConfigName(env)
if err := config.ReadInConfig(); err != nil {
return nil, fmt.Errorf("configuration error: %s", err)
}
return config, nil
}
func createLogger() (*logrus.Logger, error) {
logger := logrus.New()
logger.Level = logrus.DebugLevel
return logger, nil
}
// Creates the standard root logger that handles traffic to the server
func createRouter(config *viper.Viper) http.Handler {
router := http.NewServeMux()
router.Handle("/", &IndexHandler{config: config})
router.Handle("/status/", &StatusHandler{
healthChecks: createHealthChecksHandler(config),
})
return router
}