Documentation
¶
Overview ¶
演示: web 层三件套 —— 统一响应(errcode) + 参数校验(middleware.Bind) + 分页(BindPage/RenderPage), 以及 CORS / 限流 / trace_id / 可观测端点四个内置能力的配置化开启。零外部服务。
运行:
cd example/web && go run .
访问(另开终端):
创建用户(参数校验失败自动 400):
curl -X POST http://127.0.0.1:8081/api/v1/users -H "Content-Type: application/json" -d '{"name":"alice","email":"a@ex.com"}'
curl -X POST http://127.0.0.1:8081/api/v1/users -H "Content-Type: application/json" -d '{"name":"bob","email":"bad"}' -> 400
分页查询(内置 23 条模拟数据):
curl "http://127.0.0.1:8081/api/v1/users?page=2&size=5"
错误码映射:
curl http://127.0.0.1:8081/api/v1/users/999 -> 404 {"code":10404}
curl http://127.0.0.1:8081/api/v1/me -> 401 {"code":10401}
可观测端点:
curl http://127.0.0.1:8081/metrics -> Prometheus 指标
每个响应头都带 32 位 hex 的 X-Request-Id, 是日志聚合与调用链关联的纽带。
Click to show internal directories.
Click to hide internal directories.