diff --git a/Dockerfile b/Dockerfile index e69de29..3e34960 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,78 @@ +# 阶段 1: 依赖项安装和构建 +FROM node:18-alpine AS builder + +# 设置工作目录 +WORKDIR /app + +# 创建 sources.list 文件并设置为清华源 +RUN echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.17/main" > /etc/apk/repositories && \ + echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.17/community" >> /etc/apk/repositories + +# 更新 apk 缓存 +RUN apk update + +# 设置 npm 使用清华大学开源软件镜像站 +RUN npm config set registry https://registry.npmmirror.com + +# 复制 package.json 和 package-lock.json (如果可用) +COPY package*.json ./ + +# 安装依赖项 +RUN npm ci + +# 复制项目文件 +COPY . . + +# 构建应用前,先安装必要的系统依赖 +RUN apk add --no-cache build-base python3 g++ make + +# 重新构建 better-sqlite3 模块 +RUN npm rebuild better-sqlite3 --update-binary + +# 构建 Next.js 应用 +RUN npm run build + +# 阶段 2: 生产环境 +FROM node:18-alpine AS runner + +# 设置工作目录 +WORKDIR /app + +# 创建 sources.list 文件并设置为清华源 +RUN echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.17/main" > /etc/apk/repositories && \ + echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.17/community" >> /etc/apk/repositories + +# 更新 apk 缓存 +RUN apk update + +# 设置为生产环境 +ENV NODE_ENV production + +# 添加一个非root用户来运行应用 +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +# 复制构建的应用和依赖项 +COPY --from=builder /app/next.config.js ./ +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static + +# 设置正确的权限 +RUN chown -R nextjs:nodejs /app +RUN chmod +x /app/start.sh + +# 切换到非root用户 +USER nextjs + +# 声明数据卷 +VOLUME /app/data + +# 暴露应用端口 +EXPOSE 3000 + +# 设置环境变量 +ENV PORT 3000 +ENV HOSTNAME "0.0.0.0" + +# 运行应用 +CMD ["/app/start.sh"] \ No newline at end of file diff --git a/lib/db.ts b/lib/db.ts index 32ce89d..8bdac52 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -1,7 +1,13 @@ import Database from 'better-sqlite3'; import { nanoid } from 'nanoid'; +import fs from 'fs'; +import path from 'path'; -const db = new Database('shorturl.db'); +const dataDir = process.env.NODE_ENV === 'production' ? '/app/data' : path.join(process.cwd(), 'data'); +fs.mkdirSync(dataDir, { recursive: true }); + +const dbPath = path.join(dataDir, 'shorturl.db'); +const db = new Database(dbPath); // 创建表(如果不存在) db.exec(` diff --git a/lib/env.ts b/lib/env.ts new file mode 100644 index 0000000..88ec7b1 --- /dev/null +++ b/lib/env.ts @@ -0,0 +1,6 @@ +import getConfig from 'next/config' + +const { publicRuntimeConfig } = getConfig() + +export const BASE_URL = publicRuntimeConfig.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000' + diff --git a/next.config.js b/next.config.js new file mode 100644 index 0000000..0e568a3 --- /dev/null +++ b/next.config.js @@ -0,0 +1,20 @@ +/** @type {import('next').NextConfig} */ +const nextConfig = { + output: 'standalone', + experimental: { + serverActions: true, + }, + // 启用运行时配置 + serverRuntimeConfig: { + // 将在服务器端可用 + NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL, + }, + publicRuntimeConfig: { + // 将在客户端和服务器端可用 + NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL, + }, + } + +module.exports = nextConfig + + \ No newline at end of file diff --git a/next.config.mjs b/next.config.mjs deleted file mode 100644 index 4678774..0000000 --- a/next.config.mjs +++ /dev/null @@ -1,4 +0,0 @@ -/** @type {import('next').NextConfig} */ -const nextConfig = {}; - -export default nextConfig; diff --git a/package.json b/package.json index 1ac5216..17583fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "short-url", - "version": "0.1.0", + "version": "0.1.13", "private": true, "scripts": { "dev": "next dev", diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..ffef005 --- /dev/null +++ b/start.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# 如果存在 .env 文件,则加载环境变量 +if [ -f .env ]; then + export $(cat .env | xargs) +fi + +# 启动 Next.js 应用 +exec node server.js +