-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (110 loc) · 4.52 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
package main
import (
"moyu/auth"
"moyu/campaign"
"moyu/configs"
"moyu/handler"
"moyu/helper"
"moyu/payment"
"moyu/transaction"
"moyu/user"
"net/http"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
func main() {
db := configs.SetupDatabaseConnection()
defer configs.CloseDatabaseConnection(db)
userRepository := user.NewRepository(db)
userService := user.NewService(userRepository)
authService := auth.NewService()
userHandler := handler.NewUserHandler(userService, authService)
campaignRepository := campaign.NewRepository(db)
campaignService := campaign.NewService(campaignRepository)
campaignHandler := handler.NewCampaignHandler(campaignService)
transactionRepository := transaction.NewRepository(db)
paymentService := payment.NewService()
transactionService := transaction.NewService(transactionRepository, campaignRepository, paymentService)
transactionHandler := handler.NewTransactionHandler(transactionService)
router := gin.Default()
// router.Use(cors.Default())
router.Use(CORSMiddleware())
router.Static("/images", "./images")
router.Static("/campaign-images", "./campaign-images")
api := router.Group("/api/v1")
api.POST("/users", userHandler.RegisterUser)
api.POST("/sessions", userHandler.LoginUser)
api.POST("/email_checkers", userHandler.CheckEmailAvaibility)
api.POST("/avatars", authMiddleware(authService, userService), userHandler.UploadAvatar)
api.GET("/users/fetch", authMiddleware(authService, userService), userHandler.FetchUser)
api.POST("/campaigns", authMiddleware(authService, userService), campaignHandler.CreateNewCampaign)
api.POST("/campaign-images", authMiddleware(authService, userService), campaignHandler.UploadImage)
api.PUT("/campaigns/:id", authMiddleware(authService, userService), campaignHandler.UpdateCampaign)
api.GET("/campaigns", campaignHandler.GetCampaigns)
api.GET("/campaigns/:id", campaignHandler.GetCampaign)
api.GET("/campaigns/:id/transactions", authMiddleware(authService, userService), transactionHandler.GetCampaignTransactions)
api.GET("/transactions", authMiddleware(authService, userService), transactionHandler.GetUserTransaction)
api.POST("/transactions", authMiddleware(authService, userService), transactionHandler.CreateNewTransaction)
api.POST("/transactions/notify", transactionHandler.GetNotification)
router.GET("/", handler.Index)
router.Run()
}
// ambil nilai header Authorization :: Value -> "Bearer tokentoken"
// dari header Authorization, kita ambil nilai tokennya saja
// kita validasi token
// kita ambil userID
// ambil user dari db berdasarkan userID lewat service
// kita set context isinya user
func authMiddleware(authService auth.Service, userService user.Service) gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if !strings.Contains(authHeader, "Bearer") {
response := helper.APIResponse("Unauthorized", "error", http.StatusUnauthorized, nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
tokenString := "" // "Bearer tokentoken"
headerTokenString := strings.Split(authHeader, " ")
if len(headerTokenString) == 2 {
tokenString = headerTokenString[1]
}
token, err := authService.ValidateToken(tokenString)
if err != nil {
response := helper.APIResponse("Unauthorized", "error", http.StatusUnauthorized, nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
claim, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
response := helper.APIResponse("Unauthorized", "error", http.StatusUnauthorized, nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
userID := int(claim["user_id"].(float64))
user, err := userService.GetUserByID(userID)
if err != nil {
response := helper.APIResponse("Unauthorized", "error", http.StatusUnauthorized, nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
c.Set("currentUser", user)
}
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "*")
/*
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH")
*/
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}