Skip to content

Commit b50fd43

Browse files
committed
重构:按功能模块拆分路由文件
## 主要改进 ### 🏗️ 路由结构优化 - 将单一的 routes.go 拆分为按功能模块的多个文件 - 按路径前缀组织路由:base, share, user, chunk, admin - 保持向后兼容性,原有 SetupRoutes 函数依然可用 ### 📁 新增文件结构 - internal/routes/base.go - 基础路由(首页、健康检查) - internal/routes/share.go - 分享相关路由 (/share/*) - internal/routes/user.go - 用户相关路由 (/user/*) - internal/routes/chunk.go - 分片上传路由 (/chunk/*) - internal/routes/admin.go - 管理员路由 (/admin/*) - internal/routes/setup.go - 路由整合设置 - internal/routes/README.md - 路由结构说明文档 ### ✨ 功能改进 - 模块化设计,每个功能模块独立管理 - 提高代码可维护性和可读性 - 支持按需设置特定模块路由 - 函数名统一为公开(首字母大写) ### 🔄 兼容性 - 保持所有原有路由功能不变 - 中间件和认证逻辑完全保持 - 支持渐进式迁移,可选择使用新接口
1 parent 2a0b682 commit b50fd43

File tree

14 files changed

+494
-235
lines changed

14 files changed

+494
-235
lines changed

