# Module /http/server

High-performance Axum/Tokio HTTP/1.1 & HTTP/2 server supporting static serving, SSE, multipart forms, and WebSockets.

```tangu
(use /http/server)
```

## Functions & symbols

### close-websocket

`(close-websocket channel [options]) -> None`

Cleanly closes a server WebSocket connection with an optional close status code and reason.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `channel` | `WS-Channel` | no | Connected server WebSocket channel |
| `options` | `Map` | yes | Optional close frame {:code Int :reason String} |

```tangu
(server/close-websocket ws {:code 1000 :reason "bye"})
```

### content-type-of

`(content-type-of msg [default]) -> (Or String None)`

Returns the `Content-Type` header value of an HTTP message.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `msg` | `Map` | no | HTTP request or response Map |
| `default` | `Any` | yes | Optional default value |

```tangu
(server/content-type-of req)
```

### form-of

`(form-of req) -> Map`

Parses an `application/x-www-form-urlencoded` or `multipart/form-data` body into a field Map.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Map` | no | Incoming HTTP request Map |

```tangu
(let fields (server/form-of req))
```

### handle-static

`(handle-static options) -> Func`

Creates an HTTP handler serving static files from `:dir` with safe path traversal checks and MIME detection.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `options` | `Map` | no | Static handler config {:dir String [:prefix String :fallback String]} |

```tangu
(let assets (server/handle-static {:dir "./public" :prefix "/assets"}))
```

### header-of

`(header-of msg name [default]) -> (Or String None)`

Performs a case-insensitive lookup of HTTP header `name`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `msg` | `Map` | no | HTTP request or response Map |
| `name` | `String | KeySymbol` | no | Case-insensitive header name |
| `default` | `Any` | yes | Optional default value |

```tangu
(server/header-of req "authorization")
```

### json-of

`(json-of msg) -> Any`

Decodes the JSON body of an HTTP request or response Map into native Tangu data.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Map` | no | Incoming HTTP request Map |

```tangu
(let body (server/json-of req))
```

### path-of

`(path-of req) -> String`

Extracts the decoded URI path segment from an incoming request Map.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Map` | no | Incoming HTTP request Map |

```tangu
(let path (server/path-of req))
```

### query-of

`(query-of req) -> Map`

Parses URL query parameters into a Map (promoting repeated keys into Arrays).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Map` | no | Incoming HTTP request Map |

```tangu
(let q (server/query-of req))
```

### query-string-of

`(query-string-of req) -> String`

Returns the raw URL query string (excluding `?`) from a request Map.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Map` | no | Incoming HTTP request Map |

```tangu
(let qs (server/query-string-of req))
```

### read-form-urlencoded

`(read-form-urlencoded body) -> Map`

Parses a URL-encoded form string (`a=1&b=2`) into a Map.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `body` | `String` | no | URL-encoded form body |

```tangu
(server/read-form-urlencoded "a=1&b=2")
```

### read-multipart-form-data

`(read-multipart-form-data body content-type) -> Map`

Parses a `multipart/form-data` request body including uploaded file Maps (`:filename`, `:content-type`, `:bytes`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `body` | `Slab | String` | no | Raw multipart request body |
| `content-type` | `String` | no | Content-Type header with multipart boundary |

```tangu
(server/read-multipart-form-data req:body (server/content-type-of req))
```

### receive-websocket-message

`(receive-websocket-message channel) -> (Or Map None)`

Awaits and returns the next frame from a server WebSocket `{:type :text|:binary|:ping|:pong|:close :data ...}`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `channel` | `WS-Channel` | no | Connected server WebSocket channel |

```tangu
(let msg (server/receive-websocket-message ws))
```

### send-sse-event

`(send-sse-event channel data [options]) -> Bool`

Pushes an SSE event over `channel` to the connected client (returns `false` if disconnected).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `channel` | `SSE-Channel` | no | Open SSE channel handle |
| `data` | `String` | no | Event payload string |
| `options` | `Map` | yes | Optional {:event :id :retry} |

```tangu
(server/send-sse-event ch "ping" {:event "heartbeat"})
```

### send-websocket-message

`(send-websocket-message channel data) -> None`

Sends a text or binary message over a connected server WebSocket channel.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `channel` | `WS-Channel` | no | Connected server WebSocket channel |
| `data` | `String | Slab` | no | Text or binary WebSocket frame |

```tangu
(server/send-websocket-message ws "ok")
```

### serve

`(serve handler options [rt]) -> None`

Starts the multi-threaded HTTP server on `:host` and `:port`, dispatching every request to `handler`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `handler` | `Func` | no | Request handler `(Func req ...)` returning a response Map |
| `options` | `Map` | no | Server config {:host "127.0.0.1" :port 8080} |
| `rt` | `AsyncRuntime` | yes | Optional Async-Runtime instance (defaults to `*runtime*`) |

```tangu
(server/serve app-handler {:host "0.0.0.0" :port 8080})
```

### sse-response

`(sse-response on-connect) -> Map`

Creates an HTTP Server-Sent Events streaming response descriptor.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `on_connect` | `Func` | no | Callback `(Func channel ...)` emitting SSE events |

```tangu
(server/sse-response (Func ch (server/send-sse-event ch "hello")))
```

### websocket-response

`(websocket-response on-connect) -> Map`

Creates an HTTP 101 WebSocket upgrade response descriptor.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `on_connect` | `Func` | no | WebSocket lifecycle callback `(Func ws ...)` |

```tangu
(server/websocket-response (Func ws (server/send-websocket-message ws "hi")))
```
