diff --git a/Dockerfile b/Dockerfile index 8dab34f..bc68660 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,46 +1,29 @@ -# 多阶段构建 - 构建阶段 -FROM oven/bun:alpine AS builder - -WORKDIR /app - -# 复制依赖文件 -COPY package.json bun.lockb ./ - -# 安装所有依赖(包括开发依赖) -RUN bun install --frozen-lockfile - -# 复制源代码 -COPY . . - -# 构建阶段(如果需要) -RUN bun run build || true - # 生产阶段 FROM oven/bun:alpine AS production -# 创建非root用户 -RUN addgroup -g 1001 -S nodejs && \ - adduser -S bun -u 1001 - WORKDIR /app -# 从构建阶段复制依赖 -COPY --from=builder --chown=bun:nodejs /app/node_modules ./node_modules -COPY --from=builder --chown=bun:nodejs /app/package.json ./ -COPY --from=builder --chown=bun:nodejs /app/bun.lockb ./ -COPY --from=builder --chown=bun:nodejs /app/knexfile.mjs ./ +# 安装构建依赖 +RUN apk add --no-cache python3 make g++ gcc dos2unix -# 复制应用代码 -COPY --from=builder --chown=bun:nodejs /app/src ./src -COPY --from=builder --chown=bun:nodejs /app/public ./public +# 复制应用文件 +COPY --chown=bun:bun ./package.json ./package.json +COPY --chown=bun:bun ./bun.lockb ./bun.lockb +COPY --chown=bun:bun ./knexfile.mjs ./knexfile.mjs +COPY --chown=bun:bun ./jsconfig.json ./jsconfig.json +COPY --chown=bun:bun ./src ./src +COPY --chown=bun:bun ./public ./public +COPY --chown=bun:bun ./entrypoint.sh ./entrypoint.sh -# 复制并设置入口脚本权限 -COPY --chown=bun:nodejs entrypoint.sh ./entrypoint.sh -RUN chmod +x ./entrypoint.sh +# 安装生产依赖和修复 entrypoint.sh +RUN bun install --frozen-lockfile --production --registry https://registry.npmjs.org && \ + # 修复 entrypoint.sh 的换行符 + dos2unix ./entrypoint.sh && \ + chmod +x ./entrypoint.sh # 创建必要的目录并设置权限 -RUN mkdir -p /app/database /app/logs && \ - chown -R bun:nodejs /app/database /app/logs +RUN mkdir -p ./database ./logs && \ + chown -R bun:bun ./database ./logs # 设置环境变量 ENV NODE_ENV=production diff --git a/bun.lockb b/bun.lockb index 3eba036..98b29ec 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/database/development.sqlite3-shm b/database/development.sqlite3-shm index 305e835..cdf5f27 100644 Binary files a/database/development.sqlite3-shm and b/database/development.sqlite3-shm differ diff --git a/entrypoint.sh b/entrypoint.sh index 4796b52..db680c0 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,7 +7,7 @@ ENV=${NODE_ENV:-production} # 检查 bun 是否存在 if command -v bun >/dev/null 2>&1; then - RUNNER="bun run" + RUNNER="bunx" START="exec bun src/main.js" else RUNNER="npx" @@ -17,11 +17,11 @@ fi # 如果数据库文件不存在,先 migrate 再 seed if [ ! -f "$DB_FILE" ]; then echo "Database not found, running migration and seed..." - $RUNNER npx knex migrate:latest --env $ENV - $RUNNER npx knex seed:run --env $ENV + $RUNNER knex migrate:latest --env $ENV + $RUNNER knex seed:run --env $ENV else echo "Database exists, running migration only..." - $RUNNER npx knex migrate:latest + $RUNNER knex migrate:latest fi # 启动主服务 diff --git a/package.json b/package.json index 04be7b5..8d9da88 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "koa3-demo", "module": "index.js", + "version": "0.0.1-alpha", "type": "module", "scripts": { "dev": "bun --hot src/main.js", @@ -16,14 +17,16 @@ "devDependencies": { "@types/bun": "latest", "@types/node": "^24.0.1", + "cross-env": "^10.0.0", + "module-alias": "^2.2.3", "node-gyp": "^11.4.2", - "vite": "^7.0.0" + "vite": "^7.0.0", + "vite-plugin-static-copy": "^3.1.0" }, "dependencies": { "@koa/etag": "^5.0.1", "bcryptjs": "^3.0.2", "consolidate": "^1.0.4", - "cross-env": "^10.0.0", "get-paths": "^0.0.7", "jsonwebtoken": "^9.0.0", "knex": "^3.1.0", @@ -34,13 +37,11 @@ "lodash": "^4.17.21", "log4js": "^6.9.1", "minimatch": "^9.0.0", - "module-alias": "^2.2.3", "node-cron": "^4.1.0", "path-to-regexp": "^8.2.0", "pug": "^3.0.3", "sqlite3": "^5.1.7", - "svg-captcha": "^1.4.0", - "vite-plugin-static-copy": "^3.1.0" + "svg-captcha": "^1.4.0" }, "_moduleAliases": { "@": "./src", diff --git a/src/middlewares/install.js b/src/middlewares/install.js index 23bc1cf..80eac7a 100644 --- a/src/middlewares/install.js +++ b/src/middlewares/install.js @@ -3,7 +3,7 @@ import Send from "./Send" import { resolve } from "path" import { fileURLToPath } from "url" import path from "path" -import ErrorHandler from "./ErrorHandler" +import ErrorHandler from "./errorHandler" import { Auth } from "./Auth" import bodyParser from "koa-bodyparser" import Views from "./Views"