mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
refactor(server): resolve the data directory in one place
The data and uploads directories were derived independently in about ten files, each counting `..` hops from its own `__dirname`, and the file logger used `process.cwd()` instead. Moving a file or starting the server from another directory quietly moved where secrets, backups or logs went, and the backup code named `data/travel.db` directly, so with TREK_DB_FILE set a backup archived and a restore replaced a file the connection never opened. `resolveDataPaths()` in app-config now anchors data/, uploads/, backups, tmp, logs, the JWT secret and encryption key files and the database file (TREK_DB_FILE when set, else data/travel.db) on the server package root. Code that runs before the container calls it directly; Nest classes inject the new `DataPathsService`. Every previous anchor uses it, and the resolved paths are unchanged for the image and for `npm run dev`.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { readEnv } from '../app-config';
|
||||
import { resolveDataPaths } from '../app-config/data-paths';
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
@@ -19,16 +20,10 @@ export function resolveDbPath(): string {
|
||||
// parallel forks can't race on the same file or share migration state.
|
||||
if (readEnv().app.isTest) return ':memory:';
|
||||
|
||||
const explicit = readEnv().db.trekDbFile;
|
||||
if (explicit) {
|
||||
// Explicit DB file (used by the Playwright E2E harness to run against an
|
||||
// isolated, throwaway database instead of the real data/travel.db).
|
||||
const dir = path.dirname(explicit);
|
||||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
||||
return explicit;
|
||||
}
|
||||
|
||||
const dataDir = path.join(__dirname, '../../data');
|
||||
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
|
||||
return path.join(dataDir, 'travel.db');
|
||||
// TREK_DB_FILE (the Playwright E2E harness runs against a throwaway file this
|
||||
// way) or data/travel.db, both resolved by the one data layout.
|
||||
const file = resolveDataPaths().dbFile;
|
||||
const dir = path.dirname(file);
|
||||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
||||
return file;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user