Companion to ff92706 (drift test fix). The inline handler moves to a
module exporting { buildRouter, getVersion, getName }, pre-built once
at startup and mounted bare on apiRouter — the exact shape the drift
test walker now recognizes. Dockerfile builder stage switches to
npm ci --omit=dev for deterministic builds. Tests: 12/12 across the
three new/updated suites; full suite 1837/1837.
52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
/**
|
|
* Version route — exposes the running application version and runtime metadata.
|
|
*
|
|
* The version comes from package.json at module load time so the response
|
|
* always matches the running code. Extracted from src/app.js into its own
|
|
* module so production wiring and tests share the same code path.
|
|
*/
|
|
const express = require('express');
|
|
|
|
let appVersion = '0.0.0';
|
|
let appName = 'dashcaddy-api';
|
|
try {
|
|
const pkg = require('../package.json');
|
|
if (pkg && pkg.version) appVersion = pkg.version;
|
|
if (pkg && pkg.name) appName = pkg.name;
|
|
} catch (_) {
|
|
/* package.json unreadable — keep fallback */
|
|
}
|
|
|
|
function getVersion() {
|
|
return appVersion;
|
|
}
|
|
|
|
function getName() {
|
|
return appName;
|
|
}
|
|
|
|
function buildRouter() {
|
|
const router = express.Router();
|
|
router.get('/version', (req, res) => {
|
|
res.json({
|
|
success: true,
|
|
name: appName,
|
|
version: appVersion,
|
|
node: process.version,
|
|
platform: process.platform,
|
|
arch: process.arch,
|
|
uptime: process.uptime(),
|
|
instanceId: process.env.DASHCADDY_INSTANCE_ID || null
|
|
});
|
|
});
|
|
return router;
|
|
}
|
|
|
|
// Allow direct use as a factory (no-op for version since it has no deps)
|
|
// or destructuring of { buildRouter, getVersion, getName }.
|
|
module.exports = module.exports.default || module.exports;
|
|
module.exports.buildRouter = buildRouter;
|
|
module.exports.getVersion = getVersion;
|
|
module.exports.getName = getName;
|
|
module.exports.default = function factory() { return buildRouter(); };
|