chore: move server package to root

refactor/fs
Noah Hsu 2022-06-26 19:10:14 +08:00
parent 4cef3adc90
commit c67f128f15
9 changed files with 39 additions and 39 deletions

View File

@ -4,7 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/server" "github.com/alist-org/alist/v3/server"
"os" "os"
"github.com/alist-org/alist/v3/bootstrap" "github.com/alist-org/alist/v3/bootstrap"

View File

@ -4,7 +4,7 @@ import (
"github.com/Xhofe/go-cache" "github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/server/common" common2 "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"time" "time"
) )
@ -25,34 +25,34 @@ func Login(c *gin.Context) {
ip := c.ClientIP() ip := c.ClientIP()
count, ok := loginCache.Get(ip) count, ok := loginCache.Get(ip)
if ok && count >= defaultTimes { if ok && count >= defaultTimes {
common.ErrorStrResp(c, "Too many unsuccessful sign-in attempts have been made using an incorrect password. Try again later.", 403) common2.ErrorStrResp(c, "Too many unsuccessful sign-in attempts have been made using an incorrect password. Try again later.", 403)
loginCache.Expire(ip, defaultDuration) loginCache.Expire(ip, defaultDuration)
return return
} }
// check username // check username
var req LoginReq var req LoginReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
return return
} }
user, err := db.GetUserByName(req.Username) user, err := db.GetUserByName(req.Username)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
return return
} }
// validate password // validate password
if err := user.ValidatePassword(req.Password); err != nil { if err := user.ValidatePassword(req.Password); err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
loginCache.Set(ip, count+1) loginCache.Set(ip, count+1)
return return
} }
// generate token // generate token
token, err := common.GenerateToken(user.Username) token, err := common2.GenerateToken(user.Username)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
return return
} }
common.SuccessResp(c, gin.H{"token": token}) common2.SuccessResp(c, gin.H{"token": token})
loginCache.Del(ip) loginCache.Del(ip)
} }
@ -61,5 +61,5 @@ func Login(c *gin.Context) {
func CurrentUser(c *gin.Context) { func CurrentUser(c *gin.Context) {
user := c.MustGet("user").(*model.User) user := c.MustGet("user").(*model.User)
user.Password = "" user.Password = ""
common.SuccessResp(c, gin.H{"user": user}) common2.SuccessResp(c, gin.H{"user": user})
} }

View File

@ -3,26 +3,26 @@ package controllers
import ( import (
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/server/common"
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
common2 "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv" "strconv"
) )
func ListMetas(c *gin.Context) { func ListMetas(c *gin.Context) {
var req common.PageReq var req common2.PageReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
return return
} }
log.Debugf("%+v", req) log.Debugf("%+v", req)
metas, total, err := db.GetMetas(req.PageIndex, req.PageSize) metas, total, err := db.GetMetas(req.PageIndex, req.PageSize)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common2.ErrorResp(c, err, 500, true)
return return
} }
common.SuccessResp(c, common.PageResp{ common2.SuccessResp(c, common2.PageResp{
Content: metas, Content: metas,
Total: total, Total: total,
}) })
@ -31,28 +31,28 @@ func ListMetas(c *gin.Context) {
func CreateMeta(c *gin.Context) { func CreateMeta(c *gin.Context) {
var req model.Meta var req model.Meta
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
return return
} }
req.Path = utils.StandardizePath(req.Path) req.Path = utils.StandardizePath(req.Path)
if err := db.CreateMeta(&req); err != nil { if err := db.CreateMeta(&req); err != nil {
common.ErrorResp(c, err, 500) common2.ErrorResp(c, err, 500)
} else { } else {
common.SuccessResp(c) common2.SuccessResp(c)
} }
} }
func UpdateMeta(c *gin.Context) { func UpdateMeta(c *gin.Context) {
var req model.Meta var req model.Meta
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
return return
} }
req.Path = utils.StandardizePath(req.Path) req.Path = utils.StandardizePath(req.Path)
if err := db.UpdateMeta(&req); err != nil { if err := db.UpdateMeta(&req); err != nil {
common.ErrorResp(c, err, 500) common2.ErrorResp(c, err, 500)
} else { } else {
common.SuccessResp(c) common2.SuccessResp(c)
} }
} }
@ -60,12 +60,12 @@ func DeleteMeta(c *gin.Context) {
idStr := c.Query("id") idStr := c.Query("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400) common2.ErrorResp(c, err, 400)
return return
} }
if err := db.DeleteMetaById(uint(id)); err != nil { if err := db.DeleteMetaById(uint(id)); err != nil {
common.ErrorResp(c, err, 500) common2.ErrorResp(c, err, 500)
return return
} }
common.SuccessResp(c) common2.SuccessResp(c)
} }

View File

@ -3,7 +3,7 @@ package middlewares
import ( import (
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/server/common" common2 "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -14,7 +14,7 @@ func Auth(c *gin.Context) {
if token == "" { if token == "" {
guest, err := db.GetGuest() guest, err := db.GetGuest()
if err != nil { if err != nil {
common.ErrorResp(c, err, 500, true) common2.ErrorResp(c, err, 500, true)
c.Abort() c.Abort()
return return
} }
@ -22,15 +22,15 @@ func Auth(c *gin.Context) {
c.Next() c.Next()
return return
} }
userClaims, err := common.ParseToken(token) userClaims, err := common2.ParseToken(token)
if err != nil { if err != nil {
common.ErrorResp(c, err, 401) common2.ErrorResp(c, err, 401)
c.Abort() c.Abort()
return return
} }
user, err := db.GetUserByName(userClaims.Username) user, err := db.GetUserByName(userClaims.Username)
if err != nil { if err != nil {
common.ErrorResp(c, err, 401) common2.ErrorResp(c, err, 401)
c.Abort() c.Abort()
return return
} }
@ -41,7 +41,7 @@ func Auth(c *gin.Context) {
func AuthAdmin(c *gin.Context) { func AuthAdmin(c *gin.Context) {
user := c.MustGet("user").(*model.User) user := c.MustGet("user").(*model.User)
if !user.IsAdmin() { if !user.IsAdmin() {
common.ErrorStrResp(c, "You are not an admin", 403) common2.ErrorStrResp(c, "You are not an admin", 403)
c.Abort() c.Abort()
} else { } else {
c.Next() c.Next()

View File

@ -2,9 +2,9 @@ package server
import ( import (
"github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/alist-org/alist/v3/internal/server/controllers" controllers2 "github.com/alist-org/alist/v3/server/controllers"
"github.com/alist-org/alist/v3/internal/server/middlewares" "github.com/alist-org/alist/v3/server/middlewares"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -14,16 +14,16 @@ func Init(r *gin.Engine) {
Cors(r) Cors(r)
api := r.Group("/api", middlewares.Auth) api := r.Group("/api", middlewares.Auth)
api.POST("/auth/login", controllers.Login) api.POST("/auth/login", controllers2.Login)
api.GET("/auth/current", controllers.CurrentUser) api.GET("/auth/current", controllers2.CurrentUser)
admin := api.Group("/admin", middlewares.AuthAdmin) admin := api.Group("/admin", middlewares.AuthAdmin)
meta := admin.Group("/meta") meta := admin.Group("/meta")
meta.GET("/list", controllers.ListMetas) meta.GET("/list", controllers2.ListMetas)
meta.POST("/create", controllers.CreateMeta) meta.POST("/create", controllers2.CreateMeta)
meta.POST("/update", controllers.UpdateMeta) meta.POST("/update", controllers2.UpdateMeta)
meta.POST("/delete", controllers.DeleteMeta) meta.POST("/delete", controllers2.DeleteMeta)
} }
func Cors(r *gin.Engine) { func Cors(r *gin.Engine) {