Archived
feat(server): 生产服务器切换 uvicorn(FastAPI + Flask 兜底)
- web/main.py:uvicorn 入口,默认开启 Flask 兜底(未迁移路由回落遗留应用); HOST/PORT/LOG_LEVEL 环境变量可配;proxy_headers 按 XFF 取真实 IP(仅信回环); server_header=False 抹服务器指纹 - entrypoint.sh:默认 exec web/main.py;WEB_STACK=flask 可应急回退旧入口 - Dockerfile:安装 requirements-fastapi.txt(目标栈)+ requirements.txt(Flask 兜底) 验证(单进程双框架实跑):/health、/(Flask 兜底)与 /api/v1/site/stats(FastAPI) 均 200;Server 头为 NeteaseUrl;ruff/pyright 干净。
This commit is contained in:
1 parent
0f2e72db44
commit
52ed8b54fb
3 files changed
+31
-6
No files matched your search
@@ -6,8 +6,11 @@ WORKDIR /app
|
|||||||
# (容器内日志/展示本地时间差 8 小时;计费/会话全部 epoch 秒不受影响)
|
# (容器内日志/展示本地时间差 8 小时;计费/会话全部 epoch 秒不受影响)
|
||||||
RUN sed -i 's|dl-cdn.alpinelinux.org|mirrors.aliyun.com|g' /etc/apk/repositories && \
|
RUN sed -i 's|dl-cdn.alpinelinux.org|mirrors.aliyun.com|g' /etc/apk/repositories && \
|
||||||
apk add --no-cache tzdata ffmpeg
|
apk add --no-cache tzdata ffmpeg
|
||||||
|
# 目标栈:FastAPI + uvicorn(Flask 作为迁移期兜底仍随包)
|
||||||
|
COPY requirements-fastapi.txt requirements-fastapi.txt
|
||||||
COPY requirements.txt requirements.txt
|
COPY requirements.txt requirements.txt
|
||||||
RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
|
RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
|
||||||
|
pip3 install --no-cache-dir -r requirements-fastapi.txt && \
|
||||||
pip3 install --no-cache-dir -r requirements.txt
|
pip3 install --no-cache-dir -r requirements.txt
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN chmod +x /app/entrypoint.sh
|
RUN chmod +x /app/entrypoint.sh
|
||||||
|
|||||||
@@ -10,4 +10,9 @@ for f in user_cookie.txt cookie.txt; do
|
|||||||
fi
|
fi
|
||||||
[ -f "/app/$f" ] || touch "/app/$f"
|
[ -f "/app/$f" ] || touch "/app/$f"
|
||||||
done
|
done
|
||||||
|
# 生产服务器改为 uvicorn(FastAPI + Flask 兜底,见 web/main.py)
|
||||||
|
# WEB_STACK=flask 可回退到旧入口 main.py(waitress/Werkzeug)以应急
|
||||||
|
if [ "${WEB_STACK:-fastapi}" = "flask" ]; then
|
||||||
exec python3 main.py
|
exec python3 main.py
|
||||||
|
fi
|
||||||
|
exec python3 web/main.py
|
||||||
+23
-6
@@ -1,10 +1,13 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""FastAPI 服务入口(uvicorn)。
|
"""FastAPI 服务入口(uvicorn)。
|
||||||
|
|
||||||
开发期启动: .venv/Scripts/python.exe web/main.py
|
运行(生产/开发一致):
|
||||||
生产切换前需先将状态/任务模型与既有 Flask 进程统一(见迁移计划阶段 C)。
|
python web/main.py
|
||||||
"""
|
|
||||||
|
|
||||||
|
迁移期(strangler):默认开启 Flask 兜底——未迁移的路由由遗留 Flask 应用处理,
|
||||||
|
已迁移的 /api/v1 路由走 FastAPI。全部迁移完成后把 FASTAPI_FLASK_FALLBACK 默认值
|
||||||
|
改为 0 即可完全脱离 Flask(届时删除 setdefault 行为)。
|
||||||
|
"""
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@@ -16,8 +19,22 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||||||
|
|
||||||
from web.app import create_app
|
from web.app import create_app
|
||||||
|
|
||||||
# 生产可在联调期临时打开 Flask 兜底(未迁移路由回落到遗留应用)
|
HOST = os.environ.get('HOST', '0.0.0.0')
|
||||||
app = create_app(serve_flask_fallback=os.environ.get('FASTAPI_FLASK_FALLBACK') == '1')
|
PORT = int(os.environ.get('PORT', '5000'))
|
||||||
|
# 迁移期默认开启 Flask 兜底;设 FASTAPI_FLASK_FALLBACK=0 可关闭
|
||||||
|
FLASK_FALLBACK = os.environ.get('FASTAPI_FLASK_FALLBACK', '1') == '1'
|
||||||
|
|
||||||
|
app = create_app(serve_flask_fallback=FLASK_FALLBACK)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
uvicorn.run(app, host='0.0.0.0', port=5000, log_level='info')
|
uvicorn.run(
|
||||||
|
app,
|
||||||
|
host=HOST,
|
||||||
|
port=PORT,
|
||||||
|
log_level=os.environ.get('LOG_LEVEL', 'info').lower(),
|
||||||
|
# 反向代理(Caddy 本机回环)下按 XFF 取真实 IP,与既有口径一致
|
||||||
|
proxy_headers=True,
|
||||||
|
forwarded_allow_ips='127.0.0.1',
|
||||||
|
server_header=False, # 不暴露服务器版本指纹
|
||||||
|
date_header=True,
|
||||||
|
)
|
||||||
Reference in new issue
Block a user