-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (95 loc) · 3.83 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
package main
import (
"log"
"net/http"
"open_projects/auth"
"open_projects/handler"
"open_projects/helper"
"open_projects/participation"
"open_projects/project"
"open_projects/transaction"
"open_projects/user"
"os"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
dsn := os.Getenv("dsn")
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal(err.Error())
}
userRepository := user.NewRepository(db)
projectRepository := project.NewRepository(db)
transactionRepository := transaction.NewRepository(db)
participationRepository := participation.NewRepository(db)
userService := user.NewService(userRepository)
projectService := project.NewService(projectRepository)
authService := auth.NewService()
transactionService := transaction.NewService(transactionRepository, projectRepository)
participationService := participation.NewService(participationRepository, projectRepository)
userHandler := handler.NewUserHandler(userService, authService)
projectHandler := handler.NewProjectHandler(projectService)
transactionHandler := handler.NewTransactionHandler(transactionService)
participationHandler := handler.NewParticipationHandler(participationService)
router := gin.Default()
router.Static("/images", "./images")
api := router.Group("/api/v1")
api.POST("/users", userHandler.RegisterUser)
api.POST("/sessions", userHandler.Login)
api.POST("/email_checkers", userHandler.CheckEmailAvailability)
api.POST("/avatars", authMiddleware(authService, userService), userHandler.UploadAvatar)
api.GET("/projects", projectHandler.GetProjects)
api.GET("/projects/:id", projectHandler.GetProject)
api.POST("/projects", authMiddleware(authService, userService), projectHandler.CreateProject)
api.PUT("/projects/:id", authMiddleware(authService, userService), projectHandler.UpdateProject)
api.POST("/project-images", authMiddleware(authService, userService), projectHandler.UploadImage)
api.GET("/projects/:id/transactions", authMiddleware(authService, userService), transactionHandler.GetProjectTransactions)
api.GET("/projects/:id/participations", authMiddleware(authService, userService), participationHandler.GetProjectParticipations)
api.GET("/transactions", authMiddleware(authService, userService), transactionHandler.GetUserTransactions)
api.GET("/participations", authMiddleware(authService, userService), participationHandler.GetUserParticipations)
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
}
tokenString := ""
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)
}
}