Cloudreve/routers/router.go

72 lines
1.4 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-23 02:09:46 -05:00
"github.com/gin-contrib/cors"
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-23 02:09:46 -05:00
// 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,
}))
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
// 测试用路由
v3.GET("Site/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)
// 站点全局配置
v3.GET("Site/Config", controllers.SiteConfig)
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-22 01:56:46 -05:00
// 文件上传
file.POST("Upload", 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
}