-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
48 lines (42 loc) · 1.01 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
package api
import (
"context"
"github.com/LampardNguyen234/whale-alert/internal/store"
"github.com/LampardNguyen234/whale-alert/logger"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"os"
"os/signal"
"syscall"
)
type HTTPServer struct {
*gin.Engine
db *store.Store
log logger.Logger
}
func NewHTTPServer(db *store.Store, log logger.Logger) (*HTTPServer, error) {
// initial HTTP setups
engine := gin.New()
engine.Use(gin.Recovery())
engine.Use(gzip.Gzip(gzip.DefaultCompression))
engine.Use(cors.Default())
return &HTTPServer{Engine: engine, db: db, log: log.WithPrefix("HTTPServer")}, nil
}
func (s *HTTPServer) Start(ctx context.Context) {
go s.startHandler()
sysErr := make(chan os.Signal, 1)
signal.Notify(sysErr,
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGHUP,
syscall.SIGQUIT)
select {
case _ = <-ctx.Done():
s.log.Infof("terminating due to ctx.Done")
return
case sig := <-sysErr:
s.log.Infof("terminating got `[%v]` signal", sig)
return
}
}