Skip to content

Commit aabb2f2

Browse files
committed
Added CORSMiddleware and TrustedHostMiddleware as default application middlewares
1 parent e94d734 commit aabb2f2

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

ellar/core/conf/app_settings_models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ class Config:
8181

8282
STATIC_MOUNT_PATH: str = "/static"
8383

84+
CORS_ALLOW_ORIGINS: t.List[str] = []
85+
CORS_ALLOW_METHODS: t.List[str] = ["GET"]
86+
CORS_ALLOW_HEADERS: t.List[str] = []
87+
88+
CORS_ALLOW_CREDENTIALS: bool = False
89+
CORS_ALLOW_ORIGIN_REGEX: t.Optional[str] = None
90+
CORS_EXPOSE_HEADERS: t.Sequence[str] = ()
91+
CORS_MAX_AGE: int = 600
92+
93+
ALLOWED_HOSTS: t.List[str] = ["*"]
94+
REDIRECT_HOST: bool = True
95+
8496
MIDDLEWARE: t.List[TMiddleware] = []
8597

8698
_APP_EXCEPTION_HANDLERS: t.Dict[t.Union[int, t.Type[Exception]], t.Callable] = {

ellar/core/conf/mixins.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import typing as t
22

3-
from starlette.middleware import Middleware
43
from starlette.responses import JSONResponse
54
from starlette.types import ASGIApp
65

76
from ellar.constants import LOG_LEVELS as log_levels
87
from ellar.core.events import EventHandler
8+
from ellar.core.middleware import Middleware
99
from ellar.core.versioning import BaseAPIVersioning
1010

1111
if t.TYPE_CHECKING: # pragma: no cover
@@ -125,3 +125,17 @@ class ConfigDefaultTypesMixin:
125125

126126
# logging Level
127127
LOG_LEVEL: t.Optional[log_levels]
128+
129+
# CORS Middleware setup (ellar.core.middleware.CORSMiddleware)
130+
CORS_ALLOW_ORIGINS: t.List[str]
131+
CORS_ALLOW_METHODS: t.List[str]
132+
CORS_ALLOW_HEADERS: t.List[str]
133+
CORS_ALLOW_CREDENTIALS: bool
134+
135+
CORS_ALLOW_ORIGIN_REGEX: t.Optional[str]
136+
CORS_EXPOSE_HEADERS: t.Sequence[str]
137+
CORS_MAX_AGE: int
138+
139+
# TrustHostMiddleware setup
140+
ALLOWED_HOSTS: t.List[str]
141+
REDIRECT_HOST: bool

ellar/core/main.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
from ellar.core.events import EventHandler, RouterEventManager
1111
from ellar.core.guard import GuardCanActivate
1212
from ellar.core.middleware import (
13+
CORSMiddleware,
1314
ExceptionMiddleware,
1415
Middleware,
1516
RequestServiceProviderMiddleware,
1617
RequestVersioningMiddleware,
18+
TrustedHostMiddleware,
1719
)
1820
from ellar.core.modules import ModuleBase, ModuleTemplateRef
1921
from ellar.core.modules.ref import create_module_ref_factor
@@ -134,7 +136,7 @@ def install_module(
134136
module, container=self.injector.container, config=self.config, **init_kwargs
135137
)
136138
self.injector.add_module(module_ref)
137-
self.middleware_stack = self.build_middleware_stack()
139+
self.rebuild_middleware_stack()
138140

139141
if isinstance(module_ref, ModuleTemplateRef):
140142
module_ref.run_module_register_services()
@@ -182,6 +184,21 @@ def build_middleware_stack(self) -> ASGIApp:
182184

183185
middleware = (
184186
[
187+
Middleware(
188+
CORSMiddleware,
189+
allow_origins=self.config.CORS_ALLOW_ORIGINS,
190+
allow_credentials=self.config.CORS_ALLOW_CREDENTIALS,
191+
allow_methods=self.config.CORS_ALLOW_METHODS,
192+
allow_headers=self.config.CORS_ALLOW_HEADERS,
193+
allow_origin_regex=self.config.CORS_ALLOW_ORIGIN_REGEX,
194+
expose_headers=self.config.CORS_EXPOSE_HEADERS,
195+
max_age=self.config.CORS_MAX_AGE,
196+
),
197+
Middleware(
198+
TrustedHostMiddleware,
199+
allowed_hosts=self.config.ALLOWED_HOSTS,
200+
www_redirect=self.config.REDIRECT_HOST,
201+
),
185202
Middleware(
186203
RequestServiceProviderMiddleware,
187204
debug=self.debug,
@@ -262,3 +279,6 @@ def decorator(func: t.Callable) -> t.Callable:
262279
return func
263280

264281
return decorator
282+
283+
def rebuild_middleware_stack(self) -> None:
284+
self.middleware_stack = self.build_middleware_stack()

0 commit comments

Comments
 (0)