-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
118 lines (97 loc) · 3.23 KB
/
app.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
package app
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/niharrathod/ruleengine/app/config"
"github.com/niharrathod/ruleengine/app/controlplane"
"github.com/niharrathod/ruleengine/app/ext/datastore"
"github.com/niharrathod/ruleengine/app/handler"
"github.com/niharrathod/ruleengine/app/log"
"go.uber.org/zap"
ginzap "github.com/gin-contrib/zap"
)
type appServer struct {
httpserver *http.Server
}
func New() *appServer {
appServer := appServer{}
appServer.init()
return &appServer
}
// initialization activities
// Incase of init activity failure, log the error and exit (os.Exist(1))
func (app *appServer) init() {
config.Initialize()
log.Initialize()
datastore.Initialize()
}
func (app *appServer) Run() {
if config.IsProduction() {
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
router.Use(ginzap.Ginzap(log.Logger, time.RFC3339, true))
router.Use(ginzap.RecoveryWithZap(log.Logger, true))
rest := router.Group("health")
rest.GET("/check/", handler.HealthCheck())
reApi := router.Group("/api")
reApi.GET("/ruleengines/:ruleengine/", controlplane.GetRuleEngine())
reApi.POST("/ruleengines/:ruleengine/tags/:tag", controlplane.CreateRuleEngine())
reApi.DELETE("/ruleengines/:ruleengine", controlplane.DeleteRuleEngine())
reApi.DELETE("/ruleengines/:ruleengine/tags/:tag", controlplane.DeleteRuleEngineConfig())
reApi.PATCH("/ruleengines/:ruleengine/tags/:tag/setdefault", controlplane.SetDefaultTag())
reApi.PATCH("/ruleengines/:ruleengine/removedefault", controlplane.RemoveDefaultTag())
reApi.PATCH("/ruleengines/:ruleengine/tags/:tag/enable", controlplane.EnableRuleEngine())
reApi.PATCH("/ruleengines/:ruleengine/tags/:tag/disable", controlplane.DisableRuleEngine())
app.httpserver = &http.Server{
Addr: config.Server.Http.BindIp + ":" + strconv.Itoa(config.Server.Http.BindPort),
Handler: router,
}
go app.startServer()
app.prepAndWaitForShutDown()
}
func (app *appServer) startServer() {
log.Logger.Info("Application is starting on " + config.Server.Http.BindIp + ":" + strconv.Itoa(config.Server.Http.BindPort))
err := app.httpserver.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Logger.Panic("http server listen failed : " + err.Error())
}
}
func (app *appServer) prepAndWaitForShutDown() {
quitSignal := make(chan os.Signal, 1)
signal.Notify(quitSignal, syscall.SIGINT, syscall.SIGTERM)
<-quitSignal
app.shutdown()
}
/*
To tear down the app. Order of tear down activities is important
1. http listener - to stop incoming traffic
2. close datastore connection
# Add more activities here
log sync should be last activity
*/
func (app *appServer) shutdown() {
shutdownContext, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
log.Logger.Info("Application is shutting down")
// stop http listener
if err := app.httpserver.Shutdown(shutdownContext); err != nil {
log.Logger.Error("Server Shutdown failed:", zap.String("error", err.Error()))
}
// close datastore connection
datastore.Close(shutdownContext)
// sync logs
err := log.Logger.Sync()
if err != nil {
// logger sync failed, so back to basics :(
fmt.Println(err.Error())
}
}