Cloudreve/routers/router.go

119 lines
2.7 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-12-04 00:49:28 -05:00
"github.com/gin-contrib/pprof"
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-12-04 00:49:28 -05:00
pprof.Register(r)
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"},
2019-11-29 02:24:23 -05:00
AllowHeaders: []string{"Cookie", "Content-Length", "Content-Type", "X-Path", "X-FileName"},
2019-11-23 02:09:46 -05:00
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-23 03:58:56 -05:00
v3 := r.Group("/api/v3")
2019-11-04 23:31:22 -05:00
{
2019-11-05 06:49:56 -05:00
// 测试用路由
2019-11-23 03:58:56 -05:00
v3.GET("site/ping", controllers.Ping)
// 不需要登录的用户相关路由
{
// 用户登录
v3.POST("user/session", controllers.UserLogin)
// WebAuthn登陆初始化
v3.GET("user/authn/:username", controllers.StartLoginAuthn)
// WebAuthn登陆
v3.POST("user/authn/finish/:username", controllers.FinishLoginAuthn)
}
2019-11-13 04:03:55 -05:00
// 验证码
2019-11-23 03:58:56 -05:00
v3.GET("captcha", controllers.Captcha)
// 站点全局配置
2019-11-23 03:58:56 -05:00
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())
{
2019-11-23 02:37:13 -05:00
// 用户
2019-11-23 03:58:56 -05:00
user := auth.Group("user")
2019-11-12 02:34:54 -05:00
{
// 当前登录用户信息
2019-11-23 03:58:56 -05:00
user.GET("me", controllers.UserMe)
user.GET("storage", controllers.UserStorage)
2019-12-08 09:17:36 -05:00
// WebAuthn 注册相关
authn := user.Group("authn")
{
authn.PUT("", controllers.StartRegAuthn)
authn.PUT("finish", controllers.FinishRegAuthn)
}
2019-11-12 02:34:54 -05:00
}
2019-11-15 05:12:34 -05:00
// 文件
2019-11-23 03:58:56 -05:00
file := auth.Group("file")
2019-11-15 05:12:34 -05:00
{
2019-11-22 01:56:46 -05:00
// 文件上传
2019-11-23 03:58:56 -05:00
file.POST("upload", controllers.FileUploadStream)
// 下载文件
2019-12-08 09:17:36 -05:00
file.GET("download/*path", controllers.Download)
// 下载文件
file.GET("thumb/:id", controllers.Thumb)
// 取得文件外链
file.GET("source/:id", controllers.GetSource)
2019-11-15 05:12:34 -05:00
}
2019-11-23 02:37:13 -05:00
// 目录
2019-11-23 03:58:56 -05:00
directory := auth.Group("directory")
2019-11-23 02:37:13 -05:00
{
// 创建目录
directory.PUT("", controllers.CreateDirectory)
2019-11-24 00:06:15 -05:00
// 列出目录下内容
2019-11-30 02:09:56 -05:00
directory.GET("*path", controllers.ListDirectory)
}
// 对象,文件和目录的抽象
object := auth.Group("object")
{
2019-12-01 05:31:29 -05:00
// 删除对象
2019-11-30 02:09:56 -05:00
object.DELETE("", controllers.Delete)
2019-12-03 01:20:23 -05:00
// 移动对象
2019-12-01 05:31:29 -05:00
object.PATCH("", controllers.Move)
2019-12-03 01:20:23 -05:00
// 复制对象
object.POST("copy", controllers.Copy)
2019-12-07 02:54:07 -05:00
// 重命名对象
object.POST("rename", controllers.Rename)
2019-11-23 02:37:13 -05:00
}
2019-11-12 02:34:54 -05:00
}
2019-11-04 23:31:22 -05:00
}
return r
}