-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
109 lines (84 loc) · 3.23 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
package main
import (
"log"
"net/http"
"time"
jwt "github.com/appleboy/gin-jwt"
"github.com/gin-gonic/gin"
cartHttpDeliver "github.com/karuppaiah/shopping/cart/delivery/http"
cartRepo "github.com/karuppaiah/shopping/cart/repository"
cartUcase "github.com/karuppaiah/shopping/cart/usecase"
"github.com/karuppaiah/shopping/controller"
"github.com/karuppaiah/shopping/middleware"
"github.com/karuppaiah/shopping/model"
productHttpDeliver "github.com/karuppaiah/shopping/product/delivery/http"
productRepo "github.com/karuppaiah/shopping/product/repository"
productUcase "github.com/karuppaiah/shopping/product/usecase"
promotionHttpDeliver "github.com/karuppaiah/shopping/promotion/delivery/http"
promotionRepo "github.com/karuppaiah/shopping/promotion/repository"
promotionUcase "github.com/karuppaiah/shopping/promotion/usecase"
loginHttpDeliver "github.com/karuppaiah/shopping/login/delivery/http"
loginRepo "github.com/karuppaiah/shopping/login/repository"
loginUcase "github.com/karuppaiah/shopping/login/usecase"
// gin-swagger middleware
// swagger embed files
)
func init() {
// setupRouter()
}
func setupRouter() *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
r := gin.Default()
// the jwt middleware
authMiddleware := middleware.InitMiddleware().AuthMiddleware()
// API's to get JWT toekn
r.POST("/login", authMiddleware.LoginHandler)
r.NoRoute(authMiddleware.MiddlewareFunc(), func(c *gin.Context) {
claims := jwt.ExtractClaims(c)
log.Printf("NoRoute claims: %#v\n", claims)
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"})
})
// API's with JWT authentication
auth := r.Group("/auth")
auth.Use(authMiddleware.MiddlewareFunc())
{
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
// auth.POST("/products", controller.HandlePostProducts)
// auth.DELETE("/products", controller.HandleDelProducts)
// auth.GET("/cart", controller.HandleGetCart)
// auth.POST("/cart", controller.HandlePostCart)
// auth.DELETE("/cart", controller.HandleDelCart)
// auth.GET("/promotion", controller.HandleGetPromotion)
// auth.POST("/promotion", controller.HandlePostPromotion)
// auth.DELETE("/promotion", controller.HandleDelPromotion)
}
// r.Use(Cors())
// Public API's without JWT authentication
// HealthCheck
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
r.GET("/", controller.HandlerRoot)
// r.GET("/products", controller.HandleGetProducts)
// r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
return r
}
func main() {
r := setupRouter()
db := model.GetDBInstance()
timeoutContext := time.Duration(10) * time.Second
prR := productRepo.NewProductRepository(db.SDB)
pu := productUcase.NewProductUsecase(prR, timeoutContext)
productHttpDeliver.NewProductHttpHandler(r, pu)
caR := cartRepo.NewERepository(db.SDB)
cu := cartUcase.NewEUsecase(caR, timeoutContext)
cartHttpDeliver.NewCartHttpHandler(r, cu)
poR := promotionRepo.NewERepository(db.SDB)
pou := promotionUcase.NewEUsecase(poR, timeoutContext)
promotionHttpDeliver.NewPromotionHttpHandler(r, pou)
loR := loginRepo.NewERepository(db.SDB)
loU := loginUcase.NewEUsecase(loR, timeoutContext)
loginHttpDeliver.NewLoginHttpHandler(r, loU)
r.Run(":8080")
}