refactor(backend): add refactored runner for new backend architecture

This commit is contained in:
Haileyesus
2026-03-18 10:19:37 +03:00
parent 7df21556dd
commit bbb461f7c2
6 changed files with 65 additions and 12 deletions

View File

@@ -1,3 +1,5 @@
import './config/load-env-vars.js';
import { pathToFileURL } from 'url'; import { pathToFileURL } from 'url';
import { getRuntimePaths } from '@/config/runtime.js'; import { getRuntimePaths } from '@/config/runtime.js';
@@ -6,14 +8,25 @@ import { logger } from '@/shared/utils/logger.js';
export function createServerApplication(): ServerApplication { export function createServerApplication(): ServerApplication {
const runtimePaths = getRuntimePaths(); const runtimePaths = getRuntimePaths();
return { return {
runtimePaths, runtimePaths,
start: async () => { start: async () => {
logger.info('Bootstrapping backend via legacy runtime bridge', { // ----------------------------------------------
legacyRuntime: runtimePaths.legacyRuntimePath, // Legacy backend Runner
// logger.info('Bootstrapping backend via legacy runtime bridge', {
// legacyRuntime: runtimePaths.legacyRuntimePath,
// });
// await import(pathToFileURL(runtimePaths.legacyRuntimePath).href);
// ----------------------------------------------
// ----------------------------------------------
// Refactor backend Runner
logger.info('Bootstrapping backend via refactor runtime', {
refactorRuntime: runtimePaths.refactorRuntimePath,
}); });
await import(pathToFileURL(runtimePaths.legacyRuntimePath).href); await import(pathToFileURL(runtimePaths.refactorRuntimePath).href);
}, },
}; };
} }

View File

@@ -0,0 +1,30 @@
// Load environment variables from .env before other imports execute.
import fs from 'fs';
import os from 'os';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
try {
const envPath = path.join(__dirname, '../.env');
const envFile = fs.readFileSync(envPath, 'utf8');
envFile.split('\n').forEach(line => {
const trimmedLine = line.trim();
if (trimmedLine && !trimmedLine.startsWith('#')) {
const [key, ...valueParts] = trimmedLine.split('=');
if (key && valueParts.length > 0 && !process.env[key]) {
process.env[key] = valueParts.join('=').trim();
}
}
});
} catch (e: any) {
console.log('No .env file found or error reading it:', e.message);
}
// Set a default DATABASE_PATH if not already set by .env to ~/.cloudcli/auth.db
if (!process.env.DATABASE_PATH) {
process.env.DATABASE_PATH = path.join(os.homedir(), '.cloudcli', 'auth.db');
}

View File

@@ -6,9 +6,15 @@ import type { RuntimePaths } from '@/shared/types/app.js';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
const RUN_REFACTOR_WITH_SRC = true;
export function getRuntimePaths(): RuntimePaths { export function getRuntimePaths(): RuntimePaths {
const serverSrcDir = path.resolve(__dirname, '..'); const serverSrcDir = path.resolve(__dirname, '..');
const serverDir = path.resolve(serverSrcDir, '..'); const serverDir = path.resolve(serverSrcDir, '..');
const refactorRuntimePath =
RUN_REFACTOR_WITH_SRC
? path.join(serverDir, 'src', 'runner.ts')
: path.join(serverDir, 'dist', 'runner.js');
return { return {
serverSrcDir, serverSrcDir,
@@ -16,5 +22,6 @@ export function getRuntimePaths(): RuntimePaths {
projectRoot: path.resolve(serverDir, '..'), projectRoot: path.resolve(serverDir, '..'),
legacyRuntimePath: path.join(serverDir, 'index.js'), legacyRuntimePath: path.join(serverDir, 'index.js'),
bootstrapEntrypointPath: path.join(serverDir, 'dist', 'bootstrap.js'), bootstrapEntrypointPath: path.join(serverDir, 'dist', 'bootstrap.js'),
refactorRuntimePath
}; };
} }

6
server/src/runner.ts Normal file
View File

@@ -0,0 +1,6 @@
import { userDb } from "@/shared/database/repositories/users.js";
console.log("----------------Hello there, Refactored Runner!-------------------");
console.log("User db initialized");
console.log("First user:", userDb.getFirstUser());

View File

@@ -30,17 +30,12 @@ const __dirname = path.dirname(__filename);
* to the legacy location inside the server/database/ folder. * to the legacy location inside the server/database/ folder.
* *
* Priority: * Priority:
* 1. DATABASE_PATH environment variable (set by cli.js or load-env.js) * 1. DATABASE_PATH environment variable (set by cli.js or load-env-vars.js)
* 2. Legacy path: server/database/auth.db * 2. Legacy path: server/database/auth.db
*/ */
function resolveDatabasePath(): string { function resolveDatabasePath(): string {
if (process.env.DATABASE_PATH) { // process.env.DATABASE_PATH is set by load-env-vars.js to either the .env value or a default(~/.cloudcli/auth.db) in the user's home directory.
return process.env.DATABASE_PATH; return process.env.DATABASE_PATH || resolveLegacyDatabasePath();
}
// Fallback: <project-root>/server/database/auth.db
const serverDir = path.resolve(__dirname, '..', '..', '..');
return path.join(serverDir, 'database', 'auth.db');
} }
/** /**
@@ -80,6 +75,7 @@ function migrateLegacyDatabase(targetPath: string): void {
fs.copyFileSync(legacyPath, targetPath); fs.copyFileSync(legacyPath, targetPath);
logger.info('Migrated legacy database', { from: legacyPath, to: targetPath }); logger.info('Migrated legacy database', { from: legacyPath, to: targetPath });
// copy the write-ahead log and shared memory files (auth.db-wal, auth.db-shm) if they exist, to preserve any uncommitted transactions // copy the write-ahead log and shared memory files (auth.db-wal, auth.db-shm) if they exist, to preserve any uncommitted transactions
for (const suffix of ['-wal', '-shm']) { for (const suffix of ['-wal', '-shm']) {
const src = legacyPath + suffix; const src = legacyPath + suffix;

View File

@@ -6,6 +6,7 @@ export type RuntimePaths = {
projectRoot: string; projectRoot: string;
legacyRuntimePath: string; legacyRuntimePath: string;
bootstrapEntrypointPath: string; bootstrapEntrypointPath: string;
refactorRuntimePath: string;
}; };
export type AppLocals = { export type AppLocals = {