# `Dstar.Page.Plug`
[🔗](https://github.com/ricotrevisan/dstar/blob/v0.3.3/lib/dstar/page/plug.ex#L2)

The plug behind `Dstar.Router.dstar/2`. Drives `Dstar.Page` callbacks:

- `{:page, Module}` — GET: `mount/2` then `render/1` through Phoenix's
  view pipeline (root layout, flash, `page_title` all apply).
- `{:event, Module}` — POST `_event/:event`: reads signals, optional
  `authorize/2`, starts SSE, calls `handle_event/3`.
- `{:stream, Module}` — POST: optional `authorize/2`, atomically claims
  dedup ownership when `stream_key/1` is defined, starts SSE, calls
  `handle_connect/2`, then runs the receive loop on `Dstar.Stream`,
  dispatching to `handle_info/2`. A keyed claim failure returns 503
  before the callback.

`authorize/2` is the pre-SSE seam: a halted or already-staged
response is returned as ordinary HTTP and SSE never starts.
Before authorization, signals are fetched with the page's
`:max_signal_bytes` limit; malformed/non-object payloads return 400 and
oversized payloads return 413. `mount/2` does not run on event or stream
POSTs.

A raise in `handle_event/3`, `handle_connect/2` or `handle_info/2` is
logged and re-raised. With `config :dstar, debug_errors: true` (dev only)
it is also relayed to the browser console over the open stream.

All control flow lives here as plain functions — pages contain only
callbacks.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
