抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
This commit is contained in:
commit
b4c3ac622c
55 files changed
+5862
No files matched your search
@@ -0,0 +1,119 @@
|
||||
// Package http 提供 Gin 服务和请求管线,业务身份、权限及资源装配由应用负责。
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/diagnostic"
|
||||
"git.zhangdeman.cn/github/ginframework/fault"
|
||||
failure "git.zhangdeman.cn/github/ginframework/internal/failure"
|
||||
"git.zhangdeman.cn/github/ginframework/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
codeServerConfig = 900401
|
||||
codeRoute = 900402
|
||||
codeLifecycle = 900403
|
||||
codeServe = 900404
|
||||
codeShutdown = 900405
|
||||
codeSerialize = 900406
|
||||
codeController = 900407
|
||||
codeInject = 900408
|
||||
codePanic = 900409
|
||||
codeMultipartCleanup = 900410
|
||||
codeMiddlewareCount = 900411
|
||||
codeMiddlewareHandler = 900412
|
||||
codeTrustedRule = 900413
|
||||
codeProxy = 900414
|
||||
codeRouteHandler = 900415
|
||||
codeControllerInstance = 900416
|
||||
codeControllerResult = 900417
|
||||
codeControllerForm = 900418
|
||||
codeControllerMeta = 900419
|
||||
codeRouteDeclaration = 900420
|
||||
codeUnsupportedTag = 900421
|
||||
codeBodyLimit = 900422
|
||||
codeTrustedRead = 900423
|
||||
codeTrustedType = 900424
|
||||
codeRouteFrozen = 900425
|
||||
codeRoutePath = 900426
|
||||
)
|
||||
|
||||
// PublicError 是业务显式允许公开的错误,不包含原始原因文本。
|
||||
type PublicError struct {
|
||||
Code int `json:"code" dc:"公开业务错误码"`
|
||||
Message string `json:"message" dc:"可安全展示的文案"`
|
||||
Status int `json:"-" dc:"HTTP 状态,与业务码分开表达"`
|
||||
Data any `json:"data" dc:"可选的公开数据,不接受数据库错误对象"`
|
||||
}
|
||||
|
||||
// Error 返回应用允许展示的文案。
|
||||
// 返回:公开错误摘要。
|
||||
func (e *PublicError) Error() string { return e.Message }
|
||||
|
||||
// ErrorMapper 将应用错误转换为明确的公开契约。
|
||||
// 入参:err 为业务错误链。
|
||||
// 返回:已允许公开的错误,nil 使用安全的服务错误。
|
||||
type ErrorMapper func(err error) *PublicError
|
||||
|
||||
// Envelope 是默认 JSON 响应协议,输出前先完整序列化。
|
||||
type Envelope struct {
|
||||
Code int `json:"code" dc:"应用业务码"`
|
||||
Message string `json:"message" dc:"安全公开文案"`
|
||||
Data any `json:"data" dc:"Controller 公开 DTO,具体类型由路由决定"`
|
||||
TraceID string `json:"trace_id" dc:"可信入口生成的链路标识"`
|
||||
CostMS int64 `json:"cost_ms" dc:"请求处理耗时毫秒"`
|
||||
}
|
||||
|
||||
// writeResult 统一输出普通请求响应,避免部分 JSON 写出后再尝试补错误。
|
||||
// 入参:ctx 为请求,data 为公开结果,err 为业务或绑定错误,status 为默认错误状态。
|
||||
func (s *Server) writeResult(ctx *gin.Context, data any, err error, status int) {
|
||||
if ctx.Writer.Written() {
|
||||
return
|
||||
}
|
||||
result := Envelope{Code: s.config.SuccessCode, Message: "OK", Data: data, TraceID: diagnostic.From(ctx.Request.Context()).TraceID}
|
||||
if start, ok := ctx.Get(startKey); ok {
|
||||
result.CostMS = time.Since(start.(time.Time)).Milliseconds()
|
||||
}
|
||||
if err != nil {
|
||||
public := &PublicError{Code: http.StatusInternalServerError, Message: "Internal Server Error", Status: http.StatusInternalServerError}
|
||||
var (
|
||||
explicit *PublicError
|
||||
)
|
||||
if errors.As(err, &explicit) {
|
||||
public = explicit
|
||||
} else if !fault.Internal(err) {
|
||||
mapped := (*PublicError)(nil)
|
||||
if s.options.ErrorMapper != nil {
|
||||
mapped = s.options.ErrorMapper(err)
|
||||
}
|
||||
if mapped != nil {
|
||||
public = mapped
|
||||
} else {
|
||||
// 响应继续使用安全错误,记录仅在此首次接收未知错误的源头发生。
|
||||
_ = failure.WrapContext(ctx.Request.Context(), codeController, "execute HTTP controller failed", err)
|
||||
}
|
||||
}
|
||||
if status != 0 {
|
||||
public = &PublicError{Code: status, Message: http.StatusText(status), Status: status}
|
||||
}
|
||||
result.Code, result.Message, result.Data = public.Code, public.Message, public.Data
|
||||
status = public.Status
|
||||
if status < 400 || status > 599 {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
} else {
|
||||
status = http.StatusOK
|
||||
}
|
||||
body, marshalErr := json.Marshal(result)
|
||||
if marshalErr != nil {
|
||||
logger.RecordErrorContext(ctx.Request.Context(), "serialize HTTP response failed", fault.New(codeSerialize, "serialize HTTP response failed", marshalErr))
|
||||
ctx.Data(http.StatusInternalServerError, "application/json; charset=utf-8", []byte(`{"code":500,"message":"Internal Server Error","data":null}`))
|
||||
return
|
||||
}
|
||||
ctx.Data(status, "application/json; charset=utf-8", body)
|
||||
}
|
||||