feat(server): healthz, testable app, local dev script (P0-3)

This commit is contained in:
Benjamin Oezcan
2026-11-19 01:40:31 +01:00
parent 35348324d6
commit fa68ef620d
8 changed files with 1465 additions and 16 deletions

View File

@@ -9,10 +9,18 @@ const rtdb = require('./rtdb');
const eco = require('./economy');
const { getTree, putTree, patchTree, pushTree, delTreeAt, norm } = require('./db');
const pkg = require('./package.json');
const STARTED = Date.now();
const app = express();
app.use(express.json({ limit: '2mb', strict: false })); // strict:false so scalar values (e.g. a name string) parse too
app.use(auth.auth); // sets req.uid / req.user (null if not logged in)
// --- health (deploy checks, monitoring) --------------------------------------------------------------
app.get('/healthz', (_req, res) => res.json({
ok: true, version: pkg.version, commit: process.env.GIT_COMMIT || null, uptime: Math.round((Date.now() - STARTED) / 1000),
}));
function run(res, fn) { try { res.json(fn()); } catch (e) { res.status(e.status || 500).json({ error: e.code || 'ERROR' }); } }
// --- accounts --------------------------------------------------------------------------------------
@@ -81,5 +89,10 @@ app.use(express.static(path.join(__dirname, '..', 'game'), {
},
}));
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log('Robot Clash server on :' + PORT));
// started directly (node app.js) -> listen; required by tests -> they call listen() themselves
function listen(port = process.env.PORT || 8080) {
return new Promise(resolve => { const srv = app.listen(port, () => resolve(srv)); });
}
if (require.main === module) listen().then(srv => console.log('Robot Clash server on :' + srv.address().port));
module.exports = { app, listen };