You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/routers/router.go

79 lines
1.6 KiB

5 years ago
package routers
import (
"github.com/HFO4/cloudreve/middleware"
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/routers/controllers"
"github.com/gin-contrib/cors"
5 years ago
"github.com/gin-gonic/gin"
)
5 years ago
// InitRouter 初始化路由
5 years ago
func InitRouter() *gin.Engine {
5 years ago
r := gin.Default()
5 years ago
/*
*/
5 years ago
r.Use(middleware.Session(conf.SystemConfig.SessionSecret))
// CORS TODO: 根据配置文件来
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3000"},
AllowMethods: []string{"PUT", "POST", "GET", "OPTIONS"},
AllowHeaders: []string{"X-PINGOTHER", "Content-Type"},
AllowCredentials: true,
}))
// 测试模式加入Mock助手中间件
if gin.Mode() == gin.TestMode {
r.Use(middleware.MockHelper())
}
5 years ago
r.Use(middleware.CurrentUser())
5 years ago
/*
*/
5 years ago
v3 := r.Group("/Api/V3")
{
// 测试用路由
v3.GET("Site/Ping", controllers.Ping)
// 用户登录
5 years ago
v3.POST("User/Session", controllers.UserLogin)
5 years ago
// 验证码
v3.GET("Captcha", controllers.Captcha)
// 站点全局配置
v3.GET("Site/Config", controllers.SiteConfig)
5 years ago
5 years ago
// 需要登录保护的
auth := v3.Group("")
auth.Use(middleware.AuthRequired())
{
// 用户
5 years ago
user := auth.Group("User")
{
// 当前登录用户信息
user.GET("Me", controllers.UserMe)
}
// 文件
file := auth.Group("File")
{
// 文件上传
file.POST("Upload", controllers.FileUploadStream)
}
// 目录
directory := auth.Group("Directory")
{
// 文件上传
directory.PUT("", controllers.Ping)
}
5 years ago
}
5 years ago
}
return r
}