internal/routes/README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# 路由结构说明
2+
3+
本项目已将路由按照功能模块拆分到不同的文件中,提高代码的可维护性和可读性。
4+
5+
## 文件结构
6+
7+
```
8+
internal/routes/
9+
├── routes.go # 主路由文件(保持向后兼容)
10+
├── setup.go # 路由整合设置
11+
├── base.go # 基础路由(首页、健康检查、配置获取)
12+
├── share.go # 分享相关路由 (/share/*)
13+
├── user.go # 用户相关路由 (/user/*)
14+
├── chunk.go # 分片上传路由 (/chunk/*)
15+
├── admin.go # 管理员路由 (/admin/*)
16+
└── README.md # 本说明文档
17+
```
18+
19+
## 路由分类
20+
21+
### 1. 基础路由 (base.go)
22+
- `GET /` - 首页
23+
- `POST /` - 获取系统配置
24+
- `GET /health` - 健康检查
25+
- `GET /robots.txt` - robots.txt
26+
27+
### 2. 分享路由 (share.go)
28+
**路径前缀**: `/share`
29+
- `POST /share/text/` - 分享文本
30+
- `POST /share/file/` - 分享文件
31+
- `GET|POST /share/select/` - 获取分享内容
32+
- `GET /share/download` - 下载文件
33+
34+
### 3. 用户路由 (user.go)
35+
**路径前缀**: `/user`
36+
37+
**API 路由**:
38+
- `POST /user/register` - 用户注册
39+
- `POST /user/login` - 用户登录
40+
- `GET /user/system-info` - 获取系统信息
41+
- `POST /user/logout` - 用户登出(需认证)
42+
- `GET /user/profile` - 获取用户资料(需认证)
43+
- `PUT /user/profile` - 更新用户资料(需认证)
44+
- `POST /user/change-password` - 修改密码(需认证)
45+
- `GET /user/files` - 获取用户文件(需认证)
46+
- `GET /user/stats` - 获取用户统计(需认证)
47+
- `GET /user/check-auth` - 检查认证状态(需认证)
48+
- `DELETE /user/files/:id` - 删除用户文件(需认证)
49+
50+
**页面路由**:
51+
- `GET /user/login` - 登录页面
52+
- `GET /user/register` - 注册页面
53+
- `GET /user/dashboard` - 用户仪表板
54+
- `GET /user/forgot-password` - 忘记密码页面
55+
56+
### 4. 分片上传路由 (chunk.go)
57+
**路径前缀**: `/chunk`
58+
- `POST /chunk/upload/init/` - 初始化分片上传
59+
- `POST /chunk/upload/chunk/:upload_id/:chunk_index` - 上传分片
60+
- `POST /chunk/upload/complete/:upload_id` - 完成上传
61+
- `GET /chunk/upload/status/:upload_id` - 获取上传状态
62+
- `POST /chunk/upload/verify/:upload_id/:chunk_index` - 验证分片
63+
- `DELETE /chunk/upload/cancel/:upload_id` - 取消上传
64+
65+
### 5. 管理员路由 (admin.go)
66+
**路径前缀**: `/admin`
67+
68+
**页面路由**:
69+
- `GET /admin/` - 管理页面
70+
71+
**API 路由**:
72+
- `POST /admin/login` - 管理员登录
73+
74+
**需要认证的路由**:
75+
- `GET /admin/dashboard` - 仪表板
76+
- `GET /admin/stats` - 统计信息
77+
- `GET /admin/files` - 文件列表
78+
- `GET /admin/files/:code` - 获取文件信息
79+
- `DELETE /admin/files/:code` - 删除文件
80+
- `PUT /admin/files/:code` - 更新文件
81+
- `GET /admin/files/download` - 下载文件
82+
- `GET /admin/config` - 获取配置
83+
- `PUT /admin/config` - 更新配置
84+
- `POST /admin/clean` - 清理过期文件
85+
86+
**用户管理**:
87+
- `GET /admin/users` - 用户列表
88+
- `GET /admin/users/:id` - 获取用户信息
89+
- `POST /admin/users` - 创建用户
90+
- `PUT /admin/users/:id` - 更新用户
91+
- `DELETE /admin/users/:id` - 删除用户
92+
- `PUT /admin/users/:id/status` - 更新用户状态
93+
- `GET /admin/users/:id/files` - 获取用户文件
94+
95+
**存储管理**:
96+
- `GET /admin/storage` - 获取存储信息
97+
- `POST /admin/storage/switch` - 切换存储
98+
- `GET /admin/storage/test/:type` - 测试存储连接
99+
- `PUT /admin/storage/config` - 更新存储配置
100+
101+
## 使用方式
102+
103+
### 在 main.go 中使用
104+
```go
105+
// 方式1:使用原有接口(向后兼容)
106+
routes.SetupRoutes(router, shareHandler, chunkHandler, adminHandler, storageHandler, userHandler, cfg, userService)
107+
108+
// 方式2:使用新的整合接口
109+
routes.SetupAllRoutes(router, shareHandler, chunkHandler, adminHandler, storageHandler, userHandler, cfg, userService)
110+
111+
// 方式3:按需设置特定模块路由
112+
routes.SetupBaseRoutes(router, cfg)
113+
routes.SetupShareRoutes(router, shareHandler, cfg, userService)
114+
routes.SetupUserRoutes(router, userHandler, cfg, userService)
115+
routes.SetupChunkRoutes(router, chunkHandler, cfg)
116+
routes.SetupAdminRoutes(router, adminHandler, storageHandler, cfg)
117+
```
118+
119+
## 优势
120+
121+
1. **模块化**: 每个功能模块的路由独立管理
122+
2. **可维护性**: 修改特定功能的路由时不影响其他模块
123+
3. **可读性**: 路由定义更加清晰,易于理解
124+
4. **可扩展性**: 新增功能模块时只需添加对应的路由文件
125+
5. **向后兼容**: 保留原有的 SetupRoutes 函数,不影响现有代码
126+
127+
## 注意事项
128+
129+
- 所有函数名已改为公开(首字母大写),便于跨包调用
130+
- 保持了原有的中间件和认证逻辑
131+
- 路由的逻辑和功能完全保持不变,仅做了结构上的拆分

