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-23 15:09:46 +08:00
|
|
|
"github.com/gin-contrib/cors"
|
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-23 15:09:46 +08: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 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-23 16:58:56 +08:00
|
|
|
v3 := r.Group("/api/v3")
|
2019-11-05 12:31:22 +08:00
|
|
|
{
|
2019-11-05 19:49:56 +08:00
|
|
|
// 测试用路由
|
2019-11-23 16:58:56 +08:00
|
|
|
v3.GET("site/ping", controllers.Ping)
|
2019-11-05 19:49:56 +08:00
|
|
|
// 用户登录
|
2019-11-23 16:58:56 +08:00
|
|
|
v3.POST("user/session", controllers.UserLogin)
|
2019-11-13 17:03:55 +08:00
|
|
|
// 验证码
|
2019-11-23 16:58:56 +08:00
|
|
|
v3.GET("captcha", controllers.Captcha)
|
2019-11-22 19:33:06 +08:00
|
|
|
// 站点全局配置
|
2019-11-23 16:58:56 +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())
|
|
|
|
{
|
2019-11-23 15:37:13 +08:00
|
|
|
// 用户
|
2019-11-23 16:58:56 +08:00
|
|
|
user := auth.Group("user")
|
2019-11-12 15:34:54 +08:00
|
|
|
{
|
|
|
|
// 当前登录用户信息
|
2019-11-23 16:58:56 +08:00
|
|
|
user.GET("me", controllers.UserMe)
|
2019-11-12 15:34:54 +08:00
|
|
|
}
|
|
|
|
|
2019-11-15 18:12:34 +08:00
|
|
|
// 文件
|
2019-11-23 16:58:56 +08:00
|
|
|
file := auth.Group("file")
|
2019-11-15 18:12:34 +08:00
|
|
|
{
|
2019-11-22 14:56:46 +08:00
|
|
|
// 文件上传
|
2019-11-23 16:58:56 +08:00
|
|
|
file.POST("upload", controllers.FileUploadStream)
|
2019-11-15 18:12:34 +08:00
|
|
|
}
|
|
|
|
|
2019-11-23 15:37:13 +08:00
|
|
|
// 目录
|
2019-11-23 16:58:56 +08:00
|
|
|
directory := auth.Group("directory")
|
2019-11-23 15:37:13 +08:00
|
|
|
{
|
2019-11-24 11:33:30 +08:00
|
|
|
// 创建目录
|
|
|
|
directory.PUT("", controllers.CreateDirectory)
|
2019-11-24 13:06:15 +08:00
|
|
|
// 列出目录下内容
|
|
|
|
directory.GET("", controllers.ListDirectory)
|
2019-11-23 15:37:13 +08:00
|
|
|
}
|
|
|
|
|
2019-11-12 15:34:54 +08:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:31:22 +08:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|