Documentation
¶
Overview ¶
Package httpapi is the HTTP frontend, implemented on the standard library only (net/http with method-pattern routing). It turns the registry's HTTP hints into routes:
- HTTPHints.Method + Path define the route; path placeholders {name} map to fields tagged http:"path" (via r.PathValue).
- 未标注 http 位置或标注 http:"query" 的字段默认从 query 绑定; http:"header"(httpName)从请求头绑定;JSON body 合并为入参基底。
- Responses are bare JSON (no envelope), the same shape as CLI --json; errors map to HTTP status codes through the shared error taxonomy.
- GET /openapi.json exposes an OpenAPI 3 document generated from the same InputSchema the MCP frontend uses.
Entries without HTTP hints are not routed. This package pulls zero third-party dependencies.
Index ¶
- Constants
- func Bearer(tokens []string, h http.Handler) http.Handler
- func CORS(origins []string, h http.Handler) http.Handler
- func Gzip(h http.Handler) http.Handler
- func Handler(reg *registry.Registry) (http.Handler, error)
- func HandlerFor(e *spec.Entry) http.HandlerFunc
- func HandlerForWith(e *spec.Entry, defaults map[string]string) http.HandlerFunc
- func HandlerForWithMeta(e *spec.Entry, defaults map[string]string, meta ResponseMeta) http.HandlerFunc
- func HandlerWith(reg *registry.Registry, defaults map[string]string) (http.Handler, error)
- func HandlerWithMeta(reg *registry.Registry, defaults map[string]string, meta ResponseMeta) (http.Handler, error)
- func ServerHeaders(meta ResponseMeta, h http.Handler) http.Handler
- type ResponseMeta
Constants ¶
const ( HeaderAppName = "X-App-Name" HeaderAppVersion = "X-App-Version" HeaderSDKVersion = "X-XYZ-Version" HeaderCommand = "X-XYZ-Command" HeaderDuration = "X-XYZ-Duration-Ms" )
服务器上下文响应头(xyz-spec §11.4)。默认开启;NoAutoHeaders 关闭自动头, Headers 里的自定义头始终写入(它们是显式配置,不受开关影响)。 X-App-* 报告*应用程序*身份,X-XYZ-* 报告 xyz 库自身与本次调用的上下文。
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
CORS wraps h with permissive-allowlist CORS handling: requests whose Origin matches an entry in origins (or "*" for any origin) get the Access-Control-Allow-Origin header, and OPTIONS preflights are answered with 204 before reaching the inner handler. Empty origins disables CORS.
func Gzip ¶
Gzip transparently compresses responses for clients that send Accept-Encoding: gzip. Mount it inside auth (compression is per-request and must come after the credentials check).
func Handler ¶
Handler builds the router for registered HTTP routes plus /openapi.json. Conflicting method+path registrations are errors.
func HandlerFor ¶ added in v0.2.0
func HandlerFor(e *spec.Entry) http.HandlerFunc
HandlerFor returns the standalone handler for one registered entry: the full binding (query/path/header/body) and error mapping, without routing. Mount it onto any router (gin.WrapH, echo, chi) or wrap it with your own middleware; Handler below composes all routed entries plus /healthz and /openapi.json.
func HandlerForWith ¶ added in v0.3.1
HandlerForWith 是带通道级默认参数的 HandlerFor。
func HandlerForWithMeta ¶ added in v0.4.2
func HandlerForWithMeta(e *spec.Entry, defaults map[string]string, meta ResponseMeta) http.HandlerFunc
HandlerForWithMeta 是带通道级默认参数与服务器上下文头的 HandlerFor。
func HandlerWith ¶ added in v0.3.1
HandlerWith 是带通道级默认参数的 Handler(serve --default k=v 注入: 缺席键补上、显式入参优先)。
func HandlerWithMeta ¶ added in v0.4.2
func HandlerWithMeta(reg *registry.Registry, defaults map[string]string, meta ResponseMeta) (http.Handler, error)
HandlerWithMeta 是带通道级默认参数与服务器上下文头的 Handler。
func ServerHeaders ¶ added in v0.4.2
func ServerHeaders(meta ResponseMeta, h http.Handler) http.Handler
ServerHeaders 给每个响应附加服务器上下文头:应用身份(X-App-Name / X-App-Version)、xyz 库版本(X-XYZ-Version)与 meta.Headers 里的自定义 静态头。命令名与耗时由各路由处理器自己写(它们才知道 e.Name 与 Invoke 时长)。本中间件应挂在最外层,使 /healthz、/openapi.json 与 /mcp 也带上 这些头(xyz-spec §11.6)。
Types ¶
type ResponseMeta ¶ added in v0.4.2
type ResponseMeta struct {
// AppName 写入 X-App-Name;空则不写该头。
AppName string
// AppVersion 写入 X-App-Version(应用程序自己的版本);空则不写。
AppVersion string
// SDKVersion 写入 X-XYZ-Version(xyz 库自身版本);空则不写。
SDKVersion string
// Headers 是附加到每个响应的静态头(原样写入,键名不做规范化)。
Headers map[string]string
// NoAutoHeaders 置 true 时不写 X-App-*/X-XYZ-* 五个自动头;
// Headers 自定义头不受影响。
NoAutoHeaders bool
}
ResponseMeta 描述 HTTP 响应附带的服务器上下文:应用身份(名字/版本)、 xyz 库版本、命令名、处理耗时与一批用户自定义静态头。零值即「不带任何 自动头、无自定义头」。