refactor(部署): 重构部署架构,使用本地代理服务器替代环境变量配置

- 删除 Dockerfile.dev 和 entrypoint.sh,简化开发环境配置
- 修改 Dockerfile 使用 node 基础镜像并添加代理服务器
- 新增 proxy-server.js 处理 API 请求代理
- 更新 nginx 配置指向本地代理服务
- 修改前端请求添加自定义 base URL 头支持
This commit is contained in:
史悦
2026-01-22 16:40:16 +08:00
parent 8eb5499ecc
commit 199dd8a9db
11 changed files with 114 additions and 85 deletions

View File

@@ -1,7 +0,0 @@
# API Base URL
# 默认值: https://thinkflow.lz-t.top
API_BASE_URL=https://thinkflow.lz-t.top
# API Host (用于 Nginx 代理的 Host 头)
# 默认值: thinkflow.lz-t.top
API_HOST=thinkflow.lz-t.top

View File

@@ -10,16 +10,24 @@ COPY . .
RUN npm run build RUN npm run build
FROM nginx:alpine FROM node:20-alpine
COPY --from=builder /app/dist /usr/share/nginx/html WORKDIR /app
COPY nginx.conf.template /etc/nginx/templates/nginx.conf.template COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/proxy-server.js ./
COPY entrypoint.sh /docker-entrypoint.sh RUN npm install express cors http-proxy-middleware
RUN chmod +x /docker-entrypoint.sh RUN apk add --no-cache nginx
EXPOSE 80 COPY nginx.conf /etc/nginx/conf.d/default.conf
CMD ["/docker-entrypoint.sh"] COPY start.sh /start.sh
RUN chmod +x /start.sh
EXPOSE 80 3000
CMD ["/start.sh"]

View File

@@ -1,13 +0,0 @@
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5173
CMD ["npm", "run", "dev", "--", "--host"]

View File

