-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (28 loc) · 1.17 KB
/
Copy pathindex.js
File metadata and controls
36 lines (28 loc) · 1.17 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
const express = require('express');
const { fetchGitHubData } = require('./src/fetcher');
const { generateSVG } = require('./src/svg-generator');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/api', async (req, res) => {
const username = req.query.username;
if (!username) {
return res.status(400).send('Please provide a username, e.g., ?username=raghu24k');
}
try {
const data = await fetchGitHubData(username);
const svg = await generateSVG(data);
res.setHeader('Content-Type', 'image/svg+xml');
// Tell GitHub/browsers to cache for 15 minutes max
res.setHeader('Cache-Control', 'public, max-age=900, s-maxage=900, stale-while-revalidate=3600');
res.send(svg);
} catch (error) {
res.status(500).send(`<svg xmlns="http://www.w3.org/2000/svg" width="400" height="100"><text x="10" y="50" fill="red">${error.message}</text></svg>`);
}
});
app.get('/', (req, res) => {
res.send('GitHub Stats API is running. Use /api?username=YOUR_USERNAME to get your stats SVG.');
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
module.exports = app;