feat(api): add cors headers middleware (#2479)

This commit is contained in:
Jonas L 2023-03-23 15:40:30 +01:00 committed by GitHub
parent 1bf46b2de4
commit 7962c0adf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 0 deletions

View File

@ -25,9 +25,11 @@ INSTALLED_APPS = [
"rest_framework", "rest_framework",
"django_filters", "django_filters",
"drf_spectacular", "drf_spectacular",
"corsheaders",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware", "django.middleware.common.CommonMiddleware",

View File

@ -39,6 +39,18 @@ ALLOWED_HOSTS = ["*"]
LOGGING = setup_logger(LIBRETIME_LOG_FILEPATH) LOGGING = setup_logger(LIBRETIME_LOG_FILEPATH)
# CORS
# https://github.com/adamchainz/django-cors-headers
# Create an 'origin' by removing the public_url path
public_url_origin = (
CONFIG.general.public_url[: -len(CONFIG.general.public_url.path)]
if CONFIG.general.public_url.path
else CONFIG.general.public_url
)
CORS_ALLOWED_ORIGINS = [public_url_origin] + CONFIG.general.allowed_cors_origins
# Database # Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases # https://docs.djangoproject.com/en/3.2/ref/settings/#databases

View File

@ -1,5 +1,6 @@
# Please do not edit this file, edit the setup.py file! # Please do not edit this file, edit the setup.py file!
# This file is auto-generated by tools/extract_requirements.py. # This file is auto-generated by tools/extract_requirements.py.
django-cors-headers>=3.14.0,<3.15
django-filter>=2.4.0,<22.2 django-filter>=2.4.0,<22.2
django>=4.1.4,<4.2 django>=4.1.4,<4.2
djangorestframework>=3.12.1,<3.15 djangorestframework>=3.12.1,<3.15

View File

@ -24,6 +24,7 @@ setup(
] ]
}, },
install_requires=[ install_requires=[
"django-cors-headers>=3.14.0,<3.15",
"django-filter>=2.4.0,<22.2", "django-filter>=2.4.0,<22.2",
"django>=4.1.4,<4.2", "django>=4.1.4,<4.2",
"djangorestframework>=3.12.1,<3.15", "djangorestframework>=3.12.1,<3.15",

View File

@ -48,6 +48,8 @@ class GeneralConfig(BaseModel):
timezone: str = "UTC" timezone: str = "UTC"
allowed_cors_origins: List[AnyHttpUrl] = []
# Validators # Validators
_public_url_no_trailing_slash = no_trailing_slash_validator("public_url") _public_url_no_trailing_slash = no_trailing_slash_validator("public_url")