Files
starter/internal/handler/base_handler.go
limitcool b7628c770b Refactor user handler and middleware for improved error handling and logging
- Consolidated user ID retrieval and permission checks into helper functions.
- Updated UserHandler to utilize BaseHandler for common database and configuration access.
- Enhanced logging for user-related operations, including login, registration, and password changes.
- Removed redundant context handling in middleware and improved readability.
- Introduced FileUtil for file URL generation and management, encapsulating file-related logic.
- Refactored FileRepo and UserRepo to streamline database operations and error handling.
- Deleted unused request_id middleware and integrated its functionality into request_logger.
- Removed legacy test runner script to simplify testing process.
2025-06-17 23:09:02 +08:00

31 lines
734 B
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 handler
import (
"github.com/limitcool/starter/configs"
"github.com/limitcool/starter/internal/pkg/logger"
"gorm.io/gorm"
)
// BaseHandler 基础处理器包含所有Handler的公共字段和方法
type BaseHandler struct {
DB *gorm.DB
Config *configs.Config
Helper *HandlerHelper
FileUtil *FileUtil
}
// NewBaseHandler 创建基础处理器
func NewBaseHandler(db *gorm.DB, config *configs.Config) *BaseHandler {
return &BaseHandler{
DB: db,
Config: config,
Helper: NewHandlerHelper(),
FileUtil: NewFileUtil("/uploads"), // 默认基础URL
}
}
// LogInit 记录Handler初始化日志
func (h *BaseHandler) LogInit(handlerName string) {
logger.Info(handlerName + " initialized")
}