2020-11-01 01:09:49 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2020-11-23 23:48:29 +08:00
|
|
|
"github.com/casdoor/casdoor/internal/handler/application"
|
|
|
|
|
2020-11-11 21:38:51 +08:00
|
|
|
"github.com/casdoor/casdoor/internal/handler/user"
|
|
|
|
"github.com/casdoor/casdoor/internal/store"
|
|
|
|
|
2020-11-01 01:09:49 +08:00
|
|
|
"github.com/gin-contrib/cors"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
var corsConfig = cors.Config{
|
|
|
|
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"},
|
|
|
|
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
|
|
|
|
ExposeHeaders: []string{"Content-Length"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSNotSupportingCredentials
|
|
|
|
AllowOrigins: []string{"http://localhost:3000"},
|
|
|
|
MaxAge: 300,
|
|
|
|
}
|
|
|
|
|
2020-11-23 23:48:29 +08:00
|
|
|
func New(userStore *store.UserStore, applicationStore *store.ApplicationStore) http.Handler {
|
2020-11-01 01:09:49 +08:00
|
|
|
r := gin.New()
|
|
|
|
r.Use(gin.Logger())
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
r.Use(cors.New(corsConfig))
|
|
|
|
|
|
|
|
//r.StaticFS("/", http.Dir("web/build/index.html"))
|
|
|
|
|
|
|
|
apiGroup := r.Group("/api")
|
2020-11-11 21:38:51 +08:00
|
|
|
userHandler := user.New(userStore)
|
|
|
|
apiGroup.GET("/get-users", userHandler.GetUsers)
|
|
|
|
apiGroup.GET("/get-user", userHandler.GetUser)
|
|
|
|
apiGroup.POST("/update-user", userHandler.UpdateUser)
|
|
|
|
apiGroup.POST("/add-user", userHandler.AddUser)
|
|
|
|
apiGroup.POST("/delete-user", userHandler.DeleteUser)
|
2020-11-01 01:09:49 +08:00
|
|
|
|
2020-11-23 23:48:29 +08:00
|
|
|
applicationHandler := application.New(applicationStore)
|
|
|
|
apiGroup.GET("/applications", applicationHandler.List)
|
|
|
|
apiGroup.POST("/applications", applicationHandler.Create)
|
|
|
|
|
2020-11-01 01:09:49 +08:00
|
|
|
return r
|
|
|
|
}
|