diff --git a/app/server.ts b/app/server.ts index e0f3136f..53cf098d 100644 --- a/app/server.ts +++ b/app/server.ts @@ -3,22 +3,15 @@ import { shutdown } from "./server/modules/lifecycle/shutdown"; import { runCLI } from "./server/cli"; import { createStartHandler, defaultStreamHandler, defineHandlerCallback } from "@tanstack/react-start/server"; import { createServerEntry } from "@tanstack/react-start/server-entry"; -import { initAuth } from "~/server/lib/auth"; -import { setSchema } from "./server/db/db"; -import * as schema from "./server/db/schema"; -import { toMessage } from "./server/utils/errors"; +import { initModules } from "./server/modules/lifecycle/bootstrap"; + +await initModules(); const cliRun = await runCLI(Bun.argv); if (cliRun) { process.exit(0); } -setSchema(schema); -await initAuth().catch((err) => { - logger.error(`Error initializing auth: ${toMessage(err)}`); - throw err; -}); - const customHandler = defineHandlerCallback((ctx) => { return defaultStreamHandler(ctx); }); diff --git a/app/server/modules/lifecycle/bootstrap.ts b/app/server/modules/lifecycle/bootstrap.ts index 529b4cc8..f4071a28 100644 --- a/app/server/modules/lifecycle/bootstrap.ts +++ b/app/server/modules/lifecycle/bootstrap.ts @@ -2,11 +2,24 @@ import * as schema from "../../db/schema"; import { runDbMigrations, setSchema } from "../../db/db"; import { runMigrations } from "./migrations"; import { startup } from "./startup"; +import { initAuth } from "../../lib/auth"; +import { logger } from "../../utils/logger"; +import { toMessage } from "../../utils/errors"; let bootstrapPromise: Promise | undefined; -const runBootstrap = async () => { +export const initModules = async () => { setSchema(schema); + + await initAuth().catch((err) => { + logger.error(`Error initializing auth: ${toMessage(err)}`); + throw err; + }); +} + +const runBootstrap = async () => { + await initModules(); + runDbMigrations(); await runMigrations(); await startup(); diff --git a/app/server/modules/lifecycle/startup.ts b/app/server/modules/lifecycle/startup.ts index b830733a..dbe98f49 100644 --- a/app/server/modules/lifecycle/startup.ts +++ b/app/server/modules/lifecycle/startup.ts @@ -12,8 +12,6 @@ import { repositoriesService } from "../repositories/repositories.service"; import { notificationsService } from "../notifications/notifications.service"; import { VolumeAutoRemountJob } from "~/server/jobs/auto-remount"; import { cache } from "~/server/utils/cache"; -import { initAuth } from "~/server/lib/auth"; -import { toMessage } from "~/server/utils/errors"; import { withContext } from "~/server/core/request-context"; const ensureLatestConfigurationSchema = async () => { @@ -54,11 +52,6 @@ export const startup = async () => { await Scheduler.start(); await Scheduler.clear(); - await initAuth().catch((err) => { - logger.error(`Error initializing auth: ${toMessage(err)}`); - throw err; - }); - await ensureLatestConfigurationSchema(); const volumes = await db.query.volumesTable.findMany({ diff --git a/app/test/setup.ts b/app/test/setup.ts index 5ca37ec8..70221c2c 100644 --- a/app/test/setup.ts +++ b/app/test/setup.ts @@ -2,11 +2,9 @@ import { beforeAll, mock } from "bun:test"; import { migrate } from "drizzle-orm/bun-sqlite/migrator"; import path from "node:path"; import { cwd } from "node:process"; -import * as schema from "~/server/db/schema"; -import { db, setSchema } from "~/server/db/db"; -import { initAuth } from "~/server/lib/auth"; +import { db } from "~/server/db/db"; +import { initModules } from "../server/modules/lifecycle/bootstrap"; -setSchema(schema); void mock.module("~/server/utils/logger", () => ({ logger: { @@ -27,7 +25,8 @@ void mock.module("~/server/utils/crypto", () => ({ })); beforeAll(async () => { + await initModules(); + const migrationsFolder = path.join(cwd(), "app", "drizzle"); migrate(db, { migrationsFolder }); - await initAuth(); });