# httpPlugin

[← All plugins](/docs/reference/plugins)

```ts
import { httpPlugin } from '@routecraft/routecraft'
```

Serves routes over HTTP. Backs the [`http()` source](/docs/reference/adapters/http); routes declare `.from(http({ path, method }))` and the plugin mounts them on a named server.

`http` is a first-class core config key, so the common path is `defineConfig({ http: {...} })` rather than `plugins: [httpPlugin(...)]`. The factory is exported for programmatic composition.

```ts
import { defineConfig, jwt } from '@routecraft/routecraft'

export const craftConfig = defineConfig({
  servers: { public: { port: 8080, host: '0.0.0.0' } },
  http: {
    server: 'public',
    auth: jwt({ secret: process.env.JWT_SECRET!, issuer: '...', audience: '...' }),
  },
})
```

## Options

| Option | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `server` | `string` | `default` | No | Single-mount sugar: named server for the lone `default` mount. Mutually exclusive with `mounts`. |
| `auth` | `HttpAuth \| false` | inherited | No | Single-mount sugar: the wall for a lone `default` mount at `/`. Omit to inherit server auth, `false` to remove the wall (the inherited validator stays reachable for `.authorize()`). Mutually exclusive with `mounts`. |
| `mounts` | `Record<string, { path, server?, auth? }>` | one `default` mount at `/` | No | Named path-scoped surfaces, each a complete self-description: path, server, auth. See [Mounts and authentication](/docs/reference/adapters/http#mounts-and-authentication) and [Mounts and servers](/docs/reference/adapters/http#mounts-and-servers). |
| `maxBodySize` | `number` | `10485760` (10 MB) | No | Maximum request body in bytes. Larger requests get `413`. |
| `events` | `{ perRequest?: boolean }` | `{ perRequest: true }` | No | Toggle the `plugin:http:request:completed` event. |
| `builtins` | `{ health?, ready?, openapi?: { enabled?: boolean; requireAuth?: boolean } }` | see below | No | Per-endpoint config for `/health`, `/ready`, `/openapi.json`. Each takes the same `{ enabled, requireAuth }` shape. See [Configuring built-ins](/docs/reference/adapters/http#configuring-built-ins) on the adapter reference for defaults and the per-endpoint behaviour table. |

Per-route authorization uses the existing [`.authorize({ roles, scopes })`](/docs/reference/operations/authorize) builder. The mount, not the route, decides authentication: routes on a walled mount require a valid credential, routes on a mount with no wall (`auth: false`) never see one, and such a route that declares `.authorize()` forces verification through the inherited validator for itself alone. See [Mounts and authentication](/docs/reference/adapters/http#mounts-and-authentication) for the full matrix. Built-in endpoints `/health`, `/ready`, and `/openapi.json` serve from the `default` mount at `/` unless a user route claims the same path, and describe only routes on that mount's server when mounts span several listeners.

## Custom mounts

Custom plugins can join the same listener during `apply()`:

```ts
import { requireWebIngress, type CraftPlugin } from '@routecraft/routecraft'

const healthPlugin: CraftPlugin = {
  name: 'health',
  apply(ctx) {
    const ingress = requireWebIngress(ctx, 'public')
    const unmount = ingress.mountHttp({
      id: 'custom-health',
      claims: () => [{ kind: 'exact', path: '/internal/health', methods: ['GET'] }],
      handler: () => Response.json({ ok: true }),
    })
    ctx.registerTeardown(unmount)
  },
}
```

Claims are thunks evaluated once during startup validation. This lets plugins register dynamic routes during route startup while still detecting cross-mount conflicts before the listener binds.

A mount whose responses stream or stay quiet indefinitely (SSE, MCP-style channels) declares `longLived: true`; the ingress then exempts its requests from the listener's idle timeout while every other connection keeps the bounded default.

## Lifecycle

- `apply(ctx)` validates options, publishes the route registry, and mounts it on the selected named server. It does not bind a socket.
- During context start, all mount claims on all named servers are validated before any listener binds. The server then emits `server:listening { server, port, host }`.
- On context stop, named servers drain active requests, close idle connections, and emit `server:closed { server }`.
- A bind failure (`EADDRINUSE` / `EADDRNOTAVAIL`) surfaces as [`RC5019`](/docs/reference/errors#rc-5019).

## Events

See [HTTP plugin events](/docs/reference/events#http-plugin-events) for the full list. The plugin also re-uses the framework's `auth:success` / `auth:rejected` events with `source: "http"`.

## Related

- [`http()` adapter](/docs/reference/adapters/http) -- both source and destination overloads.
- [Configuration](/docs/reference/configuration#servers-and-http) -- the `servers` and `http` first-class config keys.
- [`.authorize()`](/docs/reference/operations/authorize) -- per-route role/scope/predicate checks.
