-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecho-server-simple.ts
More file actions
80 lines (69 loc) · 2.37 KB
/
Copy pathecho-server-simple.ts
File metadata and controls
80 lines (69 loc) · 2.37 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
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* ═══════════════════════════════════════════════════════════════════════════
* FOR BENCHMARKING ONLY — NOT FOR PRODUCTION
* ═══════════════════════════════════════════════════════════════════════════
*/
const serverId = process.env.SERVER_ID || 'unknown'
const port = parseInt(process.env.SERVER_PORT || '8080')
let requestCount = 0
let startTime = Date.now()
const SENSITIVE_HEADERS = new Set([
'authorization',
'cookie',
'proxy-authorization',
'x-api-key',
'x-metrics-key',
])
const server = Bun.serve({
port,
fetch(req) {
requestCount++
const url = new URL(req.url)
const now = Date.now()
// Health endpoint
if (url.pathname === '/health') {
return new Response('OK', {
status: 200,
headers: { 'Content-Type': 'text/plain' },
})
}
// Echo endpoint with server info — redact sensitive headers
const safeHeaders: Record<string, string> = {}
for (const [name, value] of req.headers.entries()) {
safeHeaders[name] = SENSITIVE_HEADERS.has(name.toLowerCase())
? '[REDACTED]'
: value
}
const response = {
server_id: serverId,
request_count: requestCount,
uptime_ms: now - startTime,
timestamp: now,
method: req.method,
url: req.url,
headers: safeHeaders,
remote_addr: req.headers.get('x-forwarded-for') || 'unknown',
}
return new Response(JSON.stringify(response, null, 2), {
headers: {
'Content-Type': 'application/json',
},
})
},
})
console.log(`Echo server ${serverId} running on http://localhost:${port}`)
// Graceful shutdown
process.on('SIGINT', () => {
console.log(`\nShutting down echo server ${serverId}...`)
console.log(`Total requests handled: ${requestCount}`)
console.log(`Uptime: ${(Date.now() - startTime) / 1000}s`)
server.stop()
process.exit(0)
})
process.on('SIGTERM', () => {
console.log(`\nShutting down echo server ${serverId}...`)
console.log(`Total requests handled: ${requestCount}`)
console.log(`Uptime: ${(Date.now() - startTime) / 1000}s`)
server.stop()
process.exit(0)
})