♻️ Refactor and update CORS, remove trailing slash from new Pydantic v2 (#617)
This commit is contained in:

committed by
GitHub

parent
53a0aebadf
commit
cfd66d2eee
2
src/.env
2
src/.env
@@ -7,7 +7,7 @@ PROJECT_NAME="FastAPI Project"
|
|||||||
STACK_NAME=fastapi-project
|
STACK_NAME=fastapi-project
|
||||||
|
|
||||||
# Backend
|
# Backend
|
||||||
BACKEND_CORS_ORIGINS="[\"http://localhost\", \"http://localhost:4200\", \"http://localhost:3000\", \"http://localhost:8080\", \"https://localhost\", \"https://localhost:4200\", \"https://localhost:3000\", \"https://localhost:8080\", \"http://local.dockertoolbox.tiangolo.com\", \"http://localhost.tiangolo.com\"]"
|
BACKEND_CORS_ORIGINS="http://localhost,http://localhost:5173,https://localhost,https://localhost:5173,http://localhost.tiangolo.com"
|
||||||
SECRET_KEY=changethis
|
SECRET_KEY=changethis
|
||||||
FIRST_SUPERUSER=admin@example.com
|
FIRST_SUPERUSER=admin@example.com
|
||||||
FIRST_SUPERUSER_PASSWORD=changethis
|
FIRST_SUPERUSER_PASSWORD=changethis
|
||||||
|
@@ -16,12 +16,11 @@ class Settings(BaseSettings):
|
|||||||
SECRET_KEY: str = secrets.token_urlsafe(32)
|
SECRET_KEY: str = secrets.token_urlsafe(32)
|
||||||
# 60 minutes * 24 hours * 8 days = 8 days
|
# 60 minutes * 24 hours * 8 days = 8 days
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
|
||||||
SERVER_NAME: str
|
|
||||||
SERVER_HOST: AnyHttpUrl
|
SERVER_HOST: AnyHttpUrl
|
||||||
# BACKEND_CORS_ORIGINS is a JSON-formatted list of origins
|
# BACKEND_CORS_ORIGINS is a JSON-formatted list of origins
|
||||||
# e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \
|
# e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \
|
||||||
# "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]'
|
# "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]'
|
||||||
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] = []
|
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] | str = []
|
||||||
|
|
||||||
@field_validator("BACKEND_CORS_ORIGINS", mode="before")
|
@field_validator("BACKEND_CORS_ORIGINS", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -38,7 +37,7 @@ class Settings(BaseSettings):
|
|||||||
@field_validator("SENTRY_DSN", mode="before")
|
@field_validator("SENTRY_DSN", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def sentry_dsn_can_be_blank(cls, v: str) -> str | None:
|
def sentry_dsn_can_be_blank(cls, v: str) -> str | None:
|
||||||
if len(v) == 0:
|
if not v:
|
||||||
return None
|
return None
|
||||||
return v
|
return v
|
||||||
|
|
||||||
@@ -65,7 +64,8 @@ class Settings(BaseSettings):
|
|||||||
SMTP_HOST: str | None = None
|
SMTP_HOST: str | None = None
|
||||||
SMTP_USER: str | None = None
|
SMTP_USER: str | None = None
|
||||||
SMTP_PASSWORD: str | None = None
|
SMTP_PASSWORD: str | None = None
|
||||||
EMAILS_FROM_EMAIL: str | None = None #TODO: update type to EmailStr when sqlmodel supports it
|
# TODO: update type to EmailStr when sqlmodel supports it
|
||||||
|
EMAILS_FROM_EMAIL: str | None = None
|
||||||
EMAILS_FROM_NAME: str | None = None
|
EMAILS_FROM_NAME: str | None = None
|
||||||
|
|
||||||
@field_validator("EMAILS_FROM_NAME")
|
@field_validator("EMAILS_FROM_NAME")
|
||||||
@@ -86,8 +86,10 @@ class Settings(BaseSettings):
|
|||||||
and info.data.get("EMAILS_FROM_EMAIL")
|
and info.data.get("EMAILS_FROM_EMAIL")
|
||||||
)
|
)
|
||||||
|
|
||||||
EMAIL_TEST_USER: str = "test@example.com" #TODO: update type to EmailStr when sqlmodel supports it
|
# TODO: update type to EmailStr when sqlmodel supports it
|
||||||
FIRST_SUPERUSER: str #TODO: update type to EmailStr when sqlmodel supports it
|
EMAIL_TEST_USER: str = "test@example.com"
|
||||||
|
# TODO: update type to EmailStr when sqlmodel supports it
|
||||||
|
FIRST_SUPERUSER: str
|
||||||
FIRST_SUPERUSER_PASSWORD: str
|
FIRST_SUPERUSER_PASSWORD: str
|
||||||
USERS_OPEN_REGISTRATION: bool = False
|
USERS_OPEN_REGISTRATION: bool = False
|
||||||
model_config = SettingsConfigDict(case_sensitive=True)
|
model_config = SettingsConfigDict(case_sensitive=True)
|
||||||
|
@@ -20,7 +20,9 @@ app = FastAPI(
|
|||||||
if settings.BACKEND_CORS_ORIGINS:
|
if settings.BACKEND_CORS_ORIGINS:
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
allow_origins=[
|
||||||
|
str(origin).strip("/") for origin in settings.BACKEND_CORS_ORIGINS
|
||||||
|
],
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
|
@@ -155,7 +155,6 @@ services:
|
|||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
environment:
|
environment:
|
||||||
- SERVER_NAME=${DOMAIN?Variable not set}
|
|
||||||
- SERVER_HOST=https://${DOMAIN?Variable not set}
|
- SERVER_HOST=https://${DOMAIN?Variable not set}
|
||||||
# Allow explicit env var override for tests
|
# Allow explicit env var override for tests
|
||||||
- SMTP_HOST=${SMTP_HOST?Variable not set}
|
- SMTP_HOST=${SMTP_HOST?Variable not set}
|
||||||
|
Reference in New Issue
Block a user