From 77b34844150ea395f2a3f857488f1800d7fa59ba Mon Sep 17 00:00:00 2001 From: imbytecat Date: Thu, 2 Apr 2026 03:42:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=94=B9=E7=94=A8=20Nitro=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=AE=9E=E7=8E=B0=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 1 - AGENTS.md | 4 ++-- Dockerfile | 6 ++++-- compose.yaml | 18 ++++++++++-------- src/server/plugins/migrate.ts | 17 +++++++++++++++++ vite.config.ts | 1 + 6 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 src/server/plugins/migrate.ts diff --git a/.dockerignore b/.dockerignore index bee158b..a1eb2fb 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,4 +11,3 @@ out/ *.bun-build .vscode/ -drizzle/ diff --git a/AGENTS.md b/AGENTS.md index 534bc97..05b8057 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -125,8 +125,8 @@ const handler = new RPCHandler(router, { - **Dockerfile**: Multi-stage — `oven/bun:1` (build + compile) → `gcr.io/distroless/cc-debian13:nonroot` (runtime) - **Runtime**: Bun-compiled standalone binary (glibc-linked, requires `cc` distroless variant) -- **Compose**: `app` (port 3000) + `postgres:17-alpine` (port 5432) with health check -- **Migrations**: Run separately — `bun run db:push` (dev) or `bun run db:migrate` (prod) against the compose DB +- **Compose**: `app` + `db` with health check dependency +- **Migrations**: Nitro plugin runs `migrate()` at server startup (see `src/server/plugins/migrate.ts`) - **Env**: `DATABASE_URL` set in `compose.yaml` pointing to the `db` service ## Git Workflow diff --git a/Dockerfile b/Dockerfile index ac2cb0a..034f3b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,9 +12,11 @@ RUN bun run build \ FROM gcr.io/distroless/cc-debian13:nonroot -COPY --from=build --chown=nonroot:nonroot /app/out/server /app/server +WORKDIR /app +COPY --from=build --chown=nonroot:nonroot /app/out/server ./server +COPY --from=build --chown=nonroot:nonroot /app/drizzle ./drizzle ENV HOST=0.0.0.0 EXPOSE 3000 -CMD ["/app/server"] +CMD ["./server"] diff --git a/compose.yaml b/compose.yaml index 9056072..a22d476 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,24 +1,26 @@ services: app: build: . - depends_on: - db: - condition: service_healthy ports: - '3000:3000' environment: - - DATABASE_URL=postgres://postgres:postgres@db:5432/postgres + DATABASE_URL: postgres://postgres:postgres@db:5432/postgres + depends_on: + db: + condition: service_healthy db: - image: postgres:18 - volumes: - - pgdata:/var/lib/postgresql/data + image: postgres:17-alpine + # ports: + # - '5432:5432' environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres + volumes: + - pgdata:/var/lib/postgresql/data healthcheck: - test: [ 'CMD', 'pg_isready', '-U', 'postgres' ] + test: ['CMD', 'pg_isready', '-U', 'postgres'] interval: 5s timeout: 5s retries: 5 diff --git a/src/server/plugins/migrate.ts b/src/server/plugins/migrate.ts new file mode 100644 index 0000000..59a4322 --- /dev/null +++ b/src/server/plugins/migrate.ts @@ -0,0 +1,17 @@ +import { drizzle } from 'drizzle-orm/postgres-js' +import { migrate } from 'drizzle-orm/postgres-js/migrator' +import { env } from '@/env' + +export default async () => { + if (import.meta.dev) return + + const db = drizzle({ connection: { url: env.DATABASE_URL, max: 1 } }) + + try { + console.log('Applying migrations...') + await migrate(db, { migrationsFolder: './drizzle' }) + console.log('Migrations applied successfully.') + } finally { + await db.$client.end() + } +} diff --git a/vite.config.ts b/vite.config.ts index a25fe1c..6696f32 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -15,6 +15,7 @@ export default defineConfig({ nitro({ preset: 'bun', serveStatic: 'inline', + plugins: ['./src/server/plugins/migrate.ts'], }), ], resolve: {