Documentation
¶
Overview ¶
Package yankit is the shared HTTP toolkit for the Yan services (YanAuth, YanLearn): uniform {"code","error"} JSON error rendering and an Echo v5 startup scaffold (slog request logging, health-check helper).
It must stay tiny: only code both services genuinely share (ADR 0001).
Index ¶
- Constants
- func DefaultCodeForStatus(status int) string
- func ErrorHandler(log *slog.Logger, opts ...func(*errorHandlerConfig)) echo.HTTPErrorHandler
- func Healthz(ping func(ctx context.Context) error) echo.HandlerFunc
- func JWTAuth(keys KeySetSource) echo.MiddlewareFunc
- func New(log *slog.Logger) *echo.Echo
- func ParseJWKS(b []byte) (jwk.Set, error)
- func RenderError(c *echo.Context, status int, code, msg string) error
- func RequestLog(log *slog.Logger) echo.MiddlewareFunc
- func RequirePerm(code string) echo.MiddlewareFunc
- func RequireScope(scope string) echo.MiddlewareFunc
- func WithMessages(r MessageResolver) func(*errorHandlerConfig)
- type APIError
- type Auth
- type Claims
- type KeySetFunc
- type KeySetSource
- type MessageResolver
- type Options
Constants ¶
const ( ScopeNormal = "normal" // 常规管理/学习会话 ScopePreview = "preview" // 学生视角预览(拒绝管理类接口) )
Token scopes and the expected issuer, shared with YanAuth.
const ( CodeInvalidRequest = "invalid_request" CodeForbidden = "forbidden" CodeNotFound = "not_found" CodeMethodNotAllowed = "method_not_allowed" CodeConflict = "conflict" CodeRateLimited = "rate_limited" CodeInternalError = "internal_error" // 鉴权链错误码,与 YanAuth 现行契约一致(add-only)。 CodeInvalidToken = "invalid_token" CodeScopeNotAllowed = "scope_not_allowed" CodeMissingPermission = "missing_permission" )
Stable machine-readable error codes (snake_case, add-only contract: never rename a published code). Rendered alongside the human-readable message:
{"code": "not_found", "error": "Not Found"}
SECURITY: codes must not leak information. Where failure reasons must stay indistinguishable to callers, share one code and keep the precise reason in the audit log (e.g. "invalid_credentials").
These constants cover what the framework itself renders; each service adds its own domain codes under its namespace.
Variables ¶
This section is empty.
Functions ¶
func DefaultCodeForStatus ¶
DefaultCodeForStatus gives every response a code even where no specific business code applies.
func ErrorHandler ¶
func ErrorHandler(log *slog.Logger, opts ...func(*errorHandlerConfig)) echo.HTTPErrorHandler
ErrorHandler returns uniform JSON error responses: every error body is {"code": <stable snake_case code>, "error": <human message>}. Details of any 5xx go to the log, never the client; unknown errors collapse to 500 internal_error.
func Healthz ¶
func Healthz(ping func(ctx context.Context) error) echo.HandlerFunc
Healthz reports liveness, optionally probing a dependency (typically the database: pass its Ping). A nil ping skips the probe. A failing probe renders 503 database_unavailable through the shared error handler.
func JWTAuth ¶
func JWTAuth(keys KeySetSource) echo.MiddlewareFunc
JWTAuth verifies the Bearer JWT against keys and stores the claims in the request context (read them with ClaimsOf). 401 invalid_token otherwise.
func New ¶
New returns an Echo instance wired with yankit's error handler and slog request logging: the startup scaffold shared by the Yan services.
func ParseJWKS ¶
ParseJWKS parses a JWKS document (e.g. fetched at startup from YanAuth's /.well-known/jwks.json) into a key set.
func RenderError ¶
RenderError writes the uniform JSON error body. An empty message falls back to the HTTP status text; HEAD requests get no body.
func RequestLog ¶
func RequestLog(log *slog.Logger) echo.MiddlewareFunc
RequestLog writes one structured log line per request.
func RequirePerm ¶
func RequirePerm(code string) echo.MiddlewareFunc
RequirePerm rejects tokens whose perms do not include the fully-qualified permission code (e.g. "learn:course:create"). 403 missing_permission, with the missing code in the error message, otherwise.
func RequireScope ¶
func RequireScope(scope string) echo.MiddlewareFunc
RequireScope rejects tokens whose scope does not match — e.g. preview tokens are refused by management routes requiring ScopeNormal. 403 scope_not_allowed otherwise.
func WithMessages ¶
func WithMessages(r MessageResolver) func(*errorHandlerConfig)
WithMessages registers a render-time message resolver (service-side i18n error catalogs, ADR 0011). Variadic: the zero-argument ErrorHandler keeps its original behavior.
Types ¶
type APIError ¶
type APIError struct {
// contains filtered or unexported fields
}
APIError carries a stable snake_case code plus an HTTP status and a human-readable message. Handlers return it; ErrorHandler renders it.
func NewAPIError ¶
NewAPIError builds an *APIError.
func (*APIError) StatusCode ¶
StatusCode implements echo.HTTPStatusCoder so middleware using echo.ResolveResponseStatus (e.g. RequestLog) sees the real status.
type Auth ¶
type Auth struct {
Options
}
Auth bundles the three middlewares with shared options, for services that keep keys and a permission namespace in one place.
func (*Auth) JWT ¶
func (a *Auth) JWT() echo.MiddlewareFunc
JWT returns the JWT verification middleware.
type Claims ¶
type Claims struct {
Subject string // user id(preview 令牌为影子账号 id)
Scope string // normal | preview
Act string // 发起预览的教师 id(仅 preview)
Perms []string // 带服务命名空间的权限码
}
Claims are the YanAuth token claims a consumer service needs.
type KeySetFunc ¶
KeySetFunc adapts a function to KeySetSource.
type KeySetSource ¶
KeySetSource supplies the verification key set. It is called per request, so implementations decide their own caching/refresh strategy — verification itself stays local (no network call inside yankit).
type MessageResolver ¶
MessageResolver localizes an error body at render time: given the request context and the stable error code, return the human message in the caller's locale. false (= not found) keeps the message baked into the error at construction. Consulted for 4xx responses only — 5xx bodies are scrubbed to the HTTP status text regardless.
type Options ¶
type Options struct {
Keys KeySetSource
Namespace string // 可选;Perm 会以此为前缀
}
Options configure an Auth group: the verification key source and the service's permission namespace (e.g. "learn" for codes like "learn:course:create").
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
minimal
command
Command minimal demonstrates the yankit scaffold: an Echo v5 server with slog JSON request logging, the shared health-check handler and uniform {"code","error"} error rendering.
|
Command minimal demonstrates the yankit scaffold: an Echo v5 server with slog JSON request logging, the shared health-check handler and uniform {"code","error"} error rendering. |