This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
57 lines (49 loc) · 1.91 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/ydhnwb/golang_heroku/config"
v1 "github.com/ydhnwb/golang_heroku/handler/v1"
"github.com/ydhnwb/golang_heroku/middleware"
"github.com/ydhnwb/golang_heroku/repo"
"github.com/ydhnwb/golang_heroku/service"
"gorm.io/gorm"
)
var (
db *gorm.DB = config.SetupDatabaseConnection()
userRepo repo.UserRepository = repo.NewUserRepo(db)
productRepo repo.ProductRepository = repo.NewProductRepo(db)
authService service.AuthService = service.NewAuthService(userRepo)
jwtService service.JWTService = service.NewJWTService()
userService service.UserService = service.NewUserService(userRepo)
productService service.ProductService = service.NewProductService(productRepo)
authHandler v1.AuthHandler = v1.NewAuthHandler(authService, jwtService, userService)
userHandler v1.UserHandler = v1.NewUserHandler(userService, jwtService)
productHandler v1.ProductHandler = v1.NewProductHandler(productService, jwtService)
)
func main() {
defer config.CloseDatabaseConnection(db)
server := gin.Default()
authRoutes := server.Group("api/auth")
{
authRoutes.POST("/login", authHandler.Login)
authRoutes.POST("/register", authHandler.Register)
}
userRoutes := server.Group("api/user", middleware.AuthorizeJWT(jwtService))
{
userRoutes.GET("/profile", userHandler.Profile)
userRoutes.PUT("/profile", userHandler.Update)
}
productRoutes := server.Group("api/product", middleware.AuthorizeJWT(jwtService))
{
productRoutes.GET("/", productHandler.All)
productRoutes.POST("/", productHandler.CreateProduct)
productRoutes.GET("/:id", productHandler.FindOneProductByID)
productRoutes.PUT("/:id", productHandler.UpdateProduct)
productRoutes.DELETE("/:id", productHandler.DeleteProduct)
}
checkRoutes := server.Group("api/check")
{
checkRoutes.GET("health", v1.Health)
}
server.Run()
}