-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
40 lines (32 loc) · 1.04 KB
/
server.ts
File metadata and controls
40 lines (32 loc) · 1.04 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
/// <reference types="node" />
import { Hono } from 'hono';
import { routePath } from 'hono/route';
import { serve } from '@hono/node-server';
import { createEmitter, type HonoXrayEnv } from '@stainlessdev/xray-emitter/hono';
const app = new Hono<HonoXrayEnv>();
const xray = createEmitter(
{
serviceName: 'xray-example',
endpointUrl: process.env.STAINLESS_XRAY_ENDPOINT_URL,
// Read from response headers when finalized (default: Request-Id).
requestId: { header: 'request-id' },
},
{ routePath },
);
app.use('*', xray);
app.use('*', async (c, next) => {
c.get('xray')?.setActor('tenant-123', 'user-123');
await next();
});
app.post('/hello/:subject', (c) => {
const subject = c.req.param('subject');
return c.json({ message: `Hello ${subject}` });
});
const port = Number(process.env.PORT) || 3000;
const server = serve({ fetch: app.fetch, port });
const shutdown = async () => {
server.close();
await xray.shutdown();
};
process.once('SIGINT', () => void shutdown());
process.once('SIGTERM', () => void shutdown());