internal/routes/admin.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package routes
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/zy84338719/filecodebox/internal/config"
9+
"github.com/zy84338719/filecodebox/internal/handlers"
10+
"github.com/zy84338719/filecodebox/internal/middleware"
11+
12+
"github.com/gin-gonic/gin"
13+
)
14+
15+
// SetupAdminRoutes 设置管理员相关路由
16+
func SetupAdminRoutes(
17+
router *gin.Engine,
18+
adminHandler *handlers.AdminHandler,
19+
storageHandler *handlers.StorageHandler,
20+
cfg *config.Config,
21+
) {
22+
// 管理相关路由
23+
adminGroup := router.Group("/admin")
24+
{
25+
// 管理页面
26+
adminGroup.GET("/", func(c *gin.Context) {
27+
ServeAdminPage(c, cfg)
28+
})
29+
30+
// 登录不需要认证
31+
adminGroup.POST("/login", adminHandler.Login)
32+
33+
// 需要认证的路由
34+
authGroup := adminGroup.Group("/")
35+
authGroup.Use(middleware.AdminAuth(cfg))
36+
{
37+
authGroup.GET("/dashboard", adminHandler.Dashboard)
38+
authGroup.GET("/stats", adminHandler.GetStats)
39+
authGroup.GET("/files", adminHandler.GetFiles)
40+
authGroup.GET("/files/:code", adminHandler.GetFile)
41+
authGroup.DELETE("/files/:code", adminHandler.DeleteFile)
42+
authGroup.PUT("/files/:code", adminHandler.UpdateFile)
43+
authGroup.GET("/files/download", adminHandler.DownloadFile)
44+
authGroup.GET("/config", adminHandler.GetConfig)
45+
authGroup.PUT("/config", adminHandler.UpdateConfig)
46+
authGroup.POST("/clean", adminHandler.CleanExpiredFiles)
47+
48+
// 用户管理相关路由
49+
authGroup.GET("/users", adminHandler.GetUsers)
50+
authGroup.GET("/users/:id", adminHandler.GetUser)
51+
authGroup.POST("/users", adminHandler.CreateUser)
52+
authGroup.PUT("/users/:id", adminHandler.UpdateUser)
53+
authGroup.DELETE("/users/:id", adminHandler.DeleteUser)
54+
authGroup.PUT("/users/:id/status", adminHandler.UpdateUserStatus)
55+
authGroup.GET("/users/:id/files", adminHandler.GetUserFiles)
56+
57+
// 存储管理相关路由
58+
authGroup.GET("/storage", storageHandler.GetStorageInfo)
59+
authGroup.POST("/storage/switch", storageHandler.SwitchStorage)
60+
authGroup.GET("/storage/test/:type", storageHandler.TestStorageConnection)
61+
authGroup.PUT("/storage/config", storageHandler.UpdateStorageConfig)
62+
}
63+
}
64+
}
65+
66+
// ServeAdminPage 服务管理页面
67+
func ServeAdminPage(c *gin.Context, cfg *config.Config) {
68+
adminPath := filepath.Join(".", cfg.ThemesSelect, "admin.html")
69+
70+
content, err := os.ReadFile(adminPath)
71+
if err != nil {
72+
c.String(http.StatusNotFound, "Admin page not found")
73+
return
74+
}
75+
76+
c.Header("Cache-Control", "no-cache")
77+
c.Header("Content-Type", "text/html; charset=utf-8")
78+
c.String(http.StatusOK, string(content))
79+
}

