-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (64 loc) · 2.09 KB
/
server.js
File metadata and controls
72 lines (64 loc) · 2.09 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
import express from 'express';
import Database from 'better-sqlite3';
import cors from 'cors';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const db = new Database(join(__dirname, 'gitlab-wrapped.db'));
// Initialize database
db.exec(`
CREATE TABLE IF NOT EXISTS stats_cache (
token_hash TEXT,
year INTEGER,
data TEXT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (token_hash, year)
)
`);
const app = express();
app.use(cors());
app.use(express.json({ limit: '50mb' }));
// Helper to hash token (simple and effective for local cache)
const getTokenHash = (token) => {
let hash = 0;
for (let i = 0; i < token.length; i++) {
const char = token.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return hash.toString();
};
// Get cached stats
app.get('/api/cache/:tokenHash/:year', (req, res) => {
const { tokenHash, year } = req.params;
try {
const row = db.prepare('SELECT data FROM stats_cache WHERE token_hash = ? AND year = ?')
.get(tokenHash, parseInt(year));
if (row) {
res.json({ success: true, data: JSON.parse(row.data) });
} else {
res.json({ success: false, message: 'Not found' });
}
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
// Save stats to cache
app.post('/api/cache', (req, res) => {
const { tokenHash, year, data } = req.body;
try {
const stmt = db.prepare(`
INSERT OR REPLACE INTO stats_cache (token_hash, year, data, updated_at)
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
`);
stmt.run(tokenHash, year, JSON.stringify(data));
res.json({ success: true });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
const PORT = 3001;
app.listen(PORT, '0.0.0.0', () => {
console.log(`🚀 Cache server running at http://0.0.0.0:${PORT}`);
console.log(`📡 Accessible on LAN for shared use`);
});