-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
68 lines (49 loc) · 1.1 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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"time"
)
var configfile = flag.String(
"configfile",
"./config.yml",
"YAML config file",
)
func main() {
log.Println("[*] Starting")
flag.Parse()
srv := CreateServer(*configfile)
go func() {
log.Printf("[x] Stop server due to %s\n", srv.ListenAndServe())
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("[*] Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("[*] Server Shutdown:", err)
}
log.Println("[*] Server exiting")
}
func CreateServer(configfilePath string) *http.Server {
config, err := LoadConfigYAML(configfilePath)
if err != nil {
log.Fatalf("[*] Can not load config file %s\n", configfilePath)
}
serviceLocator, err := BuildServiceLocator(config)
if err != nil {
log.Fatal("[*] Can not load services")
}
router := CreateRouter(serviceLocator)
srv := &http.Server{
Addr: config.ServerAddr,
Handler: router,
}
return srv
}