Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加 CORS 中间件支持和配置选项 #228

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions meme_generator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import filetype
from fastapi import Depends, FastAPI, Form, HTTPException, Response, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, ValidationError

from meme_generator.compat import model_dump, model_json_schema, type_validate_python
Expand All @@ -21,6 +22,17 @@
app = FastAPI()


if meme_config.server.CORS_switch:
config = meme_config.server.CORS_config
app.add_middleware(
CORSMiddleware,
allow_origins=config.allow_origins,
allow_credentials=config.allow_credentials,
allow_methods=config.allow_methods,
allow_headers=config.allow_headers,
)


class MemeArgsResponse(BaseModel):
args_model: dict[str, Any]
args_examples: list[dict[str, Any]]
Expand Down
9 changes: 9 additions & 0 deletions meme_generator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ class TranslatorConfig(BaseModel):
baidu_trans_apikey: str = ""


class CORSConfig(BaseModel):
allow_origins: list[str] = ['*']
allow_credentials: bool = True
allow_methods: list[str] = ['*']
allow_headers: list[str] = ['*']


class ServerConfig(BaseModel):
host: str = "127.0.0.1"
port: int = 2233
CORS_switch: bool = False
CORS_config: CORSConfig = CORSConfig()


class LogConfig(BaseModel):
Expand Down