fix: 优化端口

This commit is contained in:
丹尼尔
2026-03-11 11:36:05 +08:00
parent d3c5b4c27a
commit dae013dbeb
7 changed files with 48 additions and 14 deletions

View File

@@ -127,7 +127,8 @@
</div>
<script>
const $ = (id) => document.getElementById(id);
const API_BASE = 'http://localhost:8000';
// 相对路径,由 Node 代理到后端,适配 -p/-b 任意端口
const API_BASE = '';
const KEY_STORAGE = 'wechat_key';
function getToken() {

View File

@@ -634,7 +634,8 @@
<script>
const $ = (id) => document.getElementById(id);
const API_BASE = 'http://localhost:8000';
// 相对路径,由 Node 代理到后端,适配 -p/-b 任意端口
const API_BASE = '';
const state = {
pollingScan: null,

View File

@@ -215,7 +215,8 @@
</div>
<script>
const $ = (id) => document.getElementById(id);
const API_BASE = 'http://localhost:8000';
// 相对路径,由 Node 代理到后端,适配 -p/-b 任意端口
const API_BASE = '';
const KEY_STORAGE = 'wechat_key';
function getKey() {

View File

@@ -96,7 +96,8 @@
</div>
<script>
const $ = (id) => document.getElementById(id);
const API_BASE = 'http://localhost:8000';
// 相对路径,由 Node 代理到后端
const API_BASE = '';
async function callApi(path, options = {}) {
const url = API_BASE + path;
@@ -164,7 +165,8 @@
loadModels();
(function wsStatusCheck() {
const API_BASE = 'http://localhost:8000';
// 相对路径,由 Node 代理到后端
const API_BASE = '';
let wasConnected = false;
const CHECK_MS = 8000;
const t = setInterval(async () => {

View File

@@ -112,7 +112,7 @@
var copyBaseCheck = document.getElementById('copy-base-url');
function getOpenApiUrl() {
var base = (baseUrlInput && baseUrlInput.value.trim()) || 'http://localhost:8000';
var base = (baseUrlInput && baseUrlInput.value.trim()) || '';
return base.replace(/\/$/, '') + '/openapi.json';
}
@@ -128,7 +128,7 @@
var apiUrl = applyBaseUrl();
window.xxcaibi = {
BASE_URL: (baseUrlInput && baseUrlInput.value.trim()) || 'http://localhost:8000',
BASE_URL: (baseUrlInput && baseUrlInput.value.trim()) || '',
copy: true,
KEY: ''
};
@@ -173,7 +173,7 @@
var config = window.ui.specSelectors.specJson();
if (config && config.toJS) {
var spec = config.toJS();
window.xxcaibi.BASE_URL = (baseUrlInput && baseUrlInput.value.trim()) || (spec.servers && spec.servers[0] && spec.servers[0].url) || 'http://localhost:8000';
window.xxcaibi.BASE_URL = (baseUrlInput && baseUrlInput.value.trim()) || (spec.servers && spec.servers[0] && spec.servers[0].url) || '';
}
function addCopyButtons() {
document.querySelectorAll('.opblock-summary-path').forEach(function (pathEl) {

View File

@@ -1,7 +1,8 @@
#!/usr/bin/env bash
# 生产部署脚本:启动前拉取最新代码 → 构建镜像 → 停止旧容器 → 启动新容器
# 用法: ./run-docker.sh [-p PORT] [-d HOST_DATA_DIR]
# -p, --port PORT 宿主机端口,默认 3000
# 用法: ./run-docker.sh [-p PORT] [-b BACKEND_PORT] [-d HOST_DATA_DIR]
# -p, --port PORT 前端宿主机端口,默认 3000
# -b, --backend-port PORT 后端 API 宿主机端口,默认 8000容器内固定 8000
# -d, --data-dir DIR 数据目录挂载,默认 ./data
set -e
@@ -9,13 +10,15 @@ set -e
IMAGE_NAME="wechat-admin-backend"
CONTAINER_NAME="wechat-admin-backend"
PORT="${PORT:-3000}"
BACKEND_PORT="${BACKEND_PORT:-8000}"
HOST_DATA_DIR="${HOST_DATA_DIR:-$(pwd)/data}"
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port) PORT="$2"; shift 2 ;;
-b|--backend-port) BACKEND_PORT="$2"; shift 2 ;;
-d|--data-dir) HOST_DATA_DIR="$2"; shift 2 ;;
-h|--help) echo "Usage: $0 [-p PORT] [-d HOST_DATA_DIR]"; echo " -p, --port PORT default 3000"; echo " -d, --data-dir DIR default \$(pwd)/data"; exit 0 ;;
-h|--help) echo "Usage: $0 [-p PORT] [-b BACKEND_PORT] [-d HOST_DATA_DIR]"; echo " -p, --port PORT frontend (default 3000)"; echo " -b, --backend-port PORT backend API (default 8000)"; echo " -d, --data-dir DIR data volume (default \$(pwd)/data)"; exit 0 ;;
*) echo "Unknown option: $1 (use -h for help)"; exit 1 ;;
esac
done
@@ -45,15 +48,15 @@ if [ ! -f "${ENV_FILE}" ]; then
cp .env.example "${ENV_FILE}"
fi
echo "Running container ${CONTAINER_NAME} on port ${PORT}..."
echo "Running container ${CONTAINER_NAME} (frontend :${PORT}, backend :${BACKEND_PORT})..."
docker run -d \
--name "${CONTAINER_NAME}" \
--env-file "${ENV_FILE}" \
-p "${PORT}:3000" \
-p "8000:8000" \
-p "${BACKEND_PORT}:8000" \
-v "${HOST_DATA_DIR}:/app/backend/data" \
"${IMAGE_NAME}"
echo "Container started. Data persisted on host: ${HOST_DATA_DIR}"
echo "Health check: curl http://localhost:${PORT}/health"
echo "Frontend: http://localhost:${PORT} | Backend API: http://localhost:${BACKEND_PORT} | Health: curl http://localhost:${PORT}/health"

View File

@@ -7,11 +7,37 @@ import path from 'path';
dotenv.config();
const app = express();
const backendPort = Number(process.env.BACKEND_PORT) || 8000;
const backendBase = `http://127.0.0.1:${backendPort}`;
app.use(cors());
app.use(express.json());
app.use(morgan('dev'));
// 代理 /api 和 /auth 到后端,使前端用相对路径即可(任意 -p/-b 端口都正确)
async function proxyToBackend(req: express.Request, res: express.Response): Promise<void> {
try {
const url = backendBase + req.originalUrl;
const headers: Record<string, string> = { ...req.headers as Record<string, string> };
delete headers.host;
const opts: RequestInit = { method: req.method, headers };
if (req.body && ['POST', 'PUT', 'PATCH'].includes(req.method)) {
opts.body = typeof req.body === 'string' ? req.body : JSON.stringify(req.body);
if (!headers['content-type']) headers['content-type'] = 'application/json';
}
const proxyRes = await fetch(url, opts);
res.status(proxyRes.status);
proxyRes.headers.forEach((v, k) => res.setHeader(k, v));
const body = await proxyRes.text();
res.send(body);
} catch (e) {
res.status(502).json({ error: 'proxy_error', detail: String(e) });
}
}
app.use('/api', proxyToBackend);
app.use('/auth', proxyToBackend);
app.get('/openapi.json', (req, res) => proxyToBackend(req, res));
const publicDir = path.join(__dirname, '../public');
app.use(express.static(publicDir));