-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (110 loc) · 3.89 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"net/http"
"os"
DBConnection "sorteio-api/source/database"
"sorteio-api/source/middleware"
"sorteio-api/source/modules"
efi "sorteio-api/source/resources/efi_sdk"
"sorteio-api/source/resources/sentry"
"sorteio-api/source/routes"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
)
func home(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"message": "Welcome to sorteio Api"})
}
func main() {
err := godotenv.Load()
if err != nil {
panic(".env file couldn't be loaded")
}
sentry.SentryInitialization()
DBConnection.StartMongoDBConnection()
router := gin.Default()
router.GET("/", home)
router.Use(Cors())
router.Use(Options)
router.Use(sentrygin.New(sentrygin.Options{
Repanic: true,
}))
authorized := router.Group("/")
authorized.Use(middleware.Middleware(func(token string, c *gin.Context) bool {
jwtToken, err := modules.AuthTokenService.ValidateToken(token)
if err != nil {
// If token is not valid, set HTTP status code to 401 (Unauthorized)
// And return false for blocking the request flow
c.AbortWithStatus(http.StatusForbidden)
return false
}
modules.AuthTokenService.GetTokenUserId(jwtToken.Claims.(jwt.MapClaims))
// If token is valid, return true to continue the request flow
return true
}))
{
routes.OrderProtectedRoute(authorized, modules.OrderController)
routes.SorteioProtectedRoute(authorized, modules.SorteioController)
routes.UserProtectedRoute(authorized, modules.UserController)
// routes.CloudRoute(authorized, cloud)
}
routes.OrderRoute(router, modules.OrderController)
routes.UserRoute(router, modules.UserController)
routes.SorteioRoute(router, modules.SorteioController)
routes.WinnerRoute(router, modules.WinnerController)
routes.AuthRoute(router, modules.AuthTokenController)
router.GET("/ticket", modules.TicketController.TryGetTicket)
// router.POST("/webhook-config", modules.OrderController.ConfigWebhookRequest)
authorizedWebhoook := router.Group("/")
authorizedWebhoook.Use(middleware.Middleware(func(token string, c *gin.Context) bool {
if !modules.AuthTokenService.CheckIsWebhookAutenticationToken(token) {
// If token is not valid, set HTTP status code to 401 (Unauthorized)
// And return false for blocking the request flow
c.AbortWithStatus(http.StatusForbidden)
return false
}
// If token is valid, return true to continue the request flow
return true
}))
{
//authorizedWebhoook.POST("/webhook", orderController.WebhookRequest);;
authorizedWebhoook.POST("/pix", modules.OrderController.PixHandle)
// authorizedWebhoook.POST("/pix-confirm", controller.ConfirmPix)
}
efi.InitializePayment()
go modules.OrderService.AnalyzePendingOrders()
ginMode := os.Getenv("GIN_MODE")
// frontEntEndpoint := os.Getenv("CORS_FRONTEND")
// enableCors := os.Getenv("ENABLE_CORS")
gin.SetMode(ginMode)
// config := cors.DefaultConfig()
// var corsString string = frontEntEndpoint
// if enableCors == "false" {
// corsString = "*"
// }
// config.AllowOrigins = []string{corsString}
// router.Use(cors.New(config))
router.SetTrustedProxies([]string{"127.0.0.1"})
port := os.Getenv("GIN_PORT")
router.Run(":" + port)
}
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Add("Access-Control-Allow-Origin", "*")
c.Writer.Header().Add("Access-Control-Allow-Headers", "content-type, Accept, authorization")
c.Next()
}
}
func Options(c *gin.Context) {
if c.Request.Method != "OPTIONS" {
c.Next()
} else {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
c.Header("Access-Control-Allow-Headers", "authorization, origin, content-type, accept")
c.Header("Allow", "HEAD,GET,POST,PUT,PATCH,DELETE,OPTIONS")
c.Header("Content-Type", "application/json")
c.AbortWithStatus(http.StatusOK)
}
}