From fcab5ad5e15e98a2d5710a1343d850ea6345cb70 Mon Sep 17 00:00:00 2001 From: xaoyaoo Date: Sun, 29 Dec 2024 02:03:21 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E6=94=B9flask=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pywxdump/api/__init__.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pywxdump/api/__init__.py b/pywxdump/api/__init__.py index d343b55..d75d66e 100644 --- a/pywxdump/api/__init__.py +++ b/pywxdump/api/__init__.py @@ -29,7 +29,7 @@ from .local_server import ls_api from pywxdump import __version__ -def gen_fastapi_app(handler): +def gen_fastapi_app(handler, origins=None): app = FastAPI(title="wxdump", description="微信工具", version=__version__, terms_of_service="https://github.com/xaoyaoo/pywxdump", contact={"name": "xaoyaoo", "url": "https://github.com/xaoyaoo/pywxdump"}, @@ -37,15 +37,15 @@ def gen_fastapi_app(handler): "url": "https://github.com/xaoyaoo/PyWxDump/blob/master/LICENSE"}) web_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "ui", "web") # web文件夹路径 - # 跨域 - origins = [ - "http://localhost:5000", - "http://127.0.0.1:5000", - "http://localhost:8080", # 开发环境的客户端地址" - # "http://0.0.0.0:5000", - # "*" - ] + if not origins: + origins = [ + "http://localhost:5000", + "http://127.0.0.1:5000", + "http://localhost:8080", # 开发环境的客户端地址" + # "http://0.0.0.0:5000", + # "*" + ] app.add_middleware( CORSMiddleware, allow_origins=origins, # 允许所有源 @@ -191,10 +191,17 @@ def start_server(port=5000, online=False, debug=False, isopenBrowser=True, time.sleep(1) server_loger.info(f"启动flask服务,host:port:{host}:{port}") - print("[+] 请使用浏览器访问 http://127.0.0.1:5000/ 查看聊天记录") + print(f"[+] 请使用浏览器访问 http://127.0.0.1:{port}/ 查看聊天记录") global app - print("[+] 如需查看api文档,请访问 http://127.0.0.1:5000/docs ") - app = gen_fastapi_app(file_handler) + print(f"[+] 如需查看api文档,请访问 http://127.0.0.1:{port}/docs ") + origins = [ + f"http://localhost:{port}", + f"http://{host}:{port}", + f"http://localhost:8080", # 开发环境的客户端地址" + # f"http://0.0.0.0:{port}", + # "*" + ] + app = gen_fastapi_app(file_handler, origins) LOGGING_CONFIG["formatters"]["default"]["fmt"] = "[%(asctime)s] %(levelprefix)s %(message)s" LOGGING_CONFIG["formatters"]["access"][