Documentation
¶
Overview ¶
Package serverecho echo 运行时适配器:把统一路由分组树(routes.All())挂载到原生 Echo。 与 servergin(gin 适配器)能力等价:上下文装饰 -> 参数绑定 -> 入参转换 -> 校验 -> Handler 调用 -> 出参转换 -> 状态码决策 -> 响应壳包装 -> 写出。 中间件按函数类型断言:echo 项目在 Group.Middlewares 中放 echo.MiddlewareFunc。
文件布局与 servergin 对称:
- echo.go Server 定义与全部扩展点
- mount.go 请求执行链(mount/mountGroup/错误映射/路径转换/文件流)
- bind.go Q 参数绑定(字段元数据缓存 + 标签绑定)
Index ¶
- type Server
- func (s *Server) AddValidator(v validator.Func) *Server
- func (s *Server) Mount(g *echo.Group, groups []*contract.Group)
- func (s *Server) SetBindErrorStatus(status int) *Server
- func (s *Server) SetContextDecorator(fn func(c echo.Context, ctx context.Context) context.Context) *Server
- func (s *Server) SetEnvelope(env response.Envelope) *Server
- func (s *Server) SetErrorMapper(fn func(err error) (httpStatus, bizCode int)) *Server
- func (s *Server) Use(mw ...echo.MiddlewareFunc) *Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server echo 运行时服务器装配器
func (*Server) AddValidator ¶
AddValidator 扩展点:注册自定义校验器(绑定后执行), 见 validator 包:内置标签必填 + Validate() 接口调用;validator.Playground() 接入完整校验规则
func (*Server) SetBindErrorStatus ¶
SetBindErrorStatus 扩展点:设置参数绑定/校验失败(含 InTransform 错误)的 HTTP 状态码。 默认 200(存量行为:HTTP 200 + code=codeError);设为 400 可获得 RESTful 语义, 非 200 时业务 code 跟随状态码(与 StatusError 约定一致)。 仅影响绑定/校验阶段;Handler 返回的业务错误走 StatusError / SetErrorMapper,不受影响。
func (*Server) SetContextDecorator ¶
func (s *Server) SetContextDecorator(fn func(c echo.Context, ctx context.Context) context.Context) *Server
SetContextDecorator 扩展点:把 echo 上下文信息注入 context.Context。 在每次请求最前执行(Q/B 绑定之前),TransformIn / 校验器 / TransformOut / handler 共享同一个已装饰 ctx。约定:fn 必须是纯派生(只读 c、轻量 WithValue 级操作), 重操作(鉴权、查库)请放中间件——每个请求(含校验失败的请求)都会执行。
func (*Server) SetEnvelope ¶
SetEnvelope 扩展点:自定义响应壳。 传入 nil 恢复默认壳 {code, data, msg};路由级覆盖见 contract.RouteMeta.Envelope。 文档侧请用 openapi.OptionWithEnvelopeSchema 配对配置(两者独立)。
func (*Server) SetErrorMapper ¶
SetErrorMapper 扩展点:自定义 错误 -> (HTTP状态码, 业务code) 映射。 仅对不携带状态码的普通错误生效(StatusError / StatusCoder 优先)。