From 8a8a9597edded885e33f579919c6e3c24c927882 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Thu, 17 Oct 2024 11:44:01 +0200 Subject: Add support for CORS preflight OPTION requests (#2006) --- g4f/api/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index 2c723978..2e6f19a1 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -12,6 +12,7 @@ from fastapi.security import APIKeyHeader from starlette.exceptions import HTTPException from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY, HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN from fastapi.encoders import jsonable_encoder +from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from typing import Union, Optional @@ -24,6 +25,13 @@ from g4f.cookies import read_cookie_files def create_app(): app = FastAPI() api = Api(app) + app.add_middleware( + CORSMiddleware, + allow_origin_regex=".*", + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) api.register_routes() api.register_authorization() api.register_validation_exception_handler() @@ -232,4 +240,4 @@ def run_api( use_colors=use_colors, factory=True, reload=debug - ) \ No newline at end of file + ) -- cgit v1.2.3