From a7372a19a95bb461299e4e13fd2a11b6aa835491 Mon Sep 17 00:00:00 2001 From: mei Date: Sat, 30 Nov 2024 08:19:57 +0800 Subject: [PATCH] fix --- .env.example | 1 + Dockerfile | 64 +++++++-------------------------------------- docker-compose.yaml | 10 +++++++ lib/db.ts | 4 +-- lib/env.ts | 6 ----- next.config.js | 11 +------- package-lock.json | 4 +-- start.sh | 10 ------- 8 files changed, 25 insertions(+), 85 deletions(-) create mode 100644 .env.example create mode 100644 docker-compose.yaml delete mode 100755 lib/env.ts delete mode 100755 start.sh diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..9a4cc7b --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +NEXT_PUBLIC_BASE_URL=https://u.mei.lv \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 0d0b694..a6534a5 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,79 +1,35 @@ -# 阶段 1: 依赖项安装和构建 +# Stage 1: Install dependencies 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 -# 复制项目文件 +# Stage 2: Build the applicatio COPY . . - -# 构建应用前,先安装必要的系统依赖 RUN apk add --no-cache build-base python3 g++ make - -# 重新构建 better-sqlite3 模块 RUN npm rebuild better-sqlite3 --update-binary - -# 构建 Next.js 应用 +RUN mkdir -p /app/data RUN npm run build -# 阶段 2: 生产环境 +# Stage 3: Production image FROM node:18-alpine AS runner - -# 设置工作目录 +RUN mkdir -p /app/data && chmod 755 /app/data 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 - -# 复制构建的应用和依赖项 +ENV NODE_ENV Production COPY --from=builder /app/next.config.js ./ COPY --from=builder /app/.next/standalone ./ -COPY --from=builder /app/start.sh ./ COPY --from=builder /app/.next/static ./.next/static - -# 设置正确的权限 -RUN chown -R nextjs:nodejs /app -RUN chmod +x /app/start.sh - -# 切换到非root用户 -USER nextjs - -# 声明数据卷 +COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/data/shorturl.db ./data VOLUME /app/data -# 暴露应用端口 -EXPOSE 3000 - -# 设置环境变量 -ENV PORT 3000 -ENV HOSTNAME "0.0.0.0" - # 运行应用 -CMD ["/app/start.sh"] \ No newline at end of file +CMD ["node", "server.js"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..dc98102 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,10 @@ +version: "3" +services: + url-shortener: + container_name: url-shortener + image: url-shortener:v0.0.20 + restart: always + volumes: + - ./data:/app/data + ports: + - "8567:3000" \ No newline at end of file diff --git a/lib/db.ts b/lib/db.ts index 8bdac52..cf9a40c 100755 --- a/lib/db.ts +++ b/lib/db.ts @@ -1,10 +1,8 @@ import Database from 'better-sqlite3'; import { nanoid } from 'nanoid'; -import fs from 'fs'; import path from 'path'; -const dataDir = process.env.NODE_ENV === 'production' ? '/app/data' : path.join(process.cwd(), 'data'); -fs.mkdirSync(dataDir, { recursive: true }); +const dataDir = 'data' const dbPath = path.join(dataDir, 'shorturl.db'); const db = new Database(dbPath); diff --git a/lib/env.ts b/lib/env.ts deleted file mode 100755 index 88ec7b1..0000000 --- a/lib/env.ts +++ /dev/null @@ -1,6 +0,0 @@ -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 index 7aafd19..5743f7e 100755 --- a/next.config.js +++ b/next.config.js @@ -1,17 +1,8 @@ /** @type {import('next').NextConfig} */ const nextConfig = { output: 'standalone', - // 启用运行时配置 - 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 + module.exports = nextConfig \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 91e558f..5bdd708 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "short-url", - "version": "0.1.0", + "version": "0.1.13", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "short-url", - "version": "0.1.0", + "version": "0.1.13", "dependencies": { "@radix-ui/react-progress": "^1.1.0", "better-sqlite3": "^11.6.0", diff --git a/start.sh b/start.sh deleted file mode 100755 index ffef005..0000000 --- a/start.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -# 如果存在 .env 文件,则加载环境变量 -if [ -f .env ]; then - export $(cat .env | xargs) -fi - -# 启动 Next.js 应用 -exec node server.js -