-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (84 loc) · 3.24 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
package main
import (
"cloud.google.com/go/storage"
"github.com/gin-gonic/gin"
"github.com/khusanov-m/rent-gate-api/controllers"
"github.com/khusanov-m/rent-gate-api/initializers"
"github.com/khusanov-m/rent-gate-api/routes"
"log"
"net/http"
)
var (
server *gin.Engine
storageClient *storage.Client
AuthController controllers.AuthController
AuthRouteController routes.AuthRouteController
StorageController controllers.StorageController
StorageRouteController routes.StorageRouteController
UserController controllers.UserController
UserRouteController routes.UserRouteController
VehiclesController controllers.VehicleController
VehiclesRouteController routes.VehicleRouteController
PaymentController controllers.PaymentController
PaymentRouteController routes.PaymentRouteController
HistoryController controllers.HistoryController
HistoryRouteController routes.HistoryRouteController
)
func init() {
config, err := initializers.LoadConfig(".")
if err != nil {
log.Fatal("? Could not load environment variables", err)
}
initializers.ConnectDB(&config)
storageClient, err = initializers.ConnectStorage()
if err != nil {
log.Fatal("? Could not connect Firebase Storage", err)
}
AuthController = controllers.NewAuthController(initializers.DB)
AuthRouteController = routes.NewAuthRouteController(AuthController)
StorageController = controllers.NewStorageController(initializers.DB, storageClient)
StorageRouteController = routes.NewStorageRouteController(StorageController)
UserController = controllers.NewUserController(initializers.DB)
UserRouteController = routes.NewUserRouteController(UserController)
VehiclesController = controllers.NewVehicleController(initializers.DB)
VehiclesRouteController = routes.NewVehicleRouteController(VehiclesController)
PaymentController = controllers.NewPaymentController(initializers.DB)
PaymentRouteController = routes.NewPaymentRouteController(PaymentController)
HistoryController = controllers.NewHistoryController(initializers.DB)
HistoryRouteController = routes.NewHistoryRouteController(HistoryController)
if config.Environment == "prod" {
gin.SetMode(gin.ReleaseMode)
}
server = gin.Default()
server.Use(corsMiddleware())
}
func main() {
config, err := initializers.LoadConfig(".")
if err != nil {
log.Fatal("? Could not load environment variables", err)
}
router := server.Group("/api/v1")
// PING method to check service status
router.GET("/ping", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": "pong"})
})
AuthRouteController.AuthRoute(router)
StorageRouteController.StorageRoute(router)
UserRouteController.UserRoute(router)
VehiclesRouteController.VehicleRoute(router)
PaymentRouteController.PaymentRoute(router)
HistoryRouteController.HistoryRoute(router)
log.Fatal(server.Run(":" + config.ServerPort))
}
func corsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}