@@ -8,7 +8,4 @@ services:
container_name: thinkflow container_name: thinkflow
ports: ports:
- "80:80" - "80:80"
environment:
- API_BASE_URL=${API_BASE_URL:-https://thinkflow.lz-t.top}
- API_HOST=${API_HOST:-thinkflow.lz-t.top}
restart: unless-stopped restart: unless-stopped

View File

@@ -1,5 +0,0 @@
#!/bin/sh
envsubst '${API_BASE_URL} ${API_HOST}' < /etc/nginx/templates/nginx.conf.template > /etc/nginx/conf.d/default.conf
exec nginx -g 'daemon off;'

View File

@@ -1,7 +1,7 @@
server { server {
listen 80; listen 80;
server_name localhost; server_name localhost;
root /usr/share/nginx/html; root /app/dist;
index index.html; index index.html;
location / { location / {
@@ -9,16 +9,16 @@ server {
} }
location /api/chat { location /api/chat {
proxy_pass https://thinkflow.lz-t.top/chat/completions; proxy_pass http://localhost:3000/api/chat;
proxy_set_header Host thinkflow.lz-t.top; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
} }
location /api/image { location /api/image {
proxy_pass https://thinkflow.lz-t.top/images/generations; proxy_pass http://localhost:3000/api/image;
proxy_set_header Host thinkflow.lz-t.top; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;

View File

@@ -1,29 +0,0 @@
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /api/chat {
proxy_pass ${API_BASE_URL}/chat/completions;
proxy_set_header Host ${API_HOST};
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/image {
proxy_pass ${API_BASE_URL}/images/generations;
proxy_set_header Host ${API_HOST};
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}

View File

@@ -15,7 +15,10 @@
"@vue-flow/minimap": "^1.5.4", "@vue-flow/minimap": "^1.5.4",
"@vue-flow/node-resizer": "^1.5.0", "@vue-flow/node-resizer": "^1.5.0",
"axios": "^1.6.7", "axios": "^1.6.7",
"cors": "^2.8.5",
"express": "^4.18.2",
"html-to-image": "^1.11.13", "html-to-image": "^1.11.13",
"http-proxy-middleware": "^2.0.6",
"lucide-vue-next": "^0.322.0", "lucide-vue-next": "^0.322.0",
"markdown-it": "^14.1.0", "markdown-it": "^14.1.0",
"vue": "^3.4.15", "vue": "^3.4.15",

45
proxy-server.js Normal file
View File

@@ -0,0 +1,45 @@
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const cors = require('cors');
const app = express();
const PORT = process.env.PROXY_PORT || 3000;
app.use(cors());
app.use(express.json());
app.use('/api/chat', createProxyMiddleware({
target: 'https://thinkflow.lz-t.top',
changeOrigin: true,
pathRewrite: {
'^/api/chat': '/chat/completions'
},
onProxyReq: (proxyReq, req, res) => {
const customBaseUrl = req.headers['x-custom-base-url'];
if (customBaseUrl) {
const url = new URL(customBaseUrl);
proxyReq.path = url.pathname;
proxyReq.setHeader('Host', url.host);
}
}
}));
app.use('/api/image', createProxyMiddleware({
target: 'https://thinkflow.lz-t.top',
changeOrigin: true,
pathRewrite: {
'^/api/image': '/images/generations'
},
onProxyReq: (proxyReq, req, res) => {
const customBaseUrl = req.headers['x-custom-base-url'];
if (customBaseUrl) {
const url = new URL(customBaseUrl);
proxyReq.path = url.pathname;
proxyReq.setHeader('Host', url.host);
}
}
}));
app.listen(PORT, () => {
console.log(`Proxy server running on port ${PORT}`);
});

View File

@@ -761,12 +761,18 @@ export function useThinkFlow({ t, locale }: { t: Translate; locale: Ref<string>
const finalApiKey = apiConfig.mode === 'default' ? useConfig.apiKey || API_KEY : useConfig.apiKey const finalApiKey = apiConfig.mode === 'default' ? useConfig.apiKey || API_KEY : useConfig.apiKey
try { try {
const headers: any = {
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
}
if (apiConfig.mode !== 'default' && useConfig.baseUrl) {
headers['X-Custom-Base-Url'] = useConfig.baseUrl
}
const response = await fetch(useConfig.baseUrl, { const response = await fetch(useConfig.baseUrl, {
method: 'POST', method: 'POST',
headers: { headers,
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
},
body: JSON.stringify({ body: JSON.stringify({
model: useConfig.model, model: useConfig.model,
messages: [ messages: [
@@ -813,12 +819,19 @@ export function useThinkFlow({ t, locale }: { t: Translate; locale: Ref<string>
const detail = node.data.description || '' const detail = node.data.description || ''
const path = findPathToNode(nodeId) const path = findPathToNode(nodeId)
const context = path.length > 5 ? `... -> ${path.slice(-4).join(' -> ')}` : path.join(' -> ') const context = path.length > 5 ? `... -> ${path.slice(-4).join(' -> ')}` : path.join(' -> ')
const headers: any = {
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
}
if (apiConfig.mode !== 'default' && useConfig.baseUrl) {
headers['X-Custom-Base-Url'] = useConfig.baseUrl
}
const response = await fetch(useConfig.baseUrl, { const response = await fetch(useConfig.baseUrl, {
method: 'POST', method: 'POST',
headers: { headers,
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
},
body: JSON.stringify({ body: JSON.stringify({
model: useConfig.model, model: useConfig.model,
prompt: t('prompts.image', { topic, detail, context }) prompt: t('prompts.image', { topic, detail, context })
@@ -869,12 +882,18 @@ export function useThinkFlow({ t, locale }: { t: Translate; locale: Ref<string>
const path = findPathToNode(nodeId) const path = findPathToNode(nodeId)
const context = path.join(' -> ') const context = path.join(' -> ')
const headers: any = {
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
}
if (apiConfig.mode !== 'default' && useConfig.baseUrl) {
headers['X-Custom-Base-Url'] = useConfig.baseUrl
}
const response = await fetch(useConfig.baseUrl, { const response = await fetch(useConfig.baseUrl, {
method: 'POST', method: 'POST',
headers: { headers,
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
},
body: JSON.stringify({ body: JSON.stringify({
model: useConfig.model, model: useConfig.model,
messages: [{ role: 'user', content: t('prompts.deepDivePrompt', { rootTopic, context, topic, detail }) }] messages: [{ role: 'user', content: t('prompts.deepDivePrompt', { rootTopic, context, topic, detail }) }]
@@ -1031,12 +1050,18 @@ export function useThinkFlow({ t, locale }: { t: Translate; locale: Ref<string>
const finalApiKey = apiConfig.mode === 'default' ? useConfig.apiKey || API_KEY : useConfig.apiKey const finalApiKey = apiConfig.mode === 'default' ? useConfig.apiKey || API_KEY : useConfig.apiKey
try { try {
const headers: any = {
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
}
if (apiConfig.mode !== 'default' && useConfig.baseUrl) {
headers['X-Custom-Base-Url'] = useConfig.baseUrl
}
const response = await fetch(useConfig.baseUrl, { const response = await fetch(useConfig.baseUrl, {
method: 'POST', method: 'POST',
headers: { headers,
'Content-Type': 'application/json',
Authorization: `Bearer ${finalApiKey}`
},
body: JSON.stringify({ body: JSON.stringify({
model: useConfig.model, model: useConfig.model,
messages: [ messages: [

5
start.sh Normal file
View File

@@ -0,0 +1,5 @@
#!/bin/sh
node proxy-server.js &
nginx -g 'daemon off;'