-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (72 loc) · 2.25 KB
/
main.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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/mrehanabbasi/company-inc/config"
"github.com/mrehanabbasi/company-inc/database"
"github.com/mrehanabbasi/company-inc/logger"
log "github.com/mrehanabbasi/company-inc/logger"
"github.com/mrehanabbasi/company-inc/models"
"github.com/mrehanabbasi/company-inc/msq"
"github.com/mrehanabbasi/company-inc/routes"
"github.com/spf13/viper"
)
func main() {
// Initializing logger
logger.TextLogInit()
models.InitCustomValidation()
ctx := context.Background()
// Initializing database
dbClient := database.InitDB(ctx)
_ = dbClient.InitIndices()
defer func() {
_ = dbClient.Conn.Disconnect(ctx)
}()
// Initializing kafka writer
kConn := msq.InitKafka()
defer func() {
_ = kConn.Conn.Close()
}()
// Register all the routes
server := routes.NewRouter(dbClient, kConn)
// The package "endless" does not work in a Windows environment
// _ = endless.ListenAndServe(viper.GetString(config.ServerHost)+":"+viper.GetString(config.ServerPort), server)
// In case graceful shutdown is not required, server.Run() can be used
// _ = server.Run(viper.GetString(config.ServerHost) + ":" + viper.GetString(config.ServerPort))
srv := &http.Server{
Addr: viper.GetString(config.ServerHost) + ":" + viper.GetString(config.ServerPort),
Handler: server,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Panic("server listening error: ", err)
panic(err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Info("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Panic("server shutdown with error: ", err)
panic(err)
}
// catching ctx.Done(). timeout of 5 seconds.
select {
case <-ctx.Done():
log.Info("waited 5 seconds for the server to close")
}
log.Info("Exiting server")
}