Refactor transit_api into a repo/routes module split
Split the 5,441-line index.js into an 86-line bootstrap, thin domain
route modules, shared lib/ helpers, and a repo/ layer that isolates the
DEV_MOCK branches (repo/*.mock.js). Repo methods return {statusCode, body};
routes are one-line wrappers. Pin search_path to "transit.bus" and
document the /bus/ prefix with legacy root aliases (Deprecation header).
Parity verified: 57/57 byte-identical on the stub differential harness
plus 24/24 live read-only responses vs the original against real
PostgreSQL. The only intentional behavior change: mock reorder/delete
bus-line endpoints no longer require a live DB (previously 500 in
DEV_MOCK=1 without PG). DEV_MOCK gate and node --check clean.
This commit is contained in:
+22
-3
@@ -6,8 +6,8 @@ const pool = new Pool({
|
||||
user: process.env.PG_USER,
|
||||
password: process.env.PG_PASS,
|
||||
database: process.env.PG_DB,
|
||||
// Ensure unqualified table names resolve to transit schema first.
|
||||
options: '-c search_path=transit,public',
|
||||
// Ensure unqualified table names resolve to the "transit.bus" schema first.
|
||||
options: '-c search_path="transit.bus",public',
|
||||
max: 10,
|
||||
idleTimeoutMillis: 30000
|
||||
});
|
||||
@@ -17,7 +17,26 @@ pool.query('SELECT 1')
|
||||
.catch(err => {
|
||||
console.error('PostgreSQL connection FAILED');
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
const url = process.env.ERROR_LOG_API_URL || 'https://error.xtransit.testinstance.nl/error_log';
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), 5000);
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-internal-api-key': process.env.INTERNAL_API_KEY || '',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
service: 'transit_api',
|
||||
partofservice: 'startup: db connection',
|
||||
error: err.stack || err.message || String(err),
|
||||
}),
|
||||
signal: controller.signal,
|
||||
}).catch(() => {})
|
||||
.finally(() => {
|
||||
clearTimeout(timer);
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user