Skip to content

Runtime Behavior

Gateway owns public ingress behavior for Layeron backend apps. This page describes how it routes requests, normalizes errors, emits logs, and keeps security-sensitive data out of Gateway configuration.

Gateway uses Hono as the route engine. Routes declared with app.get, app.post, app.put, app.patch, app.delete, app.head, and app.routeOptions are registered into the Gateway route table.

When backend({ endpoint }) includes a host, Gateway only serves requests for that host. A request to the same path on another host returns 404. When the endpoint includes a base path, Gateway registers public routes under that base path.

During layer dev, keep endpoint as the deployed URL. The local server uses the host and port selected by the CLI and applies the endpoint base path automatically. The endpoint host remains deploy ingress metadata. For endpoint: "https://api.example.com/v1" and app.get("/api/health"), call http://localhost:8787/v1/api/health when layer dev uses port 8787.

Parameterized paths use Hono path matching:

Terminal window
app.get("/api/posts/:id", async (request) => {
return {
ok: true,
}
})

When a path exists and the requested method is missing, Gateway returns 405 with an Allow header. When a path is missing, Gateway returns 404.

Explicit OPTIONS routes are respected. If a path exists and you do not define an OPTIONS route, Gateway can return an automatic 204 response for method discovery and CORS preflight.

Routes use their own auth value first. When a route omits auth, Gateway uses gateway.defaultAuth. The default value is public.

Supported policies:

PolicyRuntime behavior
publicThe request reaches the handler without Auth Product verification.
userGateway requires a valid Auth session and a user subject.
serviceGateway requires a valid Auth session and a service subject, role, or scope.
adminGateway requires a valid Auth session and an admin role or scope.

Authenticated routes require one Auth module in the app. Gateway calls Auth Product RPC to verify the access token and resolve the subject.

CORS preflight handling is path-aware. Gateway returns 404 for preflight requests to unknown paths. Gateway returns 403 when the origin, requested method, or requested headers fail the configured policy.

Successful preflight responses include Access-Control-Allow-Origin, Access-Control-Allow-Methods, configured request headers, exposed response headers, credentials, max age, and the request id response header.

Gateway normalizes runtime errors into JSON responses:

Terminal window
{
"error": {
"code": "route_not_found",
"message": "Route not found: GET /missing.",
"requestId": "req_..."
}
}

Known client-facing errors expose their messages:

CodeStatusMeaning
route_not_found404No route matched the request path.
method_not_allowed405The path exists, but the requested method is not declared.
response_invalid500A handler returned a value that Layeron cannot serialize.

Unexpected handler failures return 500 with Internal server error. by default. Set runtime debug mode in local tooling when you need implementation details during development.

The log payload includes product identity, request id, trace id, method, path, host, origin, status, duration, CORS decisions, and normalized error codes. Header values are recorded only when includeHeaders lists them, and sensitive headers are redacted by default.

Gateway emits structured log records through the runtime observability sink. The current Cloudflare target writes those records to Worker logs.

Gateway log events:

EventDefault control
request.finishEnabled by gateway.logs.request or error severity.
request.errorEnabled by gateway.logs.errors; enabled by default for errors.
route.not_foundEnabled by gateway.logs.errors; enabled by default for errors.
method.not_allowedEnabled by gateway.logs.errors; enabled by default for errors.
cors.preflightEnabled by gateway.logs.cors or warning and error severity.
route.matchedEnabled by gateway.logs.routeMatch.

Gateway records inputSchemaId, outputSchemaId, and route bodyMode in contracts and topology metadata. Runtime request body validation, response validation, and raw body preservation require the schema registry and body mode contracts to be present on route declarations before runtime enforcement can be enabled.

Gateway records route-level secret references in topology metadata when guards or modules declare them. The app runtime receives the Worker environment for its runtime unit. Use dedicated runtime placement when a capability needs a narrower Worker-level secret scope.

  • Keep CORS origins as exact production origins when using credentials.
  • Keep authorization, cookie, set-cookie, and API key headers out of Gateway logs.
  • Use route-level auth policies for authorization decisions.
  • Use modules such as Captcha or webhook verification for capability-specific ingress checks.
  • Keep secrets in the Secret product. Gateway configuration should contain only public routing and policy values.