-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (91 loc) · 3.73 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
package main
import (
"fundraising/auth"
"fundraising/campaign"
"fundraising/handler"
"fundraising/helper"
"fundraising/payment"
"fundraising/transaction"
"fundraising/user"
"log"
"net/http"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
dsn := "root:@tcp(127.0.0.1:3306)/fundraising_db?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal(err.Error())
}
userRepository := user.NewRepository(db)
campaignRepository := campaign.NewRepository(db)
transactionRepository := transaction.NewRepository(db)
userService := user.NewService(userRepository)
campaignService := campaign.NewService(campaignRepository)
authService := auth.NewService()
paymentService := payment.NewService()
transactionService := transaction.NewService(transactionRepository, campaignRepository, paymentService)
userHandler := handler.NewUserHandler(userService, authService)
campaignHandler := handler.NewCampaignHandler(campaignService)
transactionHandler := handler.NewTransactionHandler(transactionService)
router := gin.Default()
router.Use(cors.Default())
router.Static("/images", "./images")
api := router.Group("/api/v1")
api.POST("/users", userHandler.RegisterUser)
api.POST("/sessions", userHandler.Login)
api.POST("/email_checker", userHandler.CheckEmailAvailability)
api.POST("/avatars", authMiddleware(authService, userService), userHandler.UploadAvatar)
api.GET("/users/fetch", authMiddleware(authService, userService), userHandler.FetchUser)
api.GET("/campaigns", campaignHandler.GetCampaigns)
api.GET("/campaigns/:id", campaignHandler.GetCampaign)
api.POST("/campaigns", authMiddleware(authService, userService), campaignHandler.CreateCampaign)
api.PUT("/campaigns/:id", authMiddleware(authService, userService), campaignHandler.UpdateCampaign)
api.POST("/campaign-images", authMiddleware(authService, userService), campaignHandler.UploadImage)
api.GET("/campaigns/:id/transactions", authMiddleware(authService, userService), transactionHandler.GetCampaignTransactions)
api.GET("/transactions", authMiddleware(authService, userService), transactionHandler.GetUserTransactions)
api.POST("/transactions", authMiddleware(authService, userService), transactionHandler.CreateTransaction)
api.POST("/transactions/notification", transactionHandler.GetNotification)
router.Run(":8080")
}
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("Unauthorize", http.StatusUnauthorized, "error", nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
// Bearer tokentokentoken
tokenString := ""
arrayToken := strings.Split(authHeader, " ")
if len(arrayToken) == 2 {
tokenString = arrayToken[1]
}
token, err := authService.ValidateToken(tokenString)
if err != nil {
response := helper.APIResponse("Unauthorize", http.StatusUnauthorized, "error", nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
claim, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
response := helper.APIResponse("Unauthorize", http.StatusUnauthorized, "error", nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
userID := int(claim["user_id"].(float64))
user, err := userService.GetUserByID(userID)
if err != nil {
response := helper.APIResponse("Unauthorize", http.StatusUnauthorized, "error", nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
c.Set("currentUser", user)
}
}