/
main.go
108 lines (84 loc) · 2.63 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
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/gofiber/fiber/v2"
"github.com/granitebps/fiber-api/config"
"github.com/granitebps/fiber-api/pkg/constants"
"github.com/granitebps/fiber-api/pkg/core"
"github.com/granitebps/fiber-api/scheduler"
"github.com/granitebps/fiber-api/src/middleware"
"github.com/granitebps/fiber-api/src/route"
"github.com/spf13/viper"
)
// @title Fiber API
// @version 1.0
// @description This is a Fiber API Doc
// @contact.name Granite Bagas
// @contact.url https://granitebps.com
// @contact.email granitebagas28@gmail.com
// @license.name MIT
// @BasePath /
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
func main() {
// Load ENV and setup some config
config.SetupConfig(".env")
// Initiate Fiber
app := fiber.New(config.FiberConfig())
// Setup core package
c := core.SetupCore()
// Setup middleware
middleware.SetupMiddleware(app, c)
// Setup Dependency Injection
h := SetupDependencies(c)
// Setup route
route.SetupRoute(app, h)
// Setup scheduler
scheduler.SetupScheduler(c)
startServerWithGracefulShutdown(app)
}
func startServerWithGracefulShutdown(app *fiber.App) {
PORT := viper.GetString(constants.APP_PORT)
// Listen from a different goroutine
go func() {
if err := app.Listen(PORT); err != nil {
log.Panic(err)
}
}()
c := make(chan os.Signal, 1) // Create channel to signify a signal being sent
signal.Notify(c, os.Interrupt, syscall.SIGTERM) // When an interrupt or termination signal is sent, notify the channel
<-c // This blocks the main thread until an interrupt is received
log.Println("Gracefully shutting down...")
_ = app.Shutdown()
log.Println("Running cleanup tasks...")
// Your cleanup tasks go here
// db.Close()
// redisConn.Close()
log.Println("Fiber was successful shutdown.")
}
// func startServerWithGracefulShutdown(app *fiber.App) {
// PORT := viper.GetString(constants.APP_PORT)
// // Create channel for idle connections.
// idleConnsClosed := make(chan struct{})
// go func() {
// sigint := make(chan os.Signal, 1)
// signal.Notify(sigint, os.Interrupt) // Catch OS signals.
// <-sigint
// // Received an interrupt signal, shutdown.
// if err := app.Shutdown(); err != nil {
// // Error from closing listeners, or context timeout:
// log.Printf("Oops... Server is not shutting down! Reason: %v", err)
// }
// close(idleConnsClosed)
// }()
// // Run server.
// if err := app.Listen(PORT); err != nil {
// log.Printf("Oops... Server is not running! Reason: %v", err)
// }
// <-idleConnsClosed
// }