-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (51 loc) · 1.98 KB
/
Copy pathserver.js
File metadata and controls
60 lines (51 loc) · 1.98 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
/**
* R2 PMO Operations Analytics — optional API layer.
*
* The site runs fully static (open index.html or deploy to GitHub Pages).
* This Express server is included to demonstrate the full-stack shape:
* it serves the same /data/portfolio-data.json as a JSON API and hosts
* the static front end. Run it only if you want the API endpoints.
*
* npm install
* npm start -> http://localhost:3000
*
* Endpoints:
* GET / static site (index.html)
* GET /api/portfolio full data object
* GET /api/metrics headline metrics only
* GET /api/cst26 CST26 fiscal assessment slice
* GET /api/odc ODC / SOA financial position slice
* GET /api/health { status: "ok" }
*/
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
const PORT = process.env.PORT || 3000;
const ROOT = path.join(__dirname, "..");
const DATA_PATH = path.join(ROOT, "data", "portfolio-data.json");
function loadData() {
// read on each request so edits to the JSON are picked up without a restart
return JSON.parse(fs.readFileSync(DATA_PATH, "utf8"));
}
app.use(express.static(ROOT));
app.get("/api/health", (_req, res) => res.json({ status: "ok" }));
app.get("/api/portfolio", (_req, res) => {
try { res.json(loadData()); }
catch (e) { res.status(500).json({ error: "data layer unavailable" }); }
});
app.get("/api/metrics", (_req, res) => {
try { res.json(loadData().headline); }
catch (e) { res.status(500).json({ error: "data layer unavailable" }); }
});
app.get("/api/cst26", (_req, res) => {
try { res.json(loadData().cst26); }
catch (e) { res.status(500).json({ error: "data layer unavailable" }); }
});
app.get("/api/odc", (_req, res) => {
try { res.json(loadData().odc); }
catch (e) { res.status(500).json({ error: "data layer unavailable" }); }
});
app.listen(PORT, () => {
console.log(`R2 Ops Analytics running at http://localhost:${PORT}`);
});