-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (111 loc) · 3.41 KB
/
index.js
File metadata and controls
140 lines (111 loc) · 3.41 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import express from 'express';
import rateLimit from 'express-rate-limit';
import priceFetcher from './priceFetcher.js';
import priceCache from './priceCache.js';
const PORT = process.env.PORT || 8888;
const app = express();
// CORS middleware
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
// IP-based rate limiting
const limiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 100, // 100 requests per minute per IP
standardHeaders: true,
legacyHeaders: false,
message: 'Too many requests from this IP, please try again later.'
});
app.use(limiter);
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// Price endpoint - matches Coinbase format
app.get('/v2/prices/:pair/spot', (req, res) => {
const { pair } = req.params;
const cached = priceCache.get(pair);
if (!cached) {
return res.status(404).json({ error: 'Currency pair not found' });
}
if (cached.isStale) {
return res.status(503).json({
error: 'Service temporarily unavailable - data too stale',
data: cached.data
});
}
// Match Coinbase format exactly: {"data":{"amount":"...","base":"BTC","currency":"..."}}
res.json({ data: cached.data });
});
// Start polling loop
let pollingInterval = null;
let isPollingActive = true;
async function pollPrices() {
if (!isPollingActive) return;
try {
const summary = await priceFetcher.fetchAllPrices();
if (summary.rateLimited) {
console.warn(`[Poll] Rate limited - ${summary.success} succeeded, ${summary.failed} failed`);
} else if (summary.failed > 0) {
console.warn(`[Poll] ${summary.success} succeeded, ${summary.failed} failed`);
} else {
console.log(`[Poll] Successfully updated ${summary.success} prices`);
}
} catch (error) {
console.error(`[Poll] Error during price fetch:`, error);
}
if (isPollingActive) {
scheduleNext();
}
}
function scheduleNext() {
if (!isPollingActive) return;
const interval = priceFetcher.getCurrentInterval();
pollingInterval = setTimeout(() => {
pollPrices();
}, interval);
}
function startPolling() {
// Initial fetch
pollPrices();
}
// Graceful shutdown
let isShuttingDown = false;
function gracefulShutdown(signal) {
if (isShuttingDown) return;
isShuttingDown = true;
console.log(`\n[Shutdown] Received ${signal}, shutting down gracefully...`);
// Stop polling
isPollingActive = false;
if (pollingInterval) {
clearTimeout(pollingInterval);
pollingInterval = null;
}
// Close server
server.close(() => {
console.log('[Shutdown] HTTP server closed');
process.exit(0);
});
// Force shutdown after 10 seconds
setTimeout(() => {
console.error('[Shutdown] Forced shutdown after timeout');
process.exit(1);
}, 10000);
}
process.once('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.once('SIGINT', () => gracefulShutdown('SIGINT'));
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('[UnhandledRejection]', reason);
});
// Start server
const server = app.listen(PORT, () => {
console.log(`[Server] Listening on port ${PORT}`);
startPolling();
});