feat: 添加应用骨架,配置化 zap 日志贯穿全链路
- cmd/server/main.go: 使用 logger.NewLogger(cfg.Log) 替代 zap.NewProduction() - internal/app: 依赖注入组装 DB/Slurm/Service/Handler,传递 logger - internal/middleware: RequestLogger 请求日志中间件 - internal/server: 统一响应格式和路由注册 - go.mod: module 更名为 gcy_hpc_server,添加 gin/zap/lumberjack/gorm 依赖 - 日志初始化失败时 fail fast (os.Exit(1)) - GormLevel 从配置传递到 NewGormDB,支持 YAML 独立配置 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
44
internal/server/response.go
Normal file
44
internal/server/response.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// APIResponse represents the unified JSON structure for all API responses.
|
||||
type APIResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// OK responds with 200 and success data.
|
||||
func OK(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusOK, APIResponse{Success: true, Data: data})
|
||||
}
|
||||
|
||||
// Created responds with 201 and success data.
|
||||
func Created(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusCreated, APIResponse{Success: true, Data: data})
|
||||
}
|
||||
|
||||
// BadRequest responds with 400 and an error message.
|
||||
func BadRequest(c *gin.Context, msg string) {
|
||||
c.JSON(http.StatusBadRequest, APIResponse{Success: false, Error: msg})
|
||||
}
|
||||
|
||||
// NotFound responds with 404 and an error message.
|
||||
func NotFound(c *gin.Context, msg string) {
|
||||
c.JSON(http.StatusNotFound, APIResponse{Success: false, Error: msg})
|
||||
}
|
||||
|
||||
// InternalError responds with 500 and an error message.
|
||||
func InternalError(c *gin.Context, msg string) {
|
||||
c.JSON(http.StatusInternalServerError, APIResponse{Success: false, Error: msg})
|
||||
}
|
||||
|
||||
// ErrorWithStatus responds with a custom status code and an error message.
|
||||
func ErrorWithStatus(c *gin.Context, code int, msg string) {
|
||||
c.JSON(code, APIResponse{Success: false, Error: msg})
|
||||
}
|
||||
Reference in New Issue
Block a user