From 2ab499bc5410c8b0103708fef10384b3d6a6575e Mon Sep 17 00:00:00 2001 From: HouYunFei <1844025705@qq.com> Date: Wed, 20 May 2026 13:54:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(server):=20=E8=B0=83=E6=95=B4=20Docker?= =?UTF-8?q?=20=E8=BF=90=E8=A1=8C=E6=9E=B6=E6=9E=84=E4=B8=BA=20Next.js=20?= =?UTF-8?q?=E4=BB=A3=E7=90=86=20Go=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 Dockerfile 中的运行命令,将 Next.js 设为对外服务,Go 服务在内部监听 - 更新端口配置,后端监听端口从 3000 改为 80 - 移除 Go 服务中的反向代理逻辑,统一由 Next.js 处理页面请求 - 修改 Next.js 配置,在生产环境中也启用 API 代理功能 - 更新文档描述,明确 Next.js 作为页面入口,API 请求代理到 Go 服务 - 调整开发环境端口配置,web 开发端口从 3001 改为 3000 - 更新版本号至 v0.0.4 并修改 CHANGELOG --- .env.example | 7 +++++-- CHANGELOG.md | 7 +++++++ Dockerfile | 6 +++--- VERSION | 2 +- docs/features.md | 2 +- router/router.go | 16 +--------------- web/next.config.ts | 6 ++++-- web/package.json | 2 +- 8 files changed, 23 insertions(+), 25 deletions(-) diff --git a/.env.example b/.env.example index 9e16cfe..4708dad 100644 --- a/.env.example +++ b/.env.example @@ -6,8 +6,11 @@ ADMIN_PASSWORD=infinite-canvas JWT_SECRET=infinite-canvas JWT_EXPIRE_HOURS=168 -# 后端对外监听端口,Docker 默认由 Go 统一暴露 3000。 -PORT=3000 +# 后端监听端口 +PORT=8080 + +# 前端开发代理默认使用 http://127.0.0.1:8080,如后端开发端口不同,启动前端时可单独设置 API_BASE_URL。 +# API_BASE_URL=http://127.0.0.1:8080 # 数据库配置,默认使用本地 SQLite。 STORAGE_DRIVER=sqlite diff --git a/CHANGELOG.md b/CHANGELOG.md index ca767e1..3f601a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased + +## v0.0.4 - 2026-05-20 + + ++ [调整] Docker 运行入口改为 Next.js 对外提供页面,`/api/*` 由 Next.js 代理到内部 Go 服务。 ++ [修复] 文本复制在局域网 IP 访问时可能失败的问题。 + ## v0.0.3 - 2026-05-19 + [修复] 更新 nanoid 依赖并修改 ID 生成方式,防止其他ip无法使用crypto模块导致的ID生成失败问题。 diff --git a/Dockerfile b/Dockerfile index 2768b4a..7eb3f98 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,7 @@ COPY service ./service COPY main.go ./ RUN go build -o /server . -# 运行镜像:Go 对外监听 3000,Next.js 只在容器内部监听 3001。 +# 运行镜像:Next.js 对外监听 3000,Go 只在容器内部监听 8080。 FROM oven/bun:1 WORKDIR /app @@ -35,5 +35,5 @@ RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates RUN mkdir -p /app/data/prompts EXPOSE 3000 -# 先启动内部 Next.js,再由 Go 统一处理 /api/* 和页面反代。 -CMD ["sh", "-c", "cd /app/web && HOSTNAME=0.0.0.0 PORT=3001 bun run start & PORT=3000 /app/server"] +# 先启动内部 Go API,再由 Next.js 提供页面并代理 /api/*。 +CMD ["sh", "-c", "PORT=8080 /app/server & cd /app/web && HOSTNAME=0.0.0.0 PORT=3000 bun run start"] diff --git a/VERSION b/VERSION index 8ce995b..a92e827 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.0.3 \ No newline at end of file +v0.0.4 \ No newline at end of file diff --git a/docs/features.md b/docs/features.md index f05f192..bec3f05 100644 --- a/docs/features.md +++ b/docs/features.md @@ -152,7 +152,7 @@ ## 后端能力 - Gin 提供 API 服务。 -- Docker 运行时由 Go 提供统一入口,`/api/*` 直接处理,其它页面请求转到内部 Next.js 服务。 +- Docker 运行时由 Next.js 提供页面入口,`/api/*` 请求代理到内部 Go 服务。 - GORM 管理数据库连接和自动迁移。 - 支持 SQLite、MySQL、PostgreSQL。 - 数据库保存用户、提示词分组、提示词和服务器素材。 diff --git a/router/router.go b/router/router.go index 55e582a..c311a44 100644 --- a/router/router.go +++ b/router/router.go @@ -2,17 +2,12 @@ package router import ( "net/http" - "net/http/httputil" - "net/url" - "strings" "github.com/basketikun/infinite-canvas/handler" "github.com/basketikun/infinite-canvas/middleware" "github.com/gin-gonic/gin" ) -const webBaseURL = "http://127.0.0.1:3001" - func New() *gin.Engine { router := gin.Default() router.RedirectTrailingSlash = false @@ -47,16 +42,7 @@ func New() *gin.Engine { handler.AdminDeleteAsset(c.Writer, c.Request, c.Param("id")) }) - webURL, _ := url.Parse(webBaseURL) - webProxy := httputil.NewSingleHostReverseProxy(webURL) - router.NoRoute(func(c *gin.Context) { - path := c.Request.URL.Path - if path == "/api" || strings.HasPrefix(path, "/api/") { - middleware.NotFoundJSON(c) - return - } - webProxy.ServeHTTP(c.Writer, c.Request) - }) + router.NoRoute(middleware.NotFoundJSON) return router } diff --git a/web/next.config.ts b/web/next.config.ts index d7ff28e..6c78716 100644 --- a/web/next.config.ts +++ b/web/next.config.ts @@ -1,15 +1,17 @@ import type { NextConfig } from "next"; import { PHASE_DEVELOPMENT_SERVER } from "next/constants"; +import { loadEnvConfig } from "@next/env"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { dirname, resolve } from "node:path"; -const apiBaseUrl = process.env.API_BASE_URL || "http://127.0.0.1:3000"; const webDir = dirname(fileURLToPath(import.meta.url)); const version = readFileSync(resolve(webDir, "../VERSION"), "utf8").trim() || "dev"; export default function nextConfig(phase: string): NextConfig { const isDev = phase === PHASE_DEVELOPMENT_SERVER; + loadEnvConfig(resolve(webDir, ".."), isDev, undefined, true); + const apiBaseUrl = process.env.API_BASE_URL || "http://127.0.0.1:8080"; return { allowedDevOrigins: isDev ? ["*.*.*.*"] : [], typescript: { @@ -19,7 +21,7 @@ export default function nextConfig(phase: string): NextConfig { NEXT_PUBLIC_APP_VERSION: version, }, async rewrites() { - return isDev ? [{ source: "/api/:path*", destination: `${apiBaseUrl}/api/:path*` }] : []; + return [{ source: "/api/:path*", destination: `${apiBaseUrl}/api/:path*` }]; }, }; } diff --git a/web/package.json b/web/package.json index 9af3882..72f53b8 100644 --- a/web/package.json +++ b/web/package.json @@ -4,7 +4,7 @@ "private": true, "type": "module", "scripts": { - "dev": "next dev --webpack -H 0.0.0.0 -p 3001", + "dev": "next dev --webpack -H 0.0.0.0 -p 3000", "build": "next build", "start": "next start" },