-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
67 lines (56 loc) · 2.08 KB
/
server.ts
File metadata and controls
67 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Fastify example — server.
*
* Fastify owns the HTTP routes; `attachArcpToFastify` mounts the ARCP
* upgrade handler on the underlying `app.server` (the Node `http.Server`
* Fastify created). Fastify's structured logger and request-id generation
* are shown by the `/health` route.
*/
import Fastify from "fastify";
import { attachArcpToFastify } from "@arcp/fastify";
import { ARCPServer, StaticBearerVerifier } from "@arcp/sdk";
const PORT = Number(process.env.ARCP_DEMO_PORT ?? 7897);
const TOKEN = process.env.ARCP_DEMO_TOKEN ?? "demo-token";
const ALLOWED_HOSTS = ["localhost", "127.0.0.1"];
async function main(): Promise<void> {
// Fastify's `genReqId` plus its pino logger give every HTTP request a
// correlation id in stdout — visible alongside ARCP traffic.
const app = Fastify({
logger: { level: "info" },
genReqId: () => `req_${Math.random().toString(36).slice(2, 10)}`,
});
app.get("/health", async (req, _reply) => {
req.log.info({ route: "health" }, "health check");
return { status: "ok", arcp: "/arcp", req_id: req.id };
});
const arcp = new ARCPServer({
runtime: { name: "fastify-demo", version: "1.0.0" },
capabilities: { encodings: ["json"], agents: ["echo"] },
bearer: new StaticBearerVerifier(new Map([[TOKEN, { principal: "demo" }]])),
});
arcp.registerAgent("echo", async (input, ctx) => {
await ctx.log("info", "echoing");
return { echoed: input };
});
await app.listen({ host: "127.0.0.1", port: PORT });
const upgrade = attachArcpToFastify(app, {
path: "/arcp",
allowedHosts: ALLOWED_HOSTS,
onTransport: (transport) => arcp.accept(transport),
});
app.log.info(`ARCP: ws://127.0.0.1:${PORT}/arcp`);
console.log("Press Ctrl+C to stop.");
const shutdown = async (): Promise<void> => {
console.log("\nshutting down...");
await upgrade.close();
await arcp.close();
await app.close();
process.exit(0);
};
process.on("SIGINT", () => void shutdown());
process.on("SIGTERM", () => void shutdown());
}
void main().catch((err) => {
console.error(err);
process.exit(1);
});