/
routes.go
102 lines (81 loc) · 3.12 KB
/
routes.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
package routes
import (
"net/http"
"github.com/amarmaulana95/go-echo/handler"
"github.com/amarmaulana95/go-echo/middleware"
"github.com/amarmaulana95/go-echo/repository"
"github.com/amarmaulana95/go-echo/services"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
func NewRoute(db *gorm.DB, router *echo.Echo) {
repositoryAuth := repository.NewRepositoryAuth(db)
serviceAuth := services.NewServiceAuth(repositoryAuth)
handlerAuth := handler.NewHandlerAuth(serviceAuth)
repositoryUser := repository.NewRepositoryUser(db)
serviceUser := services.NewServiceUser(repositoryUser)
handlerUser := handler.NewHandlerUser(serviceUser)
repositoryProduct := repository.NewRepositoryProduct(db)
serviceProduct := services.NewserviceProduct(repositoryProduct)
handlerProduct := handler.NewHandlerProduct(serviceProduct)
route := router.Group("/api/hello")
routerAuth := router.Group("/api/auth")
routerUser := router.Group("/api/users")
routerProducts := router.Group("/api/products")
route.GET("/main", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
routerAuth.POST("/login", handlerAuth.HandlerLogin)
routerAuth.POST("/register", handlerAuth.HandlerRegister)
middleware.WebSecurityConfig(router)
routerUser.GET("/hello", handlerUser.HandlerHello)
routerUser.GET("/users", handlerUser.HandlerResults)
routerUser.GET("/userall", handlerUser.HandlerResultAll)
routerUser.GET("/users/:id", handlerUser.HandlerResultService)
routerUser.GET("/cari", handlerUser.HandlerResultSearch)
routerProducts.GET("/product", handlerProduct.HandlerResults)
routerProducts.GET("/products", handlerProduct.HandlerResultAll)
// routerProducts.GET("/productsID", handlerProduct.HandlerProductSearch)
}
// func Authentication(next echo.HandlerFunc) echo.HandlerFunc {
// var (
// jwtKey = os.Getenv("JWT_KEY")
// )
// return func(c echo.Context) error {
// authToken := c.Request().Header.Get("Authorization")
// if authToken == "" {
// return echo.NewHTTPError(http.StatusUnauthorized, "Please provide valid credentials")
// }
// splitToken := strings.Split(authToken, "Bearer ")
// token, err := jwt.Parse(splitToken[1], func(token *jwt.Token) (interface{}, error) {
// if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
// return nil, echo.NewHTTPError(http.StatusUnauthorized, "Please provide valid credentials")
// }
// return []byte(jwtKey), nil
// })
// if !token.Valid || err != nil {
// return echo.NewHTTPError(http.StatusUnauthorized, "Please provide valid credentials")
// }
// var id int
// destructID := token.Claims.(jwt.MapClaims)["id"]
// if destructID != nil {
// id = int(destructID.(float64))
// } else {
// id = 0
// }
// return id
// }
// }
// token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{})
// if err != nil {
// fmt.Printf("Error %s", err)
// }
// if claims, ok := token.Claims.(jwt.MapClaims); ok {
// // obtains claims
// sub := fmt.Sprint(claims["sub"])
// name := fmt.Sprint(claims["name"])
// // and so on and on
// // ...
// fmt.Printf("sub = %s\r\n", sub)
// fmt.Printf("name = %s", name)
// }