Files
mce/internal/router/router.go
Victor_Jay c3dec09dae feat(settings): 个人资料编辑 + 头像上传/裁切 + 自定义裁切弹窗
**新增功能:**

用户名编辑:输入框替换静态文本,白名单验证(中文/英文/数字/下划线/连字符),前端计数器(n/16),utf8对齐PG VARCHAR,XSS防控。

个性签名编辑:Textarea,128字符上限,实时计数器。

头像上传管线:校验→解码→裁切→CatmullRom缩放→WebP二分编码≤100KB→原子写入(.tmp→os.Rename)。限制5MB,128-3840px,JPEG/PNG/WebP。输出512x512 WebP。文件名 {uid}_{timestamp}.webp。清理旧头像。

自定义裁切弹窗:浅色主题,固定裁切框+图片平移/缩放(1×-3×滚轮),box-shadow遮罩,三等分网格。坐标映射pan/zoom→原图像素→subImage。

CSP修复:img-src允许data:URI(FileReader预览)。

**文件变更:**
修改: README, docs/{development,structure}.md, go.{mod,sum}, cmd/server/main.go, controller/settings, middleware/security, router, templates/settings/{index.html,css}
新增: internal/service/avatar_service.go, docs/settings.md
2026-05-27 01:03:16 +08:00

122 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package router
import (
"net/http"
"metazone.cc/metalab/internal/common"
"metazone.cc/metalab/internal/config"
"metazone.cc/metalab/internal/controller"
adminCtrl "metazone.cc/metalab/internal/controller/admin"
"metazone.cc/metalab/internal/middleware"
"metazone.cc/metalab/internal/model"
"metazone.cc/metalab/internal/repository"
"metazone.cc/metalab/internal/service"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// Setup 注册所有路由
func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
// --- 全局中间件 ---
r.Use(middleware.SecurityHeaders())
// --- 依赖注入 ---
userRepo := repository.NewUserRepo(db)
tokenSvc := service.NewTokenService(cfg, userRepo)
authService := service.NewAuthService(userRepo, tokenSvc, cfg)
rateLimiter := middleware.NewRateLimiter()
authCtrl := controller.NewAuthController(authService, tokenSvc, rateLimiter, cfg)
avatarSvc := service.NewAvatarService(userRepo)
settingsCtrl := controller.NewSettingsController(authService, avatarSvc)
authMdw := middleware.NewAuthMiddleware(cfg, userRepo)
// 管理后台
adminService := service.NewAdminService(userRepo)
adminController := adminCtrl.NewAdminController(adminService)
// --- 页面路由CSRF 仅下发 token不验证——页面 GET 被豁免) ---
pages := r.Group("/")
pages.Use(authMdw.Optional())
pages.Use(middleware.CSRF(cfg))
pages.Use(func(c *gin.Context) {
// 页面路由:确保每个页面都下发 CSRF Cookie
middleware.SetCSRFToken(c, cfg)
c.Next()
})
{
pages.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "home/index.html", common.BuildPageData(c, gin.H{
"Title": "首页",
"ExtraCSS": "/static/css/home.css",
}))
})
// 个人设置页(需登录,/:tab 为伪静态子页面)
pages.GET("/settings", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/settings/profile")
})
pages.GET("/settings/:tab", settingsCtrl.SettingsPage)
}
// 认证页面(已登录自动跳走)
authPages := r.Group("/auth")
authPages.Use(authMdw.Optional())
authPages.Use(middleware.CSRF(cfg))
authPages.Use(func(c *gin.Context) {
middleware.SetCSRFToken(c, cfg)
c.Next()
})
{
authPages.GET("/register", authCtrl.RegisterPage)
authPages.GET("/login", authCtrl.LoginPage)
}
// --- 设置页 API需登录 ---
settingsAPI := r.Group("/api/settings")
settingsAPI.Use(authMdw.Required())
settingsAPI.Use(middleware.CSRF(cfg))
{
settingsAPI.PUT("/profile", settingsCtrl.UpdateProfile)
settingsAPI.POST("/avatar", settingsCtrl.UploadAvatar)
}
// --- 管理后台 SSR 页面(认证失败 302 跳首页) ---
adminPages := r.Group("/admin")
adminPages.Use(authMdw.AdminAuth())
adminPages.Use(middleware.RequirePageRole(model.RoleModerator))
adminPages.Use(middleware.CSRF(cfg))
adminPages.Use(func(c *gin.Context) {
middleware.SetCSRFToken(c, cfg)
c.Next()
})
{
adminPages.GET("/", adminController.Dashboard)
adminPages.GET("/users", adminController.UsersPage,
middleware.RequirePageRole(model.RoleAdmin))
}
// --- 管理后台 APIJSON 响应) ---
adminAPI := r.Group("/api/admin")
adminAPI.Use(authMdw.Required())
adminAPI.Use(middleware.RequireMinRole(model.RoleAdmin))
adminAPI.Use(middleware.CSRF(cfg))
{
adminAPI.GET("/users", adminController.ListUsers)
adminAPI.PUT("/users/:uid/status", adminController.UpdateUserStatus)
adminAPI.POST("/users/:uid/reset-token", adminController.ResetToken)
}
// --- API 路由CSRF 严格验证) ---
api := r.Group("/api")
api.Use(middleware.CSRF(cfg))
{
api.POST("/auth/check-email", authCtrl.CheckEmail)
api.POST("/auth/register", authCtrl.Register)
api.POST("/auth/login", authCtrl.Login)
api.POST("/auth/logout", authCtrl.Logout)
api.POST("/auth/refresh", authCtrl.RefreshToken)
}
}