refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion. - Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects. - Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management. - Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering. - Improve database initialization by moving it from `internal/model` to `internal/database`. - Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool. - Enhance error handling and security by implementing fail-fast checks for encryption key length during startup. - Clean up unused code, including the `avatar` package and several unused DTOs.
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
package middleware
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"with_you/internal/model"
|
||||
"with_you/internal/pkg/response"
|
||||
"with_you/internal/repository"
|
||||
"with_you/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RequireVerified(userRepo repository.UserRepository) gin.HandlerFunc {
|
||||
// RequireVerified 要求用户已通过身份认证。
|
||||
// 通过 service.UserService 查询用户(而非直接访问 repository),保持分层一致:
|
||||
//
|
||||
// middleware → service → repository
|
||||
func RequireVerified(userService service.UserService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
@@ -19,7 +23,7 @@ func RequireVerified(userRepo repository.UserRepository) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := userRepo.GetByID(userID.(string))
|
||||
user, err := userService.GetUserByID(c.Request.Context(), userID.(string))
|
||||
if err != nil {
|
||||
response.InternalServerError(c, "获取用户信息失败")
|
||||
c.Abort()
|
||||
|
||||
Reference in New Issue
Block a user