internal/routes/base.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package routes
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/zy84338719/filecodebox/internal/common"
11+
"github.com/zy84338719/filecodebox/internal/config"
12+
"github.com/zy84338719/filecodebox/internal/handlers"
13+
14+
"github.com/gin-gonic/gin"
15+
)
16+
17+
// SetupBaseRoutes 设置基础路由(首页、健康检查等)
18+
func SetupBaseRoutes(router *gin.Engine, cfg *config.Config) {
19+
// API文档和健康检查
20+
apiHandler := handlers.NewAPIHandler()
21+
router.GET("/health", apiHandler.GetHealth)
22+
23+
// 首页和静态页面
24+
router.GET("/", func(c *gin.Context) {
25+
ServeIndex(c, cfg)
26+
})
27+
28+
router.NoRoute(func(c *gin.Context) {
29+
ServeIndex(c, cfg)
30+
})
31+
32+
// robots.txt
33+
router.GET("/robots.txt", func(c *gin.Context) {
34+
c.Header("Content-Type", "text/plain")
35+
c.String(http.StatusOK, cfg.RobotsText)
36+
})
37+
38+
// 获取配置接口
39+
router.POST("/", func(c *gin.Context) {
40+
common.SuccessResponse(c, gin.H{
41+
"name": cfg.Name,
42+
"description": cfg.Description,
43+
"explain": cfg.PageExplain,
44+
"uploadSize": cfg.UploadSize,
45+
"expireStyle": cfg.ExpireStyle,
46+
"enableChunk": GetEnableChunk(cfg),
47+
"openUpload": cfg.OpenUpload,
48+
"notify_title": cfg.NotifyTitle,
49+
"notify_content": cfg.NotifyContent,
50+
"show_admin_address": cfg.ShowAdminAddr,
51+
"max_save_seconds": cfg.MaxSaveSeconds,
52+
})
53+
})
54+
}
55+
56+
// ServeIndex 服务首页
57+
func ServeIndex(c *gin.Context, cfg *config.Config) {
58+
indexPath := filepath.Join(".", cfg.ThemesSelect, "index.html")
59+
60+
content, err := os.ReadFile(indexPath)
61+
if err != nil {
62+
c.String(http.StatusNotFound, "Index file not found")
63+
return
64+
}
65+
66+
html := string(content)
67+
// 替换模板变量
68+
html = strings.ReplaceAll(html, "{{title}}", cfg.Name)
69+
html = strings.ReplaceAll(html, "{{description}}", cfg.Description)
70+
html = strings.ReplaceAll(html, "{{keywords}}", cfg.Keywords)
71+
html = strings.ReplaceAll(html, "{{page_explain}}", cfg.PageExplain)
72+
html = strings.ReplaceAll(html, "{{opacity}}", fmt.Sprintf("%.1f", cfg.Opacity))
73+
html = strings.ReplaceAll(html, `"/assets/`, `"assets/`)
74+
html = strings.ReplaceAll(html, "{{background}}", cfg.Background)
75+
76+
c.Header("Cache-Control", "no-cache")
77+
c.Header("Content-Type", "text/html; charset=utf-8")
78+
c.String(http.StatusOK, html)
79+
}
80+
81+
// GetEnableChunk 获取分片上传配置
82+
func GetEnableChunk(cfg *config.Config) int {
83+
if cfg.FileStorage == "local" && cfg.EnableChunk == 1 {
84+
return 1
85+
}
86+
return 0
87+
}

internal/routes/chunk.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package routes
2+
3+
import (
4+
"github.com/zy84338719/filecodebox/internal/config"
5+
"github.com/zy84338719/filecodebox/internal/handlers"
6+
"github.com/zy84338719/filecodebox/internal/middleware"
7+
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
// SetupChunkRoutes 设置分片上传相关路由
12+
func SetupChunkRoutes(
13+
router *gin.Engine,
14+
chunkHandler *handlers.ChunkHandler,
15+
cfg *config.Config,
16+
) {
17+
// 分片上传相关路由
18+
chunkGroup := router.Group("/chunk")
19+
chunkGroup.Use(middleware.ShareAuth(cfg))
20+
{
21+
chunkGroup.POST("/upload/init/", chunkHandler.InitChunkUpload)
22+
chunkGroup.POST("/upload/chunk/:upload_id/:chunk_index", chunkHandler.UploadChunk)
23+
chunkGroup.POST("/upload/complete/:upload_id", chunkHandler.CompleteUpload)
24+
25+
// 断点续传相关路由
26+
chunkGroup.GET("/upload/status/:upload_id", chunkHandler.GetUploadStatus)
27+
chunkGroup.POST("/upload/verify/:upload_id/:chunk_index", chunkHandler.VerifyChunk)
28+
chunkGroup.DELETE("/upload/cancel/:upload_id", chunkHandler.CancelUpload)
29+
}
30+
}

0 commit comments

Comments
 (0)