pike/glyph Public Public: anyone can read this; nobody can change it.

forked from apps/glyph

workbenchthis machine
Files acd87b6b6146da11e0c9272c92a02a8e707adf4a
90 lines · 3.1 KB · ~917 tok Raw History
1
/**
2
 * The process the forge runs. `serve` opens the database and listens:
3
 * every request gets the floor (`http.ts`), an identity (`who.ts`), the
4
 * door (`security.ts`) and one route (`routes.ts`) — so a request from
5
 * another site, from someone going too fast, or from someone the contract
6
 * turns away is refused before a byte of it is read. Run directly, it
7
 * serves what the forge said; a test calls `serve` with its own.
8
 */
9
10
import { openDb } from "./db.ts";
11
import { dataDir, identityKey, title, where } from "./env.ts";
12
import { html, text } from "./http.ts";
13
import { serveSpan } from "./otel.ts";
14
import { match } from "./routes.ts";
15
import { door } from "./security.ts";
16
import { who } from "./who.ts";
17
18
export interface ServeOptions {
19
  readonly port: number;
20
  readonly hostname?: string | undefined;
21
  readonly dataDir: string;
22
  readonly identityKey: string;
23
}
24
25
export function serve(options: ServeOptions) {
26
  const db = openDb(options.dataDir);
27
  return Bun.serve({
28
    port: options.port,
29
    hostname: options.hostname ?? "127.0.0.1",
30
    // A connection that says nothing for a minute is dropped. The event
31
    // stream heartbeats every 25 seconds to stay well inside that.
32
    idleTimeout: 60,
33
    // Every request is a span (src/otel.ts), a child of the forge's: it
34
    // shows in the platform's traces with its route, status and timing.
35
    // Wrap slow work in `span("name", () => ...)` to see it there too.
36
    fetch: (request, server) =>
37
      serveSpan(request, async (span) => {
38
        const url = new URL(request.url);
39
        const me = who(request, options.identityKey);
40
        const refused = door(
41
          db,
42
          request,
43
          url,
44
          me,
45
          server.requestIP(request)?.address ?? "?",
46
        );
47
        if (refused !== undefined) return head(request, refused);
48
        const found = match(
49
          request.method === "HEAD" ? "GET" : request.method,
50
          url.pathname,
51
        );
52
        if (found === undefined) return text("not found", 404);
53
        span.set("http.route", found.route.path);
54
        const response = await found.route.handle({
55
          request,
56
          url,
57
          db,
58
          who: me,
59
          params: found.params,
60
        });
61
        return head(request, response);
62
      }),
63
    // A bug answers with the floor and nothing else: never the runtime's
64
    // own error page, which carries source and paths, and never without
65
    // the headers. The detail goes to the log, where it belongs.
66
    error(error) {
67
      // Inside the generated app, which has no forge log module.
68
      console.error(error);
69
      return html("<h1>Something went wrong</h1>", 500);
70
    },
71
  });
72
}
73
74
// HEAD is GET without the body: a health check sees the app up.
75
const head = (request: Request, response: Response): Response =>
76
  request.method === "HEAD"
77
    ? new Response(null, { status: response.status, headers: response.headers })
78
    : response;
79
80
if (import.meta.main) {
81
  const server = serve({
82
    port: Number(process.env.PORT ?? 8080),
83
    hostname: process.env.HOST,
84
    dataDir,
85
    identityKey,
86
  });
87
  console.log(
88
    `${title} listening on http://${server.hostname}:${server.port} (${where})`,
89
  );
90
}
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts