-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.go
133 lines (108 loc) · 3.21 KB
/
Server.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
/*
* Copyright (c) 2021. D-Haven.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"github.com/D-Haven/fact-totem/version"
"github.com/D-Haven/fact-totem/webapi"
"github.com/heptiolabs/healthcheck"
"golang.org/x/net/context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
const appName = "Fact-Totem"
func main() {
err := ShowLogo(log.Writer())
if err != nil {
log.Fatalf("Error printing logo: %s", err)
}
err = version.Print(log.Writer())
if err != nil {
log.Fatalf("Error printing version: %s", err)
}
hostname, err := os.Hostname()
if err != nil {
// Don't kill the app, but report the error
hostname = "--hostname lookup error: " + err.Error()
}
log.Printf("Server host name: %s", hostname)
log.Printf("Configuring %s...", appName)
config, err := GetValidatedConfig("./config.yaml")
if err != nil {
log.Fatal(err)
}
server, err := configureServer(config)
if err != nil {
log.Fatal(err)
}
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
go func() {
useTLS := len(config.Server.TLS.CertFile) != 0 && len(config.Server.TLS.KeyFile) != 0
if useTLS {
err = server.ListenAndServeTLS(config.Server.TLS.CertFile, config.Server.TLS.KeyFile)
} else {
err = server.ListenAndServe()
}
if err != nil {
log.Printf("Error running service: %s", err)
}
}()
log.Printf("%s started, listening on %s", appName, server.Addr)
log.Print("K8s Readiness Check: /ready")
log.Print("K8s Liveness Check: /live")
killSignal := <-interrupt
switch killSignal {
case os.Interrupt:
log.Print("Received OS Interrupt")
case syscall.SIGTERM:
log.Print("Received Termination Signal")
}
log.Printf("%s is shutting down...", appName)
if err = server.Shutdown(context.Background()); err != nil {
log.Printf("Shutdown error: %s", err)
}
log.Print("...Shutdown complete")
}
func configureServer(config *Config) (*http.Server, error) {
health := healthcheck.NewHandler()
health.AddLivenessCheck("go-routinethreshold", healthcheck.GoroutineCountCheck(100))
multiplexHandler := http.NewServeMux()
multiplexHandler.Handle("/ready", health)
multiplexHandler.Handle("/live", health)
projectApi, err := webapi.NewApi(config.EventStore.Path, config.EventStore.EncryptionKey, config.EventStore.KeyDuration)
if err != nil {
return nil, err
}
authHandler := webapi.AuthHandler{
Handler: projectApi.Handle,
UserConfig: config.Permissions,
}
multiplexHandler.Handle("/", &authHandler)
server := &http.Server{
Addr: config.Server.Host + ":" + config.Server.Port,
Handler: multiplexHandler,
}
server.RegisterOnShutdown(func() {
err := projectApi.Close()
if err != nil {
log.Fatal(err)
}
})
return server, nil
}