teldrive/api/router.go

75 lines
2.1 KiB
Go
Raw Normal View History

2023-12-03 03:47:23 +08:00
package api
import (
"time"
2023-12-03 03:47:23 +08:00
"github.com/divyam234/teldrive/pkg/controller"
"github.com/divyam234/teldrive/pkg/middleware"
"github.com/divyam234/teldrive/ui"
ginzap "github.com/gin-contrib/zap"
2023-12-03 03:47:23 +08:00
"github.com/gin-gonic/gin"
"go.uber.org/zap"
2023-12-03 03:47:23 +08:00
)
func InitRouter(logger *zap.Logger) *gin.Engine {
r := gin.New()
2023-12-03 03:47:23 +08:00
r.Use(ginzap.Ginzap(logger, time.RFC3339, true))
2023-12-03 03:47:23 +08:00
r.Use(ginzap.RecoveryWithZap(logger, true))
2023-12-08 05:46:06 +08:00
2023-12-03 03:47:23 +08:00
r.Use(middleware.Cors())
c := controller.NewController(logger)
2023-12-03 03:47:23 +08:00
api := r.Group("/api")
{
auth := api.Group("/auth")
{
auth.GET("/session", c.GetSession)
auth.POST("/login", c.LogIn)
auth.POST("/logout", middleware.Authmiddleware, c.Logout)
auth.GET("/ws", c.HandleMultipleLogin)
}
files := api.Group("/files")
{
files.GET("", middleware.Authmiddleware, c.ListFiles)
files.POST("", middleware.Authmiddleware, c.CreateFile)
files.GET(":fileID", middleware.Authmiddleware, c.GetFileByID)
files.PATCH(":fileID", middleware.Authmiddleware, c.UpdateFile)
files.HEAD(":fileID/stream/:fileName", c.GetFileStream)
files.GET(":fileID/stream/:fileName", c.GetFileStream)
files.POST("/move", middleware.Authmiddleware, c.MoveFiles)
files.POST("/directories", middleware.Authmiddleware, c.MakeDirectory)
files.POST("/delete", middleware.Authmiddleware, c.DeleteFiles)
files.POST("/copy", middleware.Authmiddleware, c.CopyFile)
files.POST("/directories/move", middleware.Authmiddleware, c.MoveDirectory)
}
uploads := api.Group("/uploads")
{
uploads.Use(middleware.Authmiddleware)
uploads.POST("/parts", c.CreateUploadPart)
uploads.GET(":id", c.GetUploadFileById)
uploads.POST(":id", c.UploadFile)
uploads.DELETE(":id", c.DeleteUploadFile)
}
users := api.Group("/users")
{
2023-12-03 05:23:06 +08:00
users.Use(middleware.Authmiddleware)
2023-12-03 03:47:23 +08:00
users.GET("/profile", c.GetProfilePhoto)
2023-12-03 05:46:53 +08:00
users.GET("/stats", c.GetStats)
2023-12-03 03:47:23 +08:00
users.GET("/bots", c.GetBots)
users.GET("/channels", c.ListChannels)
users.PATCH("/channels", c.UpdateChannel)
users.POST("/bots", c.AddBots)
users.DELETE("/bots", c.RemoveBots)
}
}
2023-12-03 03:51:21 +08:00
2023-12-03 03:47:23 +08:00
ui.AddRoutes(r)
return r
}