f
This commit is contained in:
286
internal/shared/http/response.go
Normal file
286
internal/shared/http/response.go
Normal file
@@ -0,0 +1,286 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"hyapi-server/internal/shared/interfaces"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ResponseBuilder 响应构建器实现
|
||||
type ResponseBuilder struct{}
|
||||
|
||||
// NewResponseBuilder 创建响应构建器
|
||||
func NewResponseBuilder() interfaces.ResponseBuilder {
|
||||
return &ResponseBuilder{}
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
func (r *ResponseBuilder) Success(c *gin.Context, data interface{}, message ...string) {
|
||||
msg := "操作成功"
|
||||
if len(message) > 0 && message[0] != "" {
|
||||
msg = message[0]
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: true,
|
||||
Message: msg,
|
||||
Data: data,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// Created 创建成功响应
|
||||
func (r *ResponseBuilder) Created(c *gin.Context, data interface{}, message ...string) {
|
||||
msg := "创建成功"
|
||||
if len(message) > 0 && message[0] != "" {
|
||||
msg = message[0]
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: true,
|
||||
Message: msg,
|
||||
Data: data,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
// Error 错误响应
|
||||
func (r *ResponseBuilder) Error(c *gin.Context, err error) {
|
||||
// 根据错误类型确定状态码
|
||||
statusCode := http.StatusInternalServerError
|
||||
message := "服务器内部错误"
|
||||
errorDetail := err.Error()
|
||||
|
||||
// 这里可以根据不同的错误类型设置不同的状态码
|
||||
// 例如:ValidationError -> 400, NotFoundError -> 404, etc.
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: message,
|
||||
Errors: errorDetail,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(statusCode, response)
|
||||
}
|
||||
|
||||
// BadRequest 400错误响应
|
||||
func (r *ResponseBuilder) BadRequest(c *gin.Context, message string, errors ...interface{}) {
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: message,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
response.Errors = errors[0]
|
||||
}
|
||||
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
}
|
||||
|
||||
// Unauthorized 401错误响应
|
||||
func (r *ResponseBuilder) Unauthorized(c *gin.Context, message ...string) {
|
||||
msg := "用户未登录或认证已过期"
|
||||
if len(message) > 0 && message[0] != "" {
|
||||
msg = message[0]
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: msg,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusUnauthorized, response)
|
||||
}
|
||||
|
||||
// Forbidden 403错误响应
|
||||
func (r *ResponseBuilder) Forbidden(c *gin.Context, message ...string) {
|
||||
msg := "权限不足,无法访问此资源"
|
||||
if len(message) > 0 && message[0] != "" {
|
||||
msg = message[0]
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: msg,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusForbidden, response)
|
||||
}
|
||||
|
||||
// NotFound 404错误响应
|
||||
func (r *ResponseBuilder) NotFound(c *gin.Context, message ...string) {
|
||||
msg := "请求的资源不存在"
|
||||
if len(message) > 0 && message[0] != "" {
|
||||
msg = message[0]
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: msg,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNotFound, response)
|
||||
}
|
||||
|
||||
// Conflict 409错误响应
|
||||
func (r *ResponseBuilder) Conflict(c *gin.Context, message string) {
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: message,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusConflict, response)
|
||||
}
|
||||
|
||||
// InternalError 500错误响应
|
||||
func (r *ResponseBuilder) InternalError(c *gin.Context, message ...string) {
|
||||
msg := "服务器内部错误"
|
||||
if len(message) > 0 && message[0] != "" {
|
||||
msg = message[0]
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: msg,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusInternalServerError, response)
|
||||
}
|
||||
|
||||
// Paginated 分页响应
|
||||
func (r *ResponseBuilder) Paginated(c *gin.Context, data interface{}, pagination interfaces.PaginationMeta) {
|
||||
response := interfaces.APIResponse{
|
||||
Success: true,
|
||||
Message: "查询成功",
|
||||
Data: data,
|
||||
Pagination: &pagination,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// getRequestID 从上下文获取请求ID
|
||||
func (r *ResponseBuilder) getRequestID(c *gin.Context) string {
|
||||
if requestID, exists := c.Get("request_id"); exists {
|
||||
if id, ok := requestID.(string); ok {
|
||||
return id
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// BuildPagination 构建分页元数据
|
||||
func BuildPagination(page, pageSize int, total int64) interfaces.PaginationMeta {
|
||||
totalPages := int(math.Ceil(float64(total) / float64(pageSize)))
|
||||
|
||||
if totalPages < 1 {
|
||||
totalPages = 1
|
||||
}
|
||||
|
||||
return interfaces.PaginationMeta{
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
Total: total,
|
||||
TotalPages: totalPages,
|
||||
HasNext: page < totalPages,
|
||||
HasPrev: page > 1,
|
||||
}
|
||||
}
|
||||
|
||||
// CustomResponse 自定义响应
|
||||
func (r *ResponseBuilder) CustomResponse(c *gin.Context, statusCode int, data interface{}) {
|
||||
var message string
|
||||
switch statusCode {
|
||||
case http.StatusOK:
|
||||
message = "请求成功"
|
||||
case http.StatusCreated:
|
||||
message = "创建成功"
|
||||
case http.StatusNoContent:
|
||||
message = "无内容"
|
||||
case http.StatusBadRequest:
|
||||
message = "请求参数错误"
|
||||
case http.StatusUnauthorized:
|
||||
message = "认证失败"
|
||||
case http.StatusForbidden:
|
||||
message = "权限不足"
|
||||
case http.StatusNotFound:
|
||||
message = "资源不存在"
|
||||
case http.StatusConflict:
|
||||
message = "资源冲突"
|
||||
case http.StatusTooManyRequests:
|
||||
message = "请求过于频繁"
|
||||
case http.StatusInternalServerError:
|
||||
message = "服务器内部错误"
|
||||
default:
|
||||
message = "未知状态"
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: statusCode >= 200 && statusCode < 300,
|
||||
Message: message,
|
||||
Data: data,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(statusCode, response)
|
||||
}
|
||||
|
||||
// ValidationError 验证错误响应
|
||||
func (r *ResponseBuilder) ValidationError(c *gin.Context, errors interface{}) {
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: "请求参数验证失败",
|
||||
Errors: errors,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusUnprocessableEntity, response)
|
||||
}
|
||||
|
||||
// TooManyRequests 限流错误响应
|
||||
func (r *ResponseBuilder) TooManyRequests(c *gin.Context, message ...string) {
|
||||
msg := "请求过于频繁,请稍后再试"
|
||||
if len(message) > 0 && message[0] != "" {
|
||||
msg = message[0]
|
||||
}
|
||||
|
||||
response := interfaces.APIResponse{
|
||||
Success: false,
|
||||
Message: msg,
|
||||
RequestID: r.getRequestID(c),
|
||||
Timestamp: time.Now().Unix(),
|
||||
Meta: map[string]interface{}{
|
||||
"retry_after": "60s",
|
||||
},
|
||||
}
|
||||
|
||||
c.JSON(http.StatusTooManyRequests, response)
|
||||
}
|
||||
324
internal/shared/http/router.go
Normal file
324
internal/shared/http/router.go
Normal file
@@ -0,0 +1,324 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"hyapi-server/internal/config"
|
||||
"hyapi-server/internal/shared/interfaces"
|
||||
)
|
||||
|
||||
// GinRouter Gin路由器实现
|
||||
type GinRouter struct {
|
||||
engine *gin.Engine
|
||||
config *config.Config
|
||||
logger *zap.Logger
|
||||
middlewares []interfaces.Middleware
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
// NewGinRouter 创建Gin路由器
|
||||
func NewGinRouter(cfg *config.Config, logger *zap.Logger) *GinRouter {
|
||||
// 设置Gin模式
|
||||
if cfg.App.IsProduction() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
} else {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
}
|
||||
|
||||
// 创建Gin引擎
|
||||
engine := gin.New()
|
||||
|
||||
// 加载HTML模板(企业报告等页面)
|
||||
// 为避免生产环境文件不存在导致panic,这里先检查文件是否存在
|
||||
const reportTemplatePath = "resources/qiye.html"
|
||||
if _, err := os.Stat(reportTemplatePath); err == nil {
|
||||
engine.LoadHTMLFiles(reportTemplatePath)
|
||||
logger.Info("已加载企业报告模板文件", zap.String("template", reportTemplatePath))
|
||||
} else {
|
||||
logger.Warn("未找到企业报告模板文件,将跳过模板加载(请确认部署时包含 resources/qiye.html)",
|
||||
zap.String("template", reportTemplatePath),
|
||||
zap.Error(err))
|
||||
}
|
||||
|
||||
return &GinRouter{
|
||||
engine: engine,
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
middlewares: make([]interfaces.Middleware, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterHandler 注册处理器
|
||||
func (r *GinRouter) RegisterHandler(handler interfaces.HTTPHandler) error {
|
||||
// 应用处理器中间件
|
||||
middlewares := handler.GetMiddlewares()
|
||||
|
||||
// 注册路由
|
||||
r.engine.Handle(handler.GetMethod(), handler.GetPath(), append(middlewares, handler.Handle)...)
|
||||
|
||||
r.logger.Info("已注册HTTP处理器",
|
||||
zap.String("method", handler.GetMethod()),
|
||||
zap.String("path", handler.GetPath()))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterMiddleware 注册中间件
|
||||
func (r *GinRouter) RegisterMiddleware(middleware interfaces.Middleware) error {
|
||||
r.middlewares = append(r.middlewares, middleware)
|
||||
|
||||
r.logger.Info("已注册中间件",
|
||||
zap.String("name", middleware.GetName()),
|
||||
zap.Int("priority", middleware.GetPriority()))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterGroup 注册路由组
|
||||
func (r *GinRouter) RegisterGroup(prefix string, middlewares ...gin.HandlerFunc) gin.IRoutes {
|
||||
return r.engine.Group(prefix, middlewares...)
|
||||
}
|
||||
|
||||
// GetRoutes 获取路由信息
|
||||
func (r *GinRouter) GetRoutes() gin.RoutesInfo {
|
||||
return r.engine.Routes()
|
||||
}
|
||||
|
||||
// Start 启动路由器
|
||||
func (r *GinRouter) Start(addr string) error {
|
||||
// 应用中间件(按优先级排序)
|
||||
r.applyMiddlewares()
|
||||
|
||||
// 创建HTTP服务器
|
||||
r.server = &http.Server{
|
||||
Addr: addr,
|
||||
Handler: r.engine,
|
||||
ReadTimeout: r.config.Server.ReadTimeout,
|
||||
WriteTimeout: r.config.Server.WriteTimeout,
|
||||
IdleTimeout: r.config.Server.IdleTimeout,
|
||||
}
|
||||
|
||||
r.logger.Info("正在启动HTTP服务器", zap.String("addr", addr))
|
||||
|
||||
// 启动服务器
|
||||
if err := r.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
r.logger.Error("HTTP服务器启动失败",
|
||||
zap.String("addr", addr),
|
||||
zap.Error(err))
|
||||
return fmt.Errorf("failed to start server: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("HTTP服务器启动成功", zap.String("addr", addr))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop 停止路由器
|
||||
func (r *GinRouter) Stop(ctx context.Context) error {
|
||||
if r.server == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
r.logger.Info("正在关闭HTTP服务器...")
|
||||
|
||||
// 优雅关闭服务器
|
||||
if err := r.server.Shutdown(ctx); err != nil {
|
||||
r.logger.Error("优雅关闭服务器失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
r.logger.Info("HTTP服务器已关闭")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetEngine 获取Gin引擎
|
||||
func (r *GinRouter) GetEngine() *gin.Engine {
|
||||
return r.engine
|
||||
}
|
||||
|
||||
// applyMiddlewares 应用中间件
|
||||
func (r *GinRouter) applyMiddlewares() {
|
||||
// 按优先级排序中间件,优先级相同时按名称排序确保稳定性
|
||||
sort.Slice(r.middlewares, func(i, j int) bool {
|
||||
priorityI := r.middlewares[i].GetPriority()
|
||||
priorityJ := r.middlewares[j].GetPriority()
|
||||
|
||||
// 如果优先级不同,按优先级降序排列
|
||||
if priorityI != priorityJ {
|
||||
return priorityI > priorityJ
|
||||
}
|
||||
|
||||
// 如果优先级相同,按名称排序确保稳定性
|
||||
return r.middlewares[i].GetName() < r.middlewares[j].GetName()
|
||||
})
|
||||
|
||||
// 应用全局中间件
|
||||
for _, middleware := range r.middlewares {
|
||||
if middleware.IsGlobal() {
|
||||
r.engine.Use(middleware.Handle())
|
||||
r.logger.Debug("已应用全局中间件",
|
||||
zap.String("name", middleware.GetName()),
|
||||
zap.Int("priority", middleware.GetPriority()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetupDefaultRoutes 设置默认路由
|
||||
func (r *GinRouter) SetupDefaultRoutes() {
|
||||
// 健康检查
|
||||
r.engine.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "healthy",
|
||||
"timestamp": time.Now().Unix(),
|
||||
"service": r.config.App.Name,
|
||||
"version": r.config.App.Version,
|
||||
})
|
||||
})
|
||||
|
||||
// 详细健康检查
|
||||
r.engine.GET("/health/detailed", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "healthy",
|
||||
"timestamp": time.Now().Unix(),
|
||||
"service": r.config.App.Name,
|
||||
"version": r.config.App.Version,
|
||||
"uptime": time.Now().Unix(),
|
||||
"environment": r.config.App.Env,
|
||||
})
|
||||
})
|
||||
|
||||
// API信息
|
||||
r.engine.GET("/info", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"name": r.config.App.Name,
|
||||
"version": r.config.App.Version,
|
||||
"environment": r.config.App.Env,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
})
|
||||
|
||||
// Swagger文档路由 (仅在开发环境启用)
|
||||
if !r.config.App.IsProduction() {
|
||||
// Swagger UI
|
||||
r.engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
// API文档重定向
|
||||
r.engine.GET("/docs", func(c *gin.Context) {
|
||||
c.Redirect(http.StatusMovedPermanently, "/swagger/index.html")
|
||||
})
|
||||
|
||||
// API文档信息
|
||||
r.engine.GET("/api/docs", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"swagger_ui": fmt.Sprintf("http://%s/swagger/index.html", c.Request.Host),
|
||||
"openapi_json": fmt.Sprintf("http://%s/swagger/doc.json", c.Request.Host),
|
||||
"redoc": fmt.Sprintf("http://%s/redoc", c.Request.Host),
|
||||
"message": "API文档已可用",
|
||||
})
|
||||
})
|
||||
|
||||
r.logger.Info("Swagger documentation enabled",
|
||||
zap.String("swagger_url", "/swagger/index.html"),
|
||||
zap.String("docs_url", "/docs"),
|
||||
zap.String("api_docs_url", "/api/docs"))
|
||||
}
|
||||
|
||||
// 404处理
|
||||
r.engine.NoRoute(func(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"success": false,
|
||||
"message": "路由未找到",
|
||||
"path": c.Request.URL.Path,
|
||||
"method": c.Request.Method,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
})
|
||||
|
||||
// 405处理
|
||||
r.engine.NoMethod(func(c *gin.Context) {
|
||||
c.JSON(http.StatusMethodNotAllowed, gin.H{
|
||||
"success": false,
|
||||
"message": "请求方法不允许",
|
||||
"path": c.Request.URL.Path,
|
||||
"method": c.Request.Method,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// PrintRoutes 打印路由信息
|
||||
func (r *GinRouter) PrintRoutes() {
|
||||
routes := r.GetRoutes()
|
||||
|
||||
r.logger.Info("Registered routes:")
|
||||
for _, route := range routes {
|
||||
r.logger.Info("Route",
|
||||
zap.String("method", route.Method),
|
||||
zap.String("path", route.Path),
|
||||
zap.String("handler", route.Handler))
|
||||
}
|
||||
}
|
||||
|
||||
// GetStats 获取路由器统计信息
|
||||
func (r *GinRouter) GetStats() map[string]interface{} {
|
||||
routes := r.GetRoutes()
|
||||
|
||||
stats := map[string]interface{}{
|
||||
"total_routes": len(routes),
|
||||
"total_middlewares": len(r.middlewares),
|
||||
"server_config": map[string]interface{}{
|
||||
"read_timeout": r.config.Server.ReadTimeout,
|
||||
"write_timeout": r.config.Server.WriteTimeout,
|
||||
"idle_timeout": r.config.Server.IdleTimeout,
|
||||
},
|
||||
}
|
||||
|
||||
// 按方法统计路由数量
|
||||
methodStats := make(map[string]int)
|
||||
for _, route := range routes {
|
||||
methodStats[route.Method]++
|
||||
}
|
||||
stats["routes_by_method"] = methodStats
|
||||
|
||||
// 中间件统计
|
||||
middlewareStats := make([]map[string]interface{}, 0, len(r.middlewares))
|
||||
for _, middleware := range r.middlewares {
|
||||
middlewareStats = append(middlewareStats, map[string]interface{}{
|
||||
"name": middleware.GetName(),
|
||||
"priority": middleware.GetPriority(),
|
||||
"global": middleware.IsGlobal(),
|
||||
})
|
||||
}
|
||||
stats["middlewares"] = middlewareStats
|
||||
|
||||
return stats
|
||||
}
|
||||
|
||||
// EnableMetrics 启用指标收集
|
||||
func (r *GinRouter) EnableMetrics(collector interfaces.MetricsCollector) {
|
||||
r.engine.Use(func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
c.Next()
|
||||
|
||||
duration := time.Since(start).Seconds()
|
||||
collector.RecordHTTPRequest(c.Request.Method, c.FullPath(), c.Writer.Status(), duration)
|
||||
})
|
||||
}
|
||||
|
||||
// EnableProfiling 启用性能分析
|
||||
func (r *GinRouter) EnableProfiling() {
|
||||
if r.config.Development.EnableProfiler {
|
||||
// 这里可以集成pprof
|
||||
r.logger.Info("Profiling enabled")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user