forked from IAmRiteshKoushik/alfred.soc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (88 loc) · 2.58 KB
/
main.go
File metadata and controls
102 lines (88 loc) · 2.58 KB
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
package main
import (
"io"
"log"
"os"
"strconv"
"time"
"github.com/IAmRiteshKoushik/alfred/bootstrap"
"github.com/IAmRiteshKoushik/alfred/cmd"
"github.com/IAmRiteshKoushik/alfred/controller"
"github.com/IAmRiteshKoushik/alfred/middleware"
"github.com/IAmRiteshKoushik/alfred/pkg"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
failMsg := "[CRASH]: Could not start the app due to %s"
// Setup environment variables
err := cmd.SetupEnv()
if err != nil {
log.Printf(failMsg, err)
return
}
// Setup logger
f, err := os.OpenFile("app.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Printf(failMsg, err)
}
defer f.Close()
pkg.Log = pkg.NewLoggerService(cmd.AppConfig.Environment, f)
pkg.Log.SetupInfo("[ACTIVE]: Logging service is online.")
// Setup connection pooling for Postgres
pool, err := cmd.InitDB()
if err != nil {
pkg.Log.SetupFail("[CRASH]: Could not initialize database pool", err)
return
}
cmd.DBPool = pool
pkg.Log.SetupInfo("[ACTIVE]: Database pool has been created.")
// Setup valkey client
client, err := cmd.InitValkey()
if err != nil {
return
}
pkg.Valkey = client
pkg.Log.SetupInfo("[ACTIVE]: Valkey service is online.")
// Bootstrap Valkey data structures
check := bootstrap.BootstrapValkey()
if !check {
return
}
// Setup gin server
ginLogs, err := os.Create("gin.log")
if err != nil {
pkg.Log.SetupFail("Error creating log file for Gin", err)
return
}
defer ginLogs.Close()
multiWriter := io.MultiWriter(os.Stdout, ginLogs)
gin.DefaultWriter = multiWriter
gin.DefaultErrorWriter = multiWriter
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(pkg.TagRequestWithId)
router.Use(middleware.PanicRecovery)
router.Use(gin.Logger())
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
router.GET("/api/test", controller.TestEndpointHandler)
router.POST("/api/webhook", controller.WebhookHandler)
// router.POST("/api/webhook/install", controller.InstallationHandler)
port := strconv.Itoa(cmd.AppConfig.ServerPort)
pkg.Log.SetupInfo("[ON]: Server configured and starting on PORT:" + port)
routerErr := router.Run(":" + port)
if routerErr != nil {
pkg.Log.SetupFail("[FAIL]: Server failed to start due to:", err)
panic(routerErr)
}
pkg.Log.SetupInfo("[DEACTIVE]: Server offline.")
cmd.CloseValkey(pkg.Valkey)
pkg.Log.SetupInfo("[DEACTIVE]: Valkey offline.")
pkg.Log.SetupInfo("[DEACTIVE]: Logging service offline.")
}