Skip to content

Commit bd35f28

Browse files
author
Francesco Faraone
committed
LITE-28135 add support for django
1 parent 6d623a0 commit bd35f28

File tree

7 files changed

+537
-147
lines changed

7 files changed

+537
-147
lines changed

connect/eaas/runner/handlers/base.py

+22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ApplicationHandlerBase(ABC):
2525
def __init__(self, config):
2626
self._config = config
2727
self._logging_handler = None
28+
self._django_settings_module = self.get_django_settings_module()
2829

2930
@property
3031
def config(self):
@@ -70,7 +71,28 @@ def features(self):
7071
def variables(self):
7172
return self.get_variables()
7273

74+
@property
75+
def django_settings_module(self):
76+
return self._django_settings_module
77+
78+
def get_django_settings_module(self):
79+
ep = next(
80+
iter_entry_points('connect.eaas.ext', 'djsettings'),
81+
None,
82+
)
83+
if ep:
84+
get_settings = ep.load()
85+
return get_settings()
86+
return None
87+
88+
def load_django(self):
89+
if self._django_settings_module:
90+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', self._django_settings_module)
91+
import django
92+
django.setup()
93+
7394
def load_application(self, name):
95+
self.load_django()
7496
ep = next(
7597
iter_entry_points('connect.eaas.ext', name),
7698
None,

connect/eaas/runner/handlers/web.py

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import functools
66
import inspect
77
import logging
8+
import os
89

910
from fastapi import (
1011
FastAPI,
@@ -186,9 +187,41 @@ def get_asgi_application(self):
186187
static_root = self.get_application().get_static_root()
187188
if static_root:
188189
app.mount('/static', StaticFiles(directory=static_root), name='static')
190+
if self.django_settings_module:
191+
self.mount_django_apps(app)
189192

190193
return app
191194

195+
def mount_django_apps(self, app):
196+
from django.conf import settings # noqa: I001
197+
from django.core.exceptions import ImproperlyConfigured # noqa: I001
198+
from django.core.handlers.asgi import ASGIHandler # noqa: I001
199+
script_name = getattr(settings, 'FORCE_SCRIPT_NAME', None)
200+
if not (
201+
script_name
202+
and script_name.startswith('/guest/')
203+
and len(script_name) > len('/guest/')
204+
):
205+
raise ImproperlyConfigured(
206+
'`FORCE_SCRIPT_NAME` must be set and must start with "/guest/"',
207+
)
208+
static_root = self.get_application().get_static_root()
209+
if not (
210+
settings.STATIC_ROOT.startswith(static_root)
211+
and len(settings.STATIC_ROOT) > len(static_root) - 1
212+
):
213+
raise ImproperlyConfigured(
214+
f'`STATIC_ROOT` must be a directory within {static_root}',
215+
)
216+
static_path = os.path.join(script_name, settings.STATIC_URL)
217+
app.mount(
218+
static_path,
219+
StaticFiles(
220+
directory=settings.STATIC_ROOT,
221+
),
222+
)
223+
app.mount(script_name, ASGIHandler())
224+
192225
def setup_middlewares(self, app, middlewares):
193226
for middleware in middlewares:
194227
if inspect.isfunction(middleware):

connect/eaas/runner/workers/base.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ async def process_setup_response(self, data):
221221
reconfigured, then restart the tasks manager.
222222
"""
223223
self.config.update_dynamic_config(data)
224+
if self.handler.django_settings_module:
225+
await self.invoke_hook('setup_django_settings')
224226
await self.trigger_event('on_startup')
225227
logger.info('Extension configuration has been updated.')
226228

@@ -235,19 +237,22 @@ async def trigger_event(self, event):
235237
return
236238
self.on_shutdown_fired.value = 1
237239

240+
await self.invoke_hook(event)
241+
242+
async def invoke_hook(self, hook_name):
238243
application = self.handler.get_application()
239-
event_handler = getattr(application, event, None)
244+
hook = getattr(application, hook_name, None)
240245
if (
241-
event_handler
242-
and inspect.ismethod(event_handler)
243-
and event_handler.__self__ is application
246+
hook
247+
and inspect.ismethod(hook)
248+
and hook.__self__ is application
244249
):
245-
if inspect.iscoroutinefunction(event_handler):
246-
await event_handler(self.handler.get_logger(), self.config.variables)
250+
if inspect.iscoroutinefunction(hook):
251+
await hook(self.handler.get_logger(), self.config.variables)
247252
else:
248253
await asyncio.get_event_loop().run_in_executor(
249254
None,
250-
event_handler,
255+
hook,
251256
self.handler.get_logger(),
252257
self.config.variables,
253258
)

0 commit comments

Comments
 (0)