Skip to content

Commit

Permalink
chore: add CORS settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemSBulgakov committed Oct 28, 2024
1 parent 007009f commit d2a9beb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions settings.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ $defs:
description: Prefix for the API path (e.g. "/api/v0")
title: App Root Path
type: string
cors_allow_origin_regex:
default: .*
title: Cors Allow Origin Regex
type: string
db_url:
default: postgresql+asyncpg://postgres:postgres@localhost:5433/postgres
example: postgresql+asyncpg://user:password@localhost:5433/db_name
Expand Down
10 changes: 10 additions & 0 deletions src/api/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import FastAPI
from fastapi_swagger import patch_fastapi
from starlette.middleware.cors import CORSMiddleware

import src.api.logging_ # noqa: F401
from src.api import docs
Expand Down Expand Up @@ -37,6 +38,15 @@

patch_fastapi(app)

# CORS settings
app.add_middleware(
CORSMiddleware,
allow_origin_regex=api_settings.cors_allow_origin_regex,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

from src.api.auth.routes import router as router_auth # noqa: E402
from src.api.bookings.routes import router as router_booking # noqa: E402
from src.api.root.routes import router as router_root # noqa: E402
Expand Down
3 changes: 3 additions & 0 deletions src/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class Accounts(BaseModel):

class ApiSettings(BaseModel):
app_root_path: str = Field("", description='Prefix for the API path (e.g. "/api/v0")')
cors_allow_origin_regex: str = ".*"
"Allowed origins for CORS: from which domains requests to the API are allowed. Specify as a regex: `https://.*.innohassle.ru`"

db_url: str = Field(
"postgresql+asyncpg://postgres:postgres@localhost:5433/postgres",
example="postgresql+asyncpg://user:password@localhost:5433/db_name",
Expand Down

0 comments on commit d2a9beb

Please sign in to comment.