refactor: add nitro bootstrap plugin to ensure app is started before first call (#512)

* refactor: add nitro bootstrap plugin to ensure app is started before first call

* refactor(bootstrap): avoid duplicate event firing
This commit is contained in:
Nico 2026-02-13 20:46:10 +01:00 committed by GitHub
parent 1fea2a729a
commit 297e14ebb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 51 additions and 12 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,24 +1,23 @@
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";
setSchema(schema);
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";
const cliRun = await runCLI(Bun.argv);
if (cliRun) {
process.exit(0);
}
runDbMigrations();
await runMigrations();
await startup();
setSchema(schema);
await initAuth().catch((err) => {
logger.error(`Error initializing auth: ${toMessage(err)}`);
throw err;
});
const customHandler = defineHandlerCallback((ctx) => {
return defaultStreamHandler(ctx);

View file

@ -0,0 +1,26 @@
import * as schema from "../../db/schema";
import { runDbMigrations, setSchema } from "../../db/db";
import { runMigrations } from "./migrations";
import { startup } from "./startup";
let bootstrapPromise: Promise<void> | undefined;
const runBootstrap = async () => {
setSchema(schema);
runDbMigrations();
await runMigrations();
await startup();
};
export const bootstrapApplication = async () => {
if (!bootstrapPromise) {
bootstrapPromise = runBootstrap();
}
try {
await bootstrapPromise;
} catch (err) {
bootstrapPromise = undefined;
throw err;
}
};

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

@ -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"],