-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (59 loc) · 1.67 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
durable "tigerhall-kittens/cmd/durables"
router "tigerhall-kittens/cmd/routes"
"tigerhall-kittens/cmd/utils"
config "tigerhall-kittens/config"
_ "github.com/swaggo/swag"
_ "tigerhall-kittens/docs"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// @title tiggerhall-kittens
// @version 1.0
// @description Tiggerhall-Kittens
// @contact.name Mohamed Sameem
// @contact.email [email protected]
// @BasePath /
func main() {
config.LoadEnv()
durable.MessageQueueVariable = durable.NewMessageQueue(10)
ctx, cancel := context.WithCancel(context.Background())
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
cancel()
}()
workerPool := durable.NewWorkerPool(10, durable.MessageQueueVariable)
workerPool.StartWorkers()
durable.InitMysqlDb()
durable.InitMysqlDbMigration() //remove this line if you don't want to migrate the db
r := router.SetupRouter(GinContextToContextMiddleware())
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
go func() {
log.Println("Server listening on :8090")
err := r.Run(":8090")
if err != nil && err != http.ErrServerClosed {
log.Printf("HTTP server error: %v\n", err)
cancel()
}
}()
<-ctx.Done()
log.Println("Shutting down...")
utils.HandleError("Error while starting server ", nil)
defer durable.CloseDbConnection()
defer durable.MessageQueueVariable.Dequeue() // Close the message queue
}
func GinContextToContextMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
}
}