From bbb461f7c2d12d5bf9f90374c1cd859016d610c1 Mon Sep 17 00:00:00 2001 From: Haileyesus Date: Wed, 18 Mar 2026 10:19:37 +0300 Subject: [PATCH] refactor(backend): add refactored runner for new backend architecture --- server/src/app.ts | 21 +++++++++++++---- server/src/config/load-env-vars.ts | 30 ++++++++++++++++++++++++ server/src/config/runtime.ts | 7 ++++++ server/src/runner.ts | 6 +++++ server/src/shared/database/connection.ts | 12 ++++------ server/src/shared/types/app.ts | 1 + 6 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 server/src/config/load-env-vars.ts create mode 100644 server/src/runner.ts diff --git a/server/src/app.ts b/server/src/app.ts index 23f9a18b..55e6395d 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -1,3 +1,5 @@ +import './config/load-env-vars.js'; + import { pathToFileURL } from 'url'; import { getRuntimePaths } from '@/config/runtime.js'; @@ -6,14 +8,25 @@ import { logger } from '@/shared/utils/logger.js'; export function createServerApplication(): ServerApplication { const runtimePaths = getRuntimePaths(); - + return { runtimePaths, 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); }, }; } diff --git a/server/src/config/load-env-vars.ts b/server/src/config/load-env-vars.ts new file mode 100644 index 00000000..324df106 --- /dev/null +++ b/server/src/config/load-env-vars.ts @@ -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'); +} diff --git a/server/src/config/runtime.ts b/server/src/config/runtime.ts index 226b7a5f..b8e6878b 100644 --- a/server/src/config/runtime.ts +++ b/server/src/config/runtime.ts @@ -6,9 +6,15 @@ import type { RuntimePaths } from '@/shared/types/app.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); +const RUN_REFACTOR_WITH_SRC = true; + export function getRuntimePaths(): RuntimePaths { const serverSrcDir = path.resolve(__dirname, '..'); const serverDir = path.resolve(serverSrcDir, '..'); + const refactorRuntimePath = + RUN_REFACTOR_WITH_SRC + ? path.join(serverDir, 'src', 'runner.ts') + : path.join(serverDir, 'dist', 'runner.js'); return { serverSrcDir, @@ -16,5 +22,6 @@ export function getRuntimePaths(): RuntimePaths { projectRoot: path.resolve(serverDir, '..'), legacyRuntimePath: path.join(serverDir, 'index.js'), bootstrapEntrypointPath: path.join(serverDir, 'dist', 'bootstrap.js'), + refactorRuntimePath }; } diff --git a/server/src/runner.ts b/server/src/runner.ts new file mode 100644 index 00000000..ae934b0b --- /dev/null +++ b/server/src/runner.ts @@ -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()); diff --git a/server/src/shared/database/connection.ts b/server/src/shared/database/connection.ts index dd31faf0..f4fd0854 100644 --- a/server/src/shared/database/connection.ts +++ b/server/src/shared/database/connection.ts @@ -30,17 +30,12 @@ const __dirname = path.dirname(__filename); * to the legacy location inside the server/database/ folder. * * 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 */ function resolveDatabasePath(): string { - if (process.env.DATABASE_PATH) { - return process.env.DATABASE_PATH; - } - - // Fallback: /server/database/auth.db - const serverDir = path.resolve(__dirname, '..', '..', '..'); - return path.join(serverDir, 'database', 'auth.db'); + // 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 || resolveLegacyDatabasePath(); } /** @@ -80,6 +75,7 @@ function migrateLegacyDatabase(targetPath: string): void { fs.copyFileSync(legacyPath, 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 for (const suffix of ['-wal', '-shm']) { const src = legacyPath + suffix; diff --git a/server/src/shared/types/app.ts b/server/src/shared/types/app.ts index 68d70320..039c2dbf 100644 --- a/server/src/shared/types/app.ts +++ b/server/src/shared/types/app.ts @@ -6,6 +6,7 @@ export type RuntimePaths = { projectRoot: string; legacyRuntimePath: string; bootstrapEntrypointPath: string; + refactorRuntimePath: string; }; export type AppLocals = {