# The entry point

> 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.

The root of the tree is a component that takes the request. `rooklang/server` connects it to a
server:

```go
import { serve } from "rooklang/server"

@session(request)
comp App(request Request) {
    return <Logger><Router routes={ROUTES} /></Logger>
}

serve(App, { port: PORT })
```

- **`handler(App)`** gives a function `(request Request) => Promise<Response>`, the web standard:
  `Bun.serve({ fetch: handler(App) })`, `Deno.serve(handler(App))`,
  `export default { fetch: handler(App) }` of Workers.
- **`serve(App, { port })`** is the same for `node:http`: it turns an `IncomingMessage` into a
  `Request` and writes the `Response` back.
- **The root takes `request Request`** and may take other properties that `handler` is given:
  `handler(App, { db })`, `serve(App, { port: 3000, props: { db } })`.
- **Properties of each request** come from a function instead of an object,
  `Bun.serve({ fetch: handler(App, (request, server) => ({ ip: server.requestIP(request)?.address })) })`:
  it gets the request and what the platform gives with it, as the server of `Bun.serve` (up to two
  values: `env` and `ctx` of Workers), and may wait. Their types come from where the function of
  `handler` goes, here the `.d.ts` of Bun; elsewhere they are written,
  `(request Request, server Server)` with `import { Server } from "bun"`. `serve` gives the function
  the `IncomingMessage` of `node:http`, whose `socket` has the address of the client, and returns
  the `Server`. An error of the function is one of the request, 500.
  Props passed to `handler` or `serve` are checked against the root component's type.
  Omitting a required prop or giving it an incompatible type is a compile error. The runtime
  supplies `request`, and a props callback gets the request and the typed platform arguments.

- **The result becomes the response:**

| The result   | The response                                 |
| ------------ | -------------------------------------------- |
| `Response`   | as it is                                     |
| `Html`       | `200`, `text/html; charset=utf-8`            |
| `string`     | `200`, `text/plain; charset=utf-8`           |
| `null`       | `404`                                        |
| any other    | `200`, `application/json`, `JSON.stringify`  |
| thrown error | `500`, the error in the log, not in the body |

- **`reply(value, { status, headers })`** sets the status and the headers of any of these:
  `reply(<NotFound />, { status: 404 })`,
  `reply(user, { headers: { "Cache-Control": "no-store" } })`. It gives a `Response`; a `Response`
  given to it stays as it is.

