An open API service indexing awesome lists of open source software.

https://github.com/redraskal/gateway

Experimental Bun web framework.
https://github.com/redraskal/gateway

bun framework http-server typescript web

Last synced: 10 months ago
JSON representation

Experimental Bun web framework.

Awesome Lists containing this project

README

          

# Gateway

**This is a work in progress. The API is subject to change until a stable version is released.**

## Features:

- Server-side rendered HTML (SSR)
- [Next.js-style filesystem router](https://bun.sh/docs/api/file-system-router) (pages/)
- Class-based routes
- JSON responses on all routes via application/json Accept header
- JSON file routes (example: /articles/bun.json)
- Automatic JSON error responses
- Zod Request body parsing (zod())
- Static file serving & caching (public/)
- Optional entrypoint (src/index.ts)
- Optional 404 page (pages/404.ts)
- Route file generator via CLI (bun gen)

```ts
import { type Data, Route, html, parse } from "gateway";

export default class implements Route {
async data(req: Request) {
// parse JSON or form Request body
const data = await parse<{
name: string;
}>(req);
return {
time: new Date(Date.now()).toLocaleString(),
name: data?.name || "world",
_secret: "yes", // omitted in JSON responses
};
}

head(data: Data) {
return html`Hello ${data.name}!`;
}

body(data: Data) {
return html`

Hello ${data.name} at ${data.time}!



Name



`;
}
}
```

## To install Bun:

```bash
curl -fsSL https://bun.sh/install | bash
```

## To create a project:

```bash
bun create redraskal/gateway-template {dir}
```

## To run a development server:

```bash
bun dev
```

## To run a production server:

```bash
bun start
```

## Environmental variables:

| Name | Description | Default |
| ------------------- | ------------------------------------------ | --------------------------------------------- |
| GATEWAY_HOSTNAME | HTTP server hostname | 0.0.0.0 |
| GATEWAY_PORT | HTTP server port | 3000 |
| GATEWAY_ENV | Environment | prod with `bun start`, dev with `bun run dev` |
| GATEWAY_DEBUG | console.debug output | false |
| GATEWAY_CACHE_TTL | Cache-Control max age | 3600 |
| GATEWAY_JSON_ERRORS | Whether to output errors in JSON responses | true |

## To generate a route:

```bash
bun gen {name}

bun gen test
# or
bun gen test.ts
# 📝 pages/test.ts created.
```

The new route will automatically open in Visual Studio Code.

This project was created using `bun init` in bun v0.6.13. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.