Documentation
¶
Overview ¶
Application scenarios: - Attach the runtime container onto request-scoped or task-scoped contexts. - Give middleware, handlers, and background jobs a lightweight way to access container-bound capabilities. - Keep container context keys private and collision-free.
适用场景: - 将运行时容器挂到请求级或任务级 context 上。 - 为 middleware、handler 和后台任务提供轻量级容器访问路径。 - 保持容器相关 context key 私有且避免冲突。
Application scenarios: - Propagate request-scoped locale information through context. - Let validation, response shaping, and business handlers read the effective locale consistently. - Keep locale-related context keys private and collision-free.
适用场景: - 在 context 中透传请求级 locale 信息。 - 让校验、响应整形和业务 handler 统一读取当前 locale。 - 保持 locale 相关 context key 私有且避免冲突。
Application scenarios: - Safely locate configuration files, data models, or assets regardless of the current working directory. - Eliminate brittle relative-path assumptions when running binaries from subdirectories, containers, or test runners. - Search across APP_BASE_PATH, executable directories, and parent directories.
适用场景: - 无论当前工作目录如何,安全定位配置文件、数据模型或静态资源。 - 消除在子目录、容器或测试运行器中执行二进制时的脆弱相对路径假设。 - 跨 APP_BASE_PATH、可执行文件目录以及父级目录进行智能探测。
Application scenarios: - Propagate request ID and trace ID through request-scoped context. - Give middleware, logging, tracing, and handlers one shared access path to correlation identifiers. - Keep identifier context keys private to avoid collisions across packages.
适用场景: - 在请求级 context 中透传 request ID 和 trace ID。 - 为 middleware、日志、tracing 和 handler 提供统一的关联标识读取路径。 - 保持标识相关 context key 私有,避免跨包冲突。
Application scenarios: - Propagate tenant identity through request-scoped or task-scoped context. - Support multi-tenant business flows without coupling tenant access to one transport layer. - Keep tenant context keys private and collision-free.
适用场景: - 在请求级或任务级 context 中透传租户身份。 - 支持多租户业务流程,而不把租户读取绑定到某一种 transport 层。 - 保持 tenant context key 私有且避免冲突。
Application scenarios: - Store validated request payloads in context for downstream reuse. - Avoid repeated bind/validate work across middleware and handlers. - Keep validated-body context keys private and collision-free.
适用场景: - 在 context 中存储已经完成校验的请求体,供下游复用。 - 避免 middleware 和 handler 之间重复做 bind/validate。 - 保持 validated-body context key 私有且避免冲突。
Index ¶
- func FromContainerContext(ctx context.Context) (any, bool)
- func FromLocaleContext(ctx context.Context) (string, bool)
- func FromRequestIDContext(ctx context.Context) (string, bool)
- func FromTenantContext(ctx context.Context) (string, bool)
- func FromTraceIDContext(ctx context.Context) (string, bool)
- func FromValidatedBodyContext(ctx context.Context) (any, bool)
- func NewContainerContext(ctx context.Context, c any) context.Context
- func NewLocaleContext(ctx context.Context, locale string) context.Context
- func NewRequestIDContext(ctx context.Context, requestID string) context.Context
- func NewTenantContext(ctx context.Context, tenant string) context.Context
- func NewTraceIDContext(ctx context.Context, traceID string) context.Context
- func NewValidatedBodyContext(ctx context.Context, body any) context.Context
- func ResolveResourcePath(filePath string, extraSearchDirs ...string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContainerContext ¶
FromContainerContext reads a container-like object from the context.
FromContainerContext 从 context 中读取容器对象。
func FromLocaleContext ¶
FromLocaleContext reads locale information from context.
FromLocaleContext 从 context 中读取 locale 信息。
func FromRequestIDContext ¶
FromRequestIDContext reads request ID from context.
FromRequestIDContext 从 context 中读取 request ID。
func FromTenantContext ¶
FromTenantContext reads tenant identity from context.
FromTenantContext 从 context 中读取租户身份。
func FromTraceIDContext ¶
FromTraceIDContext reads trace ID from context.
FromTraceIDContext 从 context 中读取 trace ID。
func FromValidatedBodyContext ¶
FromValidatedBodyContext reads a validated body from context.
FromValidatedBodyContext 从 context 中读取已校验请求体。
func NewContainerContext ¶
NewContainerContext stores a container-like object into the context.
NewContainerContext 将容器对象写入 context。
func NewLocaleContext ¶
NewLocaleContext stores locale information in context.
NewLocaleContext 将 locale 信息写入 context。
func NewRequestIDContext ¶
NewRequestIDContext stores request ID in context.
NewRequestIDContext 将 request ID 写入 context。
func NewTenantContext ¶
NewTenantContext stores tenant identity in context.
NewTenantContext 将租户身份写入 context。
func NewTraceIDContext ¶
NewTraceIDContext stores trace ID in context.
NewTraceIDContext 将 trace ID 写入 context。
func NewValidatedBodyContext ¶
NewValidatedBodyContext stores a validated body in context.
NewValidatedBodyContext 将已校验请求体写入 context。
func ResolveResourcePath ¶ added in v0.3.0
ResolveResourcePath safely searches for a file path across standard runtime locations: 1. As-is if it exists (absolute or relative to current working directory). 2. Relative to APP_BASE_PATH environment variable (if set). 3. Relative to the running executable's directory. 4. Relative to caller-provided extra search directories. 5. Searching up to 3 parent directories (../, ../../, ../../../).
Returns the resolved absolute path if found, or empty string if not found.
Types ¶
This section is empty.