-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (104 loc) · 4.16 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
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"net/http"
"os"
"startup-funding/auth"
"startup-funding/campaign"
"startup-funding/handler"
"startup-funding/helper"
"startup-funding/payment"
"startup-funding/transaction"
"startup-funding/user"
"strings"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %s", err)
}
dbUsername := os.Getenv("DB_USERNAME")
dbPassword := os.Getenv("DB_PASSWORD")
dbHost := os.Getenv("DB_HOST")
dbPort := os.Getenv("DB_PORT")
dbDatabase := os.Getenv("DB_DATABASE")
dsn := dbUsername + ":" + dbPassword + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbDatabase +
"?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)
authService := auth.NewService()
campaignService := campaign.NewService(campaignRepository)
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/")
// Users Endpoint
api.POST("/users", userHandler.RegisterUser)
api.POST("/sessions", userHandler.LoginUser)
api.POST("/email-checkers", userHandler.CheckEmailAvailability)
api.POST("/avatars", authMiddleware(authService, userService), userHandler.UploadAvatar)
api.GET("/users/fetch", authMiddleware(authService, userService), userHandler.FetchUser)
// Campaigns Endpoint
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)
// Transactions Endpoint
api.GET("/campaigns/:id/transactions", authMiddleware(authService, userService), transactionHandler.GetCampaignTransacations)
api.GET("/transactions", authMiddleware(authService, userService), transactionHandler.GetUserTransaction)
api.POST("/transactions", authMiddleware(authService, userService), transactionHandler.CreateTransaction)
api.POST("/transactions/notification", transactionHandler.GetNotification)
router.Run()
}
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", http.StatusUnauthorized, "error", nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
var tokenString string
arrayToken := strings.Split(authHeader, " ")
if len(arrayToken) == 2 {
tokenString = arrayToken[1]
}
token, err := authService.ValidateToken(tokenString)
if err != nil {
response := helper.APIResponse("Unauthorized", http.StatusUnauthorized, "error", nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
claim, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
response := helper.APIResponse("Unauthorized", 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("Unauthorized", http.StatusUnauthorized, "error", nil)
c.AbortWithStatusJSON(http.StatusUnauthorized, response)
return
}
c.Set("currentUser", user)
}
}