-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (81 loc) · 2 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
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/cjtim/be-friends-api/configs"
"github.com/cjtim/be-friends-api/handlers"
"github.com/cjtim/be-friends-api/handlers/middlewares"
"github.com/cjtim/be-friends-api/repository"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/swagger"
"go.uber.org/zap"
_ "github.com/cjtim/be-friends-api/docs"
)
func main() {
os.Exit(realMain())
}
func realMain() int {
err := configs.InitConfig()
if err != nil {
log.Fatalln(err)
}
// Initial Global logger
logger := middlewares.InitZap()
defer logger.Sync()
zap.ReplaceGlobals(logger)
// Connect DB
closeFn, err := repository.PrepareDB()
defer closeFn() // Close all DB conn
if err != nil {
zap.L().Error("prepare db error", zap.Error(err))
return 1
}
// Prepare API route
app := prepareFiber()
setupCloseHandler(app)
// Start accept connection
listen := fmt.Sprintf(":%d", configs.Config.Port)
err = app.Listen(listen)
if err != nil {
zap.L().Error("fiber error", zap.Error(err))
return 1
}
return 0
}
// @title Be Friends API
// @version 1.0
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @BasePath /
func prepareFiber() *fiber.App {
app := fiber.New(fiber.Config{
ErrorHandler: middlewares.ErrorHandling,
BodyLimit: 100 * 1024 * 1024, // Limit file size to 100MB
})
app.Get("/api/swagger/*", swagger.HandlerDefault)
app.Get("/api/swagger/*", swagger.New(swagger.Config{
URL: "/api/doc.json",
DeepLinking: true,
DocExpansion: "none",
}))
app.Use(middlewares.Cors())
app.Use(middlewares.RequestLog())
app.Use(middlewares.NoCache())
handlers.Route(app) // setup router path
return app
}
// setupCloseHandler - What to do when got ctrl+c SIGTERM
func setupCloseHandler(app *fiber.App) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
signal.Notify(c, os.Interrupt, syscall.SIGINT)
go func() {
<-c
zap.L().Info("Got SIGTERM, terminating program...")
app.Server().Shutdown()
}()
}