-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (49 loc) · 1.78 KB
/
server.js
File metadata and controls
60 lines (49 loc) · 1.78 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
/**
* eVue Risk Score Cloud Dashboard Server
* Reads results from EtechInc/evue-risk-results on GitHub.
* Deploy to Railway — set GITHUB_TOKEN env var in Railway dashboard.
*/
const express = require('express')
const path = require('path')
const app = express()
const PORT = process.env.PORT || 3001
const RESULTS_REPO = 'EtechInc/evue-risk-results'
const RESULTS_FILE = 'results.json'
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
// Simple in-memory cache (5 min TTL) to avoid hammering GitHub API
let cache = { data: null, fetchedAt: 0 }
const CACHE_TTL_MS = 5 * 60 * 1000
async function fetchResultsFromGitHub() {
const now = Date.now()
if (cache.data && (now - cache.fetchedAt) < CACHE_TTL_MS) {
return cache.data
}
const url = `https://api.github.com/repos/${RESULTS_REPO}/contents/${RESULTS_FILE}`
const headers = {
'User-Agent': 'evue-risk-dashboard',
Accept: 'application/vnd.github.v3.raw',
...(GITHUB_TOKEN ? { Authorization: `token ${GITHUB_TOKEN}` } : {}),
}
const res = await fetch(url, { headers })
if (!res.ok) throw new Error(`GitHub returned ${res.status} for results.json`)
const data = await res.json()
cache = { data, fetchedAt: now }
return data
}
app.use(express.static(path.join(__dirname, 'public')))
app.get('/api/results', async (req, res) => {
try {
const data = await fetchResultsFromGitHub()
res.json(data)
} catch (err) {
res.status(500).json({ error: err.message })
}
})
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
app.listen(PORT, () => {
console.log(`eVue Risk Dashboard running on port ${PORT}`)
console.log(`Reading results from: EtechInc/evue-risk-results`)
if (!GITHUB_TOKEN) console.warn('WARNING: GITHUB_TOKEN not set — may fail on private repo')
})