benjivers/notes Public Public: anyone can read this; nobody can change it.

Files otel
89 lines · 3.0 KB · ~884 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 { sessionWho } from "./accounts.ts";
13
import { html, readCookie, text } from "./http.ts";
14
import { SESSION_COOKIE } from "./routes.ts";
15
import { match } from "./routes.ts";
16
import { door } from "./security.ts";
17
import { who } from "./who.ts";
18
import { traceServe } from "./otel.ts";
19
20
export interface ServeOptions {
21
  readonly port: number;
22
  readonly hostname?: string | undefined;
23
  readonly dataDir: string;
24
  readonly identityKey: string;
25
}
26
27
export function serve(options: ServeOptions) {
28
  const db = openDb(options.dataDir);
29
  return Bun.serve(traceServe({
30
    port: options.port,
31
    hostname: options.hostname ?? "127.0.0.1",
32
    // A connection that says nothing for a minute is dropped. The event
33
    // stream heartbeats every 25 seconds to stay well inside that.
34
    idleTimeout: 60,
35
    async fetch(request, server) {
36
      const url = new URL(request.url);
37
      // The forge's signed word first; else the app's own session, if any.
38
      const signed = who(request, options.identityKey);
39
      const me =
40
        signed.role === "anon" ? sessionWho(db, readCookie(request, SESSION_COOKIE)) : signed;
41
      const refused = door(
42
        db,
43
        request,
44
        url,
45
        me,
46
        server.requestIP(request)?.address ?? "?",
47
      );
48
      if (refused !== undefined) return head(request, refused);
49
      const found = match(
50
        request.method === "HEAD" ? "GET" : request.method,
51
        url.pathname,
52
      );
53
      if (found === undefined) return text("not found", 404);
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
      console.error(error);
68
      return html("<h1>Something went wrong</h1>", 500);
69
    },
70
  }));
71
}
72
73
// HEAD is GET without the body: a health check sees the app up.
74
const head = (request: Request, response: Response): Response =>
75
  request.method === "HEAD"
76
    ? new Response(null, { status: response.status, headers: response.headers })
77
    : response;
78
79
if (import.meta.main) {
80
  const server = serve({
81
    port: Number(process.env.PORT ?? 8080),
82
    hostname: process.env.HOST,
83
    dataDir,
84
    identityKey,
85
  });
86
  console.log(
87
    `${title} listening on http://${server.hostname}:${server.port} (${where})`,
88
  );
89
}
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts