-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (73 loc) · 2.08 KB
/
server.js
File metadata and controls
88 lines (73 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const rateLimit = require('express-rate-limit');
const axios = require('axios');
const packageJson = require('./package.json');
require('dotenv').config();
function logWithTime(message) {
console.log(`[${new Date().toISOString()}] ${message}`);
}
const app = express();
const PORT = process.env.PORT || 3000;
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: {
error: 'Too many requests from this IP, please try again after 15 minutes.'
},
standardHeaders: true,
legacyHeaders: false,
});
const priceCache = {};
const CACHE_DURATION = 60 * 1000;
app.use(express.json());
app.get('/health', (req, res) => {
res.json({
status: 'OK',
timestamp: new Date().toISOString()
});
});
app.use('/api/v1/', limiter);
async function fetchPrices() {
try {
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd,eur');
return response.data;
} catch (error) {
console.error('Error fetching prices:', error.message);
throw error;
}
}
app.get('/api/v1/prices', async (req, res) => {
const now = Date.now();
if (priceCache.data && (now - priceCache.timestamp) < CACHE_DURATION) {
return res.json({
...priceCache.data,
cached: true,
timestamp: new Date(priceCache.timestamp).toISOString()
});
}
try {
const prices = await fetchPrices();
priceCache.data = prices;
priceCache.timestamp = now;
res.json({
...prices,
cached: false,
timestamp: new Date(now).toISOString()
});
} catch (error) {
res.status(httpStatusCodes.INTERNAL_SERVER_ERROR).json({
error: 'Failed to fetch prices',
message: error.message
});
}
});
app.get('/api/v1/test', (req, res) => {
res.json({ message: 'Test endpoint working' });
});
app.get('/api/v1/version', (req, res) => {
res.json({ version: packageJson.version });
});
app.listen(PORT, () => {
logWithTime(`Server running on port ${PORT}`);
logWithTime(`Health check available at: http://localhost:${PORT}/health`);
});