-
Notifications
You must be signed in to change notification settings - Fork 14
/
handler.go
133 lines (121 loc) · 3.83 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package handler
import (
"github.com/gin-gonic/gin"
"github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
"github.com/wq1019/cloud_disk/handler/middleware"
"github.com/wq1019/cloud_disk/server"
"net/http"
"strconv"
)
func CreateHTTPHandler(s *server.Server) http.Handler {
authHandler := NewAuthHandler()
meHandler := NewMeHandler(s.ImageUrl)
userHandler := NewUserHandler(s.ImageUrl)
uploadFileHandler := NewUploadFileHandler(s.FileUploader)
uploadImageHandler := NewUploadImage(s.ImageUploader, s.ImageUrl)
folderHandler := NewFolderHandler(s.NosClient, s.BucketName)
fileHandler := NewFileHandler()
downloadHandler := NewDownloadHandler(s.FileUploader)
groupHandler := NewGroupHandler()
if s.Debug {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
router.Use(middleware.Gorm(s.DB))
router.Use(middleware.Service(s.Service))
router.Use(middleware.NewHandleErrorMiddleware(s.Conf.ServiceName))
api := router.Group("/api")
authRouter := api.Group("/auth")
authRouter.POST("/register", authHandler.Register)
authRouter.POST("/login", authHandler.Login)
authorized := api.Group("/")
authorized.Use(middleware.AuthMiddleware)
{
// 显示我的基本信息
authorized.GET("/auth/me", meHandler.Show)
// 更新我的基本信息
authorized.PUT("/auth/me", meHandler.UpdateInfo)
// 退出登录
authorized.GET("/auth/logout", authHandler.Logout)
// 上传文件
authorized.POST("/upload_file", uploadFileHandler.UploadChunk)
// 上传图片
authorized.POST("/upload_image", uploadImageHandler.UploadImage)
// 指定目录下第一层的资源列表
authorized.GET("/folder", folderHandler.LoadFolder)
// 创建目录
authorized.POST("/folder", folderHandler.CreateFolder)
// 删除文件和目录资源
authorized.DELETE("/source", folderHandler.DeleteSource)
// 移动到指定目录
authorized.PUT("/source/move", folderHandler.Move2Folder)
// 复制到指定目录
authorized.PUT("/source/copy", folderHandler.Copy2Folder)
// 重命名文件
authorized.PUT("/file/rename", fileHandler.RenameFile)
// 重命名目录
authorized.PUT("/folder/rename", folderHandler.RenameFolder)
// 文件下载
authorized.GET("/download", downloadHandler.Download)
// 获取文件分享链接
authorized.GET("/share_link", downloadHandler.GetShareLink)
// 获取要下载的文件和目录的详细信息
authorized.GET("/pre_download", downloadHandler.PreDownload)
}
adminRouter := api.Group("/admin")
adminRouter.Use(middleware.AuthMiddleware, middleware.AdminMiddleware)
{
// 用户列表
adminRouter.GET("/user", userHandler.UserList)
// 更新用户的禁用状态
adminRouter.PUT("/user/:id/ban_status", userHandler.UpdateBanStatus)
// 组列表
adminRouter.GET("/group", groupHandler.GroupList)
// 创建组
adminRouter.POST("/group", groupHandler.GroupCreate)
// 更新组
adminRouter.PUT("/group/:id", groupHandler.GroupUpdate)
// 删除指定组
adminRouter.DELETE("/group/:id", groupHandler.GroupDelete)
}
// 文档
api.GET("/doc/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
return router
}
func getInt32LimitAndOffset(c *gin.Context) (limit, offset int32) {
var err error
limitI64, err := strconv.ParseInt(c.Query("limit"), 10, 32)
if err != nil {
limit = 10
} else {
limit = int32(limitI64)
}
if limit > 50 {
limit = 50
}
offsetI64, err := strconv.ParseInt(c.Query("offset"), 10, 32)
if err != nil {
offset = 0
} else {
offset = int32(offsetI64)
}
return limit, offset
}
func getInt64LimitAndOffset(c *gin.Context) (limit, offset int64) {
var err error
limit, err = strconv.ParseInt(c.Query("limit"), 10, 32)
if err != nil {
limit = 10
}
if limit > 50 {
limit = 50
}
offset, err = strconv.ParseInt(c.Query("offset"), 10, 32)
if err != nil {
offset = 0
}
return limit, offset
}