-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartAPIServer.go
73 lines (56 loc) · 1.5 KB
/
StartAPIServer.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
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"github.com/DataManager-Go/DataManagerServer/services"
"gorm.io/gorm"
log "github.com/sirupsen/logrus"
)
// Services
var (
apiService *services.APIService // Handle endpoints
cleanupService *services.CleanupService // Cleanup db stuff
)
func startAPI() {
log.Info("Starting version " + version)
// Create and start required services
apiService = services.NewAPIService(config, db)
apiService.Start()
cleanupService = services.NewClienupService(config, db)
cleanupService.Start()
if config.Webserver.Profiling {
log.Info("Starting in profiling mode")
}
// Startup done
log.Info("Startup completed")
awaitExit(apiService, db)
}
// Shutdown server gracefully
func awaitExit(httpServer *services.APIService, db *gorm.DB) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, os.Interrupt, syscall.SIGKILL, syscall.SIGTERM)
// await os signal
<-signalChan
// Create a deadline for the await
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
log.Info("Shutting down server")
if httpServer.HTTPServer != nil {
err := httpServer.HTTPServer.Shutdown(ctx)
if err != nil {
log.Warn(err)
}
log.Info("HTTP server shutdown complete")
}
if httpServer.HTTPTLSServer != nil {
err := httpServer.HTTPTLSServer.Shutdown(ctx)
if err != nil {
log.Warn(err)
}
log.Info("HTTPs server shutdown complete")
}
log.Info("Shutting down complete")
}