-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (95 loc) · 2.99 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
package main
import (
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"net/http"
"startup_be/Handler"
"startup_be/Helper"
"startup_be/Test"
"startup_be/Users"
"startup_be/auth"
"startup_be/campaign"
"strings"
)
func main() {
dsn := "root:@tcp(127.0.0.1:3306)/startup?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal(err.Error())
}
router := gin.Default()
apiV1 := router.Group("api/v1")
//TEST
testRepository := Test.NewRepository(db)
testService := Test.NewService(testRepository)
testHandler := Handler.NewTestHandler(testService)
//TEST
//TEST CONNECTION TO DB
apiV1.GET("/testConnect", testHandler.GetUser)
//END CONNECTION TO DB
// USERS
authService := auth.NewService()
userRepository := Users.NewRepository(db)
userService := Users.NewService(userRepository)
userHandler := Handler.NewUserHandler(userService, authService)
// END USERS
// Campaign
campaignRepository := campaign.NewRepository(db)
campaignService := campaign.NewService(campaignRepository)
campaignHandler := Handler.NewHandler(campaignService)
// End Campaign
//REGISTER USER API
apiV1.POST("/users", userHandler.RegisterUser) //Register User
//END REGISTER USER API
//LOGIN USER API
apiV1.POST("/sessions", userHandler.Login)
//END LOGIN USER API
//CHECK EMAIL AVAILABLE
apiV1.POST("/checkEmail", userHandler.CheckEmailAvailability)
//END CHECK EMAIL AVAILABLE
//UPLOAD AVATAR
apiV1.POST("/avatars", authMiddleware(authService, userService), userHandler.UploadAvatar)
//END UPLOAD AVATAR
// GET LIST CAMPAIGNS
apiV1.GET("/campaigns", campaignHandler.FindCampaigns)
// END GET LIST CAMPAIGNS
router.Run()
}
func authMiddleware(authService auth.Service, userService Users.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)
}
}