-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
67 lines (51 loc) · 1.11 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
package geoipfix
import (
"os"
"os/signal"
"time"
"github.com/go-chi/valve"
)
// Run launchs the server with a config path.
func Run(config string) error {
app, err := newApplication(config)
if err != nil {
return err
}
defer app.Logger.Sync()
services := []service{}
if app.Config.Server.HTTP != nil {
httpServer := newHTTPServer(*app.Config.Server.HTTP,
withLogger(app.Logger),
withDebug(app.Config.Debug),
withDB(app.DB))
services = append(services, httpServer)
}
if app.Config.Server.RPC != nil {
rpcServer := newRPCServer(*app.Config.Server.RPC,
withLogger(app.Logger),
withDB(app.DB))
services = append(services, rpcServer)
}
valv := valve.New()
baseCtx := valv.Context()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for _, service := range services {
err = service.Init()
if err != nil {
return err
}
go service.Serve(baseCtx)
}
<-c
app.Logger.Info("Shutting down servers..")
// first valv
valv.Shutdown(20 * time.Second)
for _, service := range services {
err = service.Shutdown()
if err != nil {
return nil
}
}
return nil
}