-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (56 loc) · 1.48 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/joho/godotenv"
"go-auth/infrastructure/controllers"
"go-auth/infrastructure/controllers/forms"
"go-auth/infrastructure/middlewares"
"net/http"
)
func main() {
errEnv := godotenv.Load(".env")
if errEnv != nil {
fmt.Println(errEnv)
}
r := gin.Default()
binding.Validator = new(forms.DefaultValidator)
r.GET("/ping", controllers.PingController{}.Ping)
v1 := r.Group("/api/v1")
{
userController := controllers.NewUserController()
v1.POST("/register", userController.Register)
v1.POST("/login", userController.Login)
// auth route
auth := v1.Group("/")
auth.Use(middlewares.AuthMiddleware())
{
auth.GET("/private", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "success",
"data": gin.H{},
})
})
role := auth.Group("/")
{
roleAdmin := role.Group("/")
roleAdmin.Use(middlewares.RoleMiddleware([]string{"admin"}))
{
roleAdmin.GET("/private/admin", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "success",
"data": gin.H{},
})
})
}
}
}
auth.GET("/posts", middlewares.ReverseProxy("https://jsonplaceholder.typicode.com", true))
auth.GET("/get-all-comments", middlewares.ReverseProxy("https://jsonplaceholder.typicode.com/comments", false))
}
err := r.Run()
if err != nil {
return
} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}