Cloudreve/routers/router.go

63 lines
1.1 KiB
Go
Raw Normal View History

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