2019-11-05 12:31:22 +08:00
|
|
|
package routers
|
|
|
|
|
|
|
|
import (
|
2019-11-16 16:11:37 +08:00
|
|
|
"github.com/HFO4/cloudreve/middleware"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/conf"
|
|
|
|
"github.com/HFO4/cloudreve/routers/controllers"
|
2019-11-05 12:31:22 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2019-11-12 15:34:54 +08:00
|
|
|
// InitRouter 初始化路由
|
2019-11-05 12:31:22 +08:00
|
|
|
func InitRouter() *gin.Engine {
|
2019-11-14 14:24:39 +08:00
|
|
|
r := gin.Default()
|
2019-11-05 12:31:22 +08:00
|
|
|
|
2019-11-12 16:53:32 +08:00
|
|
|
/*
|
|
|
|
中间件
|
|
|
|
*/
|
2019-11-11 19:13:17 +08:00
|
|
|
r.Use(middleware.Session(conf.SystemConfig.SessionSecret))
|
2019-11-12 16:53:32 +08:00
|
|
|
|
2019-11-14 14:45:06 +08:00
|
|
|
// 测试模式加入Mock助手中间件
|
2019-11-12 16:53:32 +08:00
|
|
|
if gin.Mode() == gin.TestMode {
|
|
|
|
r.Use(middleware.MockHelper())
|
|
|
|
}
|
|
|
|
|
2019-11-12 15:34:54 +08:00
|
|
|
r.Use(middleware.CurrentUser())
|
2019-11-11 19:13:17 +08:00
|
|
|
|
2019-11-12 16:53:32 +08:00
|
|
|
/*
|
|
|
|
路由
|
|
|
|
*/
|
2019-11-05 12:31:22 +08:00
|
|
|
v3 := r.Group("/Api/V3")
|
|
|
|
{
|
2019-11-05 19:49:56 +08:00
|
|
|
// 测试用路由
|
2019-11-22 19:33:06 +08:00
|
|
|
v3.GET("Site/Ping", controllers.Ping)
|
2019-11-05 19:49:56 +08:00
|
|
|
// 用户登录
|
2019-11-11 19:13:17 +08:00
|
|
|
v3.POST("User/Session", controllers.UserLogin)
|
2019-11-13 17:03:55 +08:00
|
|
|
// 验证码
|
|
|
|
v3.GET("Captcha", controllers.Captcha)
|
2019-11-22 19:33:06 +08:00
|
|
|
// 站点全局配置
|
|
|
|
v3.GET("Site/Config", controllers.SiteConfig)
|
2019-11-05 12:31:22 +08:00
|
|
|
|
2019-11-12 15:34:54 +08:00
|
|
|
// 需要登录保护的
|
|
|
|
auth := v3.Group("")
|
|
|
|
auth.Use(middleware.AuthRequired())
|
|
|
|
{
|
|
|
|
// 用户类
|
|
|
|
user := auth.Group("User")
|
|
|
|
{
|
|
|
|
// 当前登录用户信息
|
|
|
|
user.GET("Me", controllers.UserMe)
|
|
|
|
}
|
|
|
|
|
2019-11-15 18:12:34 +08:00
|
|
|
// 文件
|
|
|
|
file := auth.Group("File")
|
|
|
|
{
|
2019-11-22 14:56:46 +08:00
|
|
|
// 文件上传
|
|
|
|
file.POST("Upload", controllers.FileUploadStream)
|
2019-11-15 18:12:34 +08:00
|
|
|
}
|
|
|
|
|
2019-11-12 15:34:54 +08:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:31:22 +08:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|