# Decorators on the server

> Status: implemented are markup as HTML, components of the server, their results, `Response`
> going up, `Content<T>`, component values with a result, decorators with guards, wrappers that
> take the body and the request context, routes, and `rooklang/server`; the server of the site, `site/server/main.rk`, is written
> with them, and the guide has a section on them. The decisions are collected at the end of the
> document, together with what is not decided yet. Server markup, components and `rooklang/server`
> remain **experimental** in 1.x and may change between releases.

> This document builds on [components.md](https://github.com/QWERN-9876r/RookLang/blob/HEAD/docs/components.md): components, decorators, context,
> components as values. It describes only what is different on the server.

Decorators keep what they are in the browser: state and functions of their own, public members,
context, `includes`, a wrapper. On the server their body may also wait and may answer instead of
the component.

```go
dec admin() {
    if @session.user?.role != "admin" {
        return reply("Forbidden", { status: 403 })
    }
    return (page Html) => <Layout title="Admin">{page}</Layout>
}

@admin
comp Dashboard() {
    const stats = await db.stats()      // not run without access
    return <main>...</main>
}
```

- **`await`** is allowed in the body of a decorator: `public const user = await userOf(request)`.
- **An early `return` of a value** (not the function of a wrapper) is the result of the component:
  the body of the component and the bodies of the decorators below do not run, and the wrappers are
  not applied. Its type is added to the type of the component's tag: `<Dashboard />` is
  `Html | Response`. This is a guard: an access check, a limit of requests, a cache that already has
  the answer.
- **Arguments can read the component's context.** In `@guard(@api.secret, request)`
  the compiler infers the dependency on `@api`. The value is read before the guard runs.
- **A parameter with `public`** is a member, `dec http(public request Request, public ip string)`,
  read as `@http.request`, so a context does not copy its parameters into constants.
- **The last `return`** is still the wrapper, `(page T) => U`. `T` limits where the decorator can be
  applied, as `(form HTMLFormElement) => ...` does in the browser; `U` is the result of the tag.
  The wrapper may wait, as the body does.
- **A wrapper that takes `Content<T>`** gets the body instead of what it gave: a function that runs
  the component's body, with the wrappers of the decorators below, when the wrapper calls it. So a
  decorator decides whether the body runs and catches what it throws, an error boundary written in
  Rook:

  ```go
  dec fallback(view func(Error) Html) {
      return (page Content<Html | Response>) => {
          const value, err = try await page()
          if err != null {
              return view(err)
          }
          return value
      }
  }

  @fallback((err) => <Oops message={err.message} />)
  comp Feed() { ... }
  ```

  `try` lets a `Response` from below go up, so a redirect under the boundary is still the answer.
  The bodies of the decorators run before, as always: the boundary catches the component and what
  it renders. What the component gives is checked against `T`.

- **`mount`** does not exist on the server: there is nothing to mount. The cleanup of a decorator
  is a `defer` in its body, which runs when the component ends, after its children.
- The order is that of the browser: bodies from top to bottom, wrappers from the bottom up; a
  guard of the top decorator stops the ones below it.

