alist/server/router.go

135 lines
4.4 KiB
Go
Raw Normal View History

2022-06-06 14:08:39 +00:00
package server
2022-06-25 12:38:02 +00:00
import (
2022-08-07 05:09:59 +00:00
"github.com/alist-org/alist/v3/cmd/flags"
2022-06-25 13:34:44 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-07-01 08:53:01 +00:00
"github.com/alist-org/alist/v3/internal/message"
2022-06-26 11:10:14 +00:00
"github.com/alist-org/alist/v3/server/common"
2022-07-11 09:12:50 +00:00
"github.com/alist-org/alist/v3/server/handles"
2022-06-26 11:10:14 +00:00
"github.com/alist-org/alist/v3/server/middlewares"
2022-08-28 15:13:03 +00:00
"github.com/alist-org/alist/v3/server/static"
2022-06-25 12:38:02 +00:00
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func Init(r *gin.Engine) {
2022-06-25 13:34:44 +00:00
common.SecretKey = []byte(conf.Conf.JwtSecret)
2022-06-25 12:38:02 +00:00
Cors(r)
2022-08-11 13:32:33 +00:00
r.Use(middlewares.StoragesLoaded)
if conf.Conf.MaxConnections > 0 {
r.Use(middlewares.MaxAllowed(conf.Conf.MaxConnections))
}
2022-09-06 06:39:21 +00:00
WebDav(r.Group("/dav"))
2022-06-25 13:34:44 +00:00
2022-08-28 07:23:00 +00:00
r.GET("/favicon.ico", handles.Favicon)
r.GET("/i/:link_name", handles.Plist)
2022-07-11 09:12:50 +00:00
r.GET("/d/*path", middlewares.Down, handles.Down)
r.GET("/p/*path", middlewares.Down, handles.Proxy)
2022-06-28 10:00:11 +00:00
2022-07-23 12:42:12 +00:00
api := r.Group("/api")
auth := api.Group("", middlewares.Auth)
2022-07-07 06:19:24 +00:00
2022-07-23 12:42:12 +00:00
api.POST("/auth/login", handles.Login)
auth.GET("/me", handles.CurrentUser)
auth.POST("/me/update", handles.UpdateCurrent)
2022-08-05 17:22:13 +00:00
auth.POST("/auth/2fa/generate", handles.Generate2FA)
auth.POST("/auth/2fa/verify", handles.Verify2FA)
// github auth
api.GET("/auth/github", handles.GithubLoginRedirect)
api.GET("/auth/github_callback", handles.GithubLoginCallback)
2022-06-26 11:09:28 +00:00
2022-07-23 12:42:12 +00:00
// no need auth
public := api.Group("/public")
public.Any("/settings", handles.PublicSettings)
2022-08-28 15:13:03 +00:00
_fs(auth.Group("/fs"))
2022-07-23 12:42:12 +00:00
admin(auth.Group("/admin", middlewares.AuthAdmin))
2022-08-07 05:09:59 +00:00
if flags.Dev {
2022-08-02 14:16:58 +00:00
dev(r.Group("/dev"))
}
2022-08-28 15:13:03 +00:00
static.Static(r)
2022-07-23 12:42:12 +00:00
}
2022-06-26 11:09:28 +00:00
2022-07-23 12:42:12 +00:00
func admin(g *gin.RouterGroup) {
meta := g.Group("/meta")
2022-07-11 09:12:50 +00:00
meta.GET("/list", handles.ListMetas)
2022-07-27 09:41:25 +00:00
meta.GET("/get", handles.GetMeta)
2022-07-11 09:12:50 +00:00
meta.POST("/create", handles.CreateMeta)
meta.POST("/update", handles.UpdateMeta)
meta.POST("/delete", handles.DeleteMeta)
2022-06-26 11:36:27 +00:00
2022-07-23 12:42:12 +00:00
user := g.Group("/user")
2022-07-11 09:12:50 +00:00
user.GET("/list", handles.ListUsers)
2022-07-27 09:41:25 +00:00
user.GET("/get", handles.GetUser)
2022-07-11 09:12:50 +00:00
user.POST("/create", handles.CreateUser)
user.POST("/update", handles.UpdateUser)
2022-08-07 03:59:33 +00:00
user.POST("/cancel_2fa", handles.Cancel2FAById)
2022-07-11 09:12:50 +00:00
user.POST("/delete", handles.DeleteUser)
2022-06-26 12:00:36 +00:00
2022-07-23 12:42:12 +00:00
storage := g.Group("/storage")
2022-07-11 09:12:50 +00:00
storage.GET("/list", handles.ListStorages)
2022-07-18 15:02:14 +00:00
storage.GET("/get", handles.GetStorage)
2022-07-11 09:12:50 +00:00
storage.POST("/create", handles.CreateStorage)
storage.POST("/update", handles.UpdateStorage)
storage.POST("/delete", handles.DeleteStorage)
storage.POST("/enable", handles.EnableStorage)
storage.POST("/disable", handles.DisableStorage)
storage.POST("/load_all", handles.LoadAllStorages)
2022-06-26 12:25:02 +00:00
2022-07-23 12:42:12 +00:00
driver := g.Group("/driver")
2022-08-30 06:39:10 +00:00
driver.GET("/list", handles.ListDriverInfo)
2022-07-11 09:12:50 +00:00
driver.GET("/names", handles.ListDriverNames)
2022-08-30 06:39:10 +00:00
driver.GET("/info", handles.GetDriverInfo)
2022-06-27 09:06:10 +00:00
2022-07-23 12:42:12 +00:00
setting := g.Group("/setting")
2022-07-11 09:12:50 +00:00
setting.GET("/get", handles.GetSetting)
setting.GET("/list", handles.ListSettings)
setting.POST("/save", handles.SaveSettings)
setting.POST("/delete", handles.DeleteSetting)
setting.POST("/reset_token", handles.ResetToken)
setting.POST("/set_aria2", handles.SetAria2)
setting.POST("/set_qbittorrent", handles.SetQbittorrent)
2022-06-27 09:06:10 +00:00
2022-07-23 12:42:12 +00:00
task := g.Group("/task")
handles.SetupTaskRoute(task)
2022-06-29 10:36:14 +00:00
2022-07-23 12:42:12 +00:00
ms := g.Group("/message")
2022-08-13 19:05:30 +00:00
ms.POST("/get", message.HttpInstance.GetHandle)
ms.POST("/send", message.HttpInstance.SendHandle)
index := g.Group("/index")
index.POST("/build", middlewares.SearchIndex, handles.BuildIndex)
2022-12-24 12:23:04 +00:00
index.POST("/update", middlewares.SearchIndex, handles.UpdateIndex)
index.POST("/stop", middlewares.SearchIndex, handles.StopIndex)
index.POST("/clear", middlewares.SearchIndex, handles.ClearIndex)
index.GET("/progress", middlewares.SearchIndex, handles.GetProgress)
2022-07-23 12:42:12 +00:00
}
2022-07-01 08:53:01 +00:00
2022-08-28 15:13:03 +00:00
func _fs(g *gin.RouterGroup) {
2022-07-23 12:42:12 +00:00
g.Any("/list", handles.FsList)
g.Any("/search", middlewares.SearchIndex, handles.Search)
2022-07-23 12:42:12 +00:00
g.Any("/get", handles.FsGet)
g.Any("/other", handles.FsOther)
2022-07-23 12:42:12 +00:00
g.Any("/dirs", handles.FsDirs)
g.POST("/mkdir", handles.FsMkdir)
g.POST("/rename", handles.FsRename)
g.POST("/move", handles.FsMove)
g.POST("/copy", handles.FsCopy)
g.POST("/remove", handles.FsRemove)
g.PUT("/put", middlewares.FsUp, handles.FsStream)
g.PUT("/form", middlewares.FsUp, handles.FsForm)
2022-07-23 12:42:12 +00:00
g.POST("/link", middlewares.AuthAdmin, handles.Link)
g.POST("/add_aria2", handles.AddAria2)
g.POST("/add_qbit", handles.AddQbittorrent)
2022-06-25 12:38:02 +00:00
}
func Cors(r *gin.Engine) {
config := cors.DefaultConfig()
config.AllowAllOrigins = true
//config.AllowHeaders = append(config.AllowHeaders, "Authorization", "range", "File-Path", "As-Task", "Password")
config.AllowHeaders = []string{"*"}
config.AllowMethods = []string{"*"}
2022-06-25 12:38:02 +00:00
r.Use(cors.New(config))
}