httpserver

package
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpserver 提供根 HTTP 中间件链,标准库风格:接缝只出现 http.Handler,与 appkit.Middleware(httpserver.Base(log)...) 组合使用。 模块内部选用什么 web 框架是模块自己的事,本包不出现 gin 类型。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLog

func AccessLog(log *slog.Logger) func(http.Handler) http.Handler

AccessLog 每请求记录一条访问日志:method/path/status/bytes/duration/request_id。 探针路径(/healthz、/readyz)不记录。日志在 defer 里落,panic 上抛的请求 也会留下记录(此时状态按 500 记——外层 Recover 会写 500)。

func Base

func Base(log *slog.Logger) []func(http.Handler) http.Handler

Base 返回推荐的根中间件链,外层在前(与 appkit.Middleware 的约定一致): RequestID → Recover → AccessLog → OTel。

func ClientIP

func ClientIP(trusted ...netip.Prefix) func(http.Handler) http.Handler

ClientIP 在链上解析一次真实客户端 IP 存进 ctx(callctx.ClientIP 读取), 之后模块任意深度可取、不可重算——信任决策只发生在这里。

trusted 是可信代理网段(如负载均衡、内部服务网段):

  • 对端不在可信网段:直接用 TCP 对端地址,请求头一律不信—— 转发头是调用方可以随便写的,信了等于把限流、风控、审计的 IP 全部交给伪造方;
  • 对端在可信网段:依次信 X-Client-IP(可信代理或内部跳写入的 原始客户端 IP)、X-Forwarded-For 从右往左第一个不可信地址 (可信代理逐跳追加,最右是最近的跳;伪造的注入永远在最左, 走不到答案);全链可信时取最左(起点本身是可信代理)。

零个可信网段 = 永远用 TCP 对端地址,是安全默认:直连部署直接可用, 前面有代理时组合根把网段配上(配置解析与校验留在组合根,启动期失败)。

建议放在 Base 链之后挂(appkit.Middleware 里追加),任何需要 IP 的 中间件与 handler 之前。

func OTel

func OTel() func(http.Handler) http.Handler

OTel 用 otelhttp 包装下游,span 名为 "METHOD pattern"。探针路径不产生 span(与 AccessLog 同一套 isProbe 判断)。 formatter 会被调用两次:span 起始时 ServeMux 尚未匹配(r.Pattern 为空, 先以 method 起名),handler 返回后 otelhttp 用它按 pattern 重命名。 带方法的 pattern(含空格)不再重复拼 method。

func Recover

func Recover(log *slog.Logger) func(http.Handler) http.Handler

Recover 捕获下游 panic:记录 error 级日志(含堆栈),并以 problem+json 写出 500。http.ErrAbortHandler 是 net/http 认可的中止方式,原样上抛。 若响应头已发出,这里的 WriteHeader 无效但无害(net/http 忽略并告警)。

func RequestID

func RequestID() func(http.Handler) http.Handler

RequestID 从请求头提取跨边界元数据白名单并存进 ctx,request id 缺省时 生成 uuid 并回写响应头。严格安全模式的 identity boundary 位于本链之外, 已先清除 unsigned 租户/caller 头,因此这里只会读到可追踪但不授权的 request id;SecurityDisabled 保留历史的原样提取行为。 放在链最外层,保证 Recover/AccessLog 的日志都带 request id。

func RequestIDFrom

func RequestIDFrom(ctx context.Context) string

RequestIDFrom 取出当前请求的 request id;不存在时返回空串。 需要租户等其它字段时用 callctx.From(ctx)。

func WriteError

func WriteError(log *slog.Logger, w http.ResponseWriter, err error)

WriteError 是服务端错误响应的统一出口:规范化为 apperr 后写 RFC 9457 响应;5xx 时记录一条 error 日志(含完整错误链)。整条请求路径只应在 这里 log 一次错误——上游层 wrap 后原样上抛即可,不要边 log 边返回。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL