forked from trustwallet/watchmarket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
executable file
·56 lines (49 loc) · 1.15 KB
/
base.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
package internal
import (
"context"
"flag"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func GetConfigPath(defaultConfigPath string) string {
var confPath string
flag.StringVar(&confPath, "c", defaultConfigPath, "config file for api")
flag.Parse()
return confPath
}
func SetupGracefulShutdown(port string, engine *gin.Engine) {
server := &http.Server{
Addr: ":" + port,
Handler: engine,
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
defer func() {
if err := server.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown: ", err)
}
}()
go func() {
if err := server.ListenAndServe(); err != nil {
log.Fatal("Application failed", err)
}
}()
log.WithFields(log.Fields{"bind": port}).Info("Running application")
waitingForExitSignal()
log.Info("Waiting for all jobs to stop")
}
func waitingForExitSignal() {
signalForExit := make(chan os.Signal, 1)
signal.Notify(signalForExit,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
stop := <-signalForExit
log.Info("Stop signal Received", stop)
}