Compare commits

...

3 commits

Author SHA1 Message Date
Nicolas Meienberger
48b3c9a87f refactor: extract common setup in initModule function
Some checks failed
Release Workflow / determine-release-type (push) Has been cancelled
Release Workflow / checks (push) Has been cancelled
Release Workflow / e2e-tests (push) Has been cancelled
Release Workflow / build-images (push) Has been cancelled
Release Workflow / publish-release (push) Has been cancelled
2026-02-13 20:53:15 +01:00
Nicolas Meienberger
ca2a2cb74f refactor(bootstrap): avoid duplicate event firing
Some checks failed
Release Workflow / determine-release-type (push) Has been cancelled
Release Workflow / checks (push) Has been cancelled
Release Workflow / e2e-tests (push) Has been cancelled
Release Workflow / build-images (push) Has been cancelled
Release Workflow / publish-release (push) Has been cancelled
2026-02-13 20:37:12 +01:00
Nicolas Meienberger
436a63a4b4 refactor: add nitro bootstrap plugin to ensure app is started before first call
Some checks failed
Release Workflow / determine-release-type (push) Has been cancelled
Release Workflow / checks (push) Has been cancelled
Release Workflow / e2e-tests (push) Has been cancelled
Release Workflow / build-images (push) Has been cancelled
Release Workflow / publish-release (push) Has been cancelled
2026-02-13 20:20:16 +01:00
8 changed files with 61 additions and 24 deletions

View file

@ -100,7 +100,6 @@ ENV PORT=4096
WORKDIR /app
COPY --from=builder /app/package.json ./
RUN bun install --production --frozen-lockfile && rm -rf $HOME/.bun/install/cache
COPY --from=deps /deps/restic /usr/local/bin/restic
COPY --from=deps /deps/rclone /usr/local/bin/rclone

View file

@ -1,25 +1,17 @@
import * as schema from "./server/db/schema";
import { setSchema, runDbMigrations } from "./server/db/db";
import { startup } from "./server/modules/lifecycle/startup";
import { logger } from "./server/utils/logger";
import { shutdown } from "./server/modules/lifecycle/shutdown";
import { runCLI } from "./server/cli";
import { runMigrations } from "./server/modules/lifecycle/migrations";
import { createStartHandler, defaultStreamHandler, defineHandlerCallback } from "@tanstack/react-start/server";
import { createServerEntry } from "@tanstack/react-start/server-entry";
import { initModules } from "./server/modules/lifecycle/bootstrap";
setSchema(schema);
await initModules();
const cliRun = await runCLI(Bun.argv);
if (cliRun) {
process.exit(0);
}
runDbMigrations();
await runMigrations();
await startup();
const customHandler = defineHandlerCallback((ctx) => {
return defaultStreamHandler(ctx);
});

View file

@ -0,0 +1,39 @@
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<void> | undefined;
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();
};
export const bootstrapApplication = async () => {
if (!bootstrapPromise) {
bootstrapPromise = runBootstrap();
}
try {
await bootstrapPromise;
} catch (err) {
bootstrapPromise = undefined;
throw err;
}
};

View file

@ -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({

View file

@ -0,0 +1,11 @@
import { definePlugin } from "nitro";
import { bootstrapApplication } from "../modules/lifecycle/bootstrap";
import { logger } from "../utils/logger";
import { toMessage } from "../utils/errors";
export default definePlugin(() => {
void bootstrapApplication().catch((err) => {
logger.error(`Bootstrap failed: ${toMessage(err)}`);
process.exit(1);
});
});

View file

@ -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();
});

View file

@ -43,6 +43,7 @@ services:
- APP_SECRET=94bad4678ce84a60b9789bd2114a6bf780aeb38df426f7352c941c66e25d5c2b
- BASE_URL=http://localhost:4096
- PORT=4096
- LOG_LEVEL=debug
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/lib/zerobyte:/var/lib/zerobyte

View file

@ -14,7 +14,10 @@ export default defineConfig({
routesDirectory: "routes",
},
}),
nitro({ preset: "bun" }),
nitro({
preset: "bun",
plugins: ["./app/server/plugins/bootstrap.ts"],
}),
viteReact({
babel: {
plugins: ["babel-plugin-react-compiler"],