Cloudreve/routers/router.go

209 lines
5.4 KiB
Go
Raw Normal View History

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/pkg/util"
2019-11-16 16:11:37 +08:00
"github.com/HFO4/cloudreve/routers/controllers"
2019-12-13 19:00:58 +08:00
"github.com/gin-contrib/cors"
2019-11-05 12:31:22 +08:00
"github.com/gin-gonic/gin"
)
// InitRouter 初始化路由
func InitRouter() *gin.Engine {
if conf.SystemConfig.Mode == "master" {
util.Log().Info("当前运行模式Master")
return InitMasterRouter()
2019-12-16 19:43:00 +08:00
}
util.Log().Info("当前运行模式Slave")
return InitSlaveRouter()
2019-12-16 19:43:00 +08:00
}
// InitSlaveRouter 初始化从机模式路由
func InitSlaveRouter() *gin.Engine {
2019-11-14 14:24:39 +08:00
r := gin.Default()
v3 := r.Group("/api/v3/slave")
// 跨域相关
InitCORS(v3)
// 鉴权中间件
v3.Use(middleware.SignRequired())
2019-11-12 16:53:32 +08:00
/*
路由
2019-11-12 16:53:32 +08:00
*/
{
v3.POST("upload", controllers.SlaveUpload)
}
return r
}
2019-11-12 16:53:32 +08:00
// InitCORS 初始化跨域配置
func InitCORS(group *gin.RouterGroup) {
2019-12-13 19:00:58 +08:00
if conf.CORSConfig.AllowOrigins[0] != "UNSET" || conf.CORSConfig.AllowAllOrigins {
group.Use(cors.New(cors.Config{
2019-12-13 19:00:58 +08:00
AllowOrigins: conf.CORSConfig.AllowOrigins,
AllowAllOrigins: conf.CORSConfig.AllowAllOrigins,
AllowMethods: conf.CORSConfig.AllowHeaders,
AllowHeaders: conf.CORSConfig.AllowHeaders,
AllowCredentials: conf.CORSConfig.AllowCredentials,
ExposeHeaders: conf.CORSConfig.ExposeHeaders,
}))
return
2019-12-13 19:00:58 +08:00
}
2019-11-23 15:09:46 +08:00
// slave模式下未启动跨域的警告
if conf.SystemConfig.Mode == "slave" {
util.Log().Warning("当前作为存储端Slave运行但未启用跨域配置可能会导致 Master 端无法正常上传文件")
}
}
// InitMasterRouter 初始化主机模式路由
func InitMasterRouter() *gin.Engine {
r := gin.Default()
v3 := r.Group("/api/v3")
/*
中间件
*/
v3.Use(middleware.Session(conf.SystemConfig.SessionSecret))
// 跨域相关
InitCORS(v3)
2019-11-14 14:45:06 +08:00
// 测试模式加入Mock助手中间件
2019-11-12 16:53:32 +08:00
if gin.Mode() == gin.TestMode {
2019-12-16 19:43:00 +08:00
v3.Use(middleware.MockHelper())
2019-11-12 16:53:32 +08:00
}
2019-12-16 19:43:00 +08:00
v3.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
{
2019-12-10 17:10:34 +08:00
// 全局设置相关
site := v3.Group("site")
{
// 测试用路由
site.GET("ping", controllers.Ping)
// 验证码
site.GET("captcha", controllers.Captcha)
// 站点全局配置
site.GET("config", controllers.SiteConfig)
}
2019-12-10 17:10:34 +08:00
// 用户相关路由
user := v3.Group("user")
{
// 用户登录
2019-12-10 17:10:34 +08:00
user.POST("session", controllers.UserLogin)
// WebAuthn登陆初始化
2019-12-10 17:10:34 +08:00
user.GET("authn/:username", controllers.StartLoginAuthn)
// WebAuthn登陆
2019-12-10 17:10:34 +08:00
user.POST("authn/finish/:username", controllers.FinishLoginAuthn)
}
2019-12-10 17:10:34 +08:00
// 需要携带签名验证的
sign := v3.Group("")
sign.Use(middleware.SignRequired())
{
file := sign.Group("file")
{
// 文件外链
2019-12-10 17:10:34 +08:00
file.GET("get/:id/:name", controllers.AnonymousGetContent)
// 下載已经打包好的文件
file.GET("archive/:id/archive.zip", controllers.DownloadArchive)
// 下载文件
file.GET("download/:id", controllers.Download)
2019-12-10 17:10:34 +08:00
}
}
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)
user.GET("storage", controllers.UserStorage)
2019-12-08 22:17:36 +08:00
// WebAuthn 注册相关
authn := user.Group("authn")
{
authn.PUT("", controllers.StartRegAuthn)
authn.PUT("finish", controllers.FinishRegAuthn)
}
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-12-28 15:50:56 +08:00
// 获取上传凭证
file.GET("upload/credential", controllers.GetUploadCredential)
2019-12-15 14:01:37 +08:00
// 更新文件
file.PUT("update/*path", controllers.PutContent)
// 创建文件下载会话
file.PUT("download/*path", controllers.CreateDownloadSession)
// 预览文件
file.GET("preview/*path", controllers.Preview)
// 取得Office文档预览地址
file.GET("doc/*path", controllers.GetDocPreview)
// 获取缩略图
2019-12-08 22:17:36 +08:00
file.GET("thumb/:id", controllers.Thumb)
// 取得文件外链
file.GET("source/:id", controllers.GetSource)
2019-12-13 15:10:44 +08:00
// 打包要下载的文件
file.POST("archive", controllers.Archive)
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
{
// 创建目录
directory.PUT("", controllers.CreateDirectory)
2019-11-24 13:06:15 +08:00
// 列出目录下内容
2019-11-30 15:09:56 +08:00
directory.GET("*path", controllers.ListDirectory)
}
// 对象,文件和目录的抽象
object := auth.Group("object")
{
2019-12-01 18:31:29 +08:00
// 删除对象
2019-11-30 15:09:56 +08:00
object.DELETE("", controllers.Delete)
2019-12-03 14:20:23 +08:00
// 移动对象
2019-12-01 18:31:29 +08:00
object.PATCH("", controllers.Move)
2019-12-03 14:20:23 +08:00
// 复制对象
object.POST("copy", controllers.Copy)
2019-12-07 15:54:07 +08:00
// 重命名对象
object.POST("rename", controllers.Rename)
2019-11-23 15:37:13 +08:00
}
2019-11-12 15:34:54 +08:00
}
2019-11-05 12:31:22 +08:00
}
2019-12-16 19:43:00 +08:00
// 初始化WebDAV相关路由
initWebDAV(r.Group("dav"))
2019-11-05 12:31:22 +08:00
return r
}
// initWebDAV 初始化WebDAV相关路由
func initWebDAV(group *gin.RouterGroup) {
{
group.Use(middleware.WebDAVAuth())
group.Any("/*path", controllers.ServeWebDAV)
group.Any("", controllers.ServeWebDAV)
group.Handle("PROPFIND", "/*path", controllers.ServeWebDAV)
group.Handle("PROPFIND", "", controllers.ServeWebDAV)
group.Handle("MKCOL", "/*path", controllers.ServeWebDAV)
group.Handle("LOCK", "/*path", controllers.ServeWebDAV)
group.Handle("UNLOCK", "/*path", controllers.ServeWebDAV)
group.Handle("PROPPATCH", "/*path", controllers.ServeWebDAV)
group.Handle("COPY", "/*path", controllers.ServeWebDAV)
group.Handle("MOVE", "/*path", controllers.ServeWebDAV)
}
}