From 0fa2f2da3b0c1064c27b5e2851c147843c75a450 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 28 Dec 2020 13:26:12 +0200 Subject: [PATCH] Make the fastapi application the main asgi one. --- etebase_fastapi/{app.py => main.py} | 13 +++---------- etebase_server/asgi.py | 21 +++++++++++---------- etebase_server/urls.py | 15 ++++++++++++++- etebase_server/wsgi.py | 16 ---------------- 4 files changed, 28 insertions(+), 37 deletions(-) rename etebase_fastapi/{app.py => main.py} (85%) delete mode 100644 etebase_server/wsgi.py diff --git a/etebase_fastapi/app.py b/etebase_fastapi/main.py similarity index 85% rename from etebase_fastapi/app.py rename to etebase_fastapi/main.py index 6dffef4..2c10854 100644 --- a/etebase_fastapi/app.py +++ b/etebase_fastapi/main.py @@ -1,15 +1,7 @@ -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "etebase_server.settings") -application = get_wsgi_application() - from django.conf import settings # Not at the top of the file because we first need to setup django from fastapi import FastAPI, Request -from fastapi.middleware.wsgi import WSGIMiddleware from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.trustedhost import TrustedHostMiddleware @@ -30,15 +22,16 @@ app.include_router(item_router, prefix=f"{BASE_PATH}/collection/{COLLECTION_UID_ app.include_router(member_router, prefix=f"{BASE_PATH}/collection/{COLLECTION_UID_MARKER}", tags=["member"]) app.include_router(invitation_incoming_router, prefix=f"{BASE_PATH}/invitation/incoming", tags=["incoming invitation"]) app.include_router(invitation_outgoing_router, prefix=f"{BASE_PATH}/invitation/outgoing", tags=["outgoing invitation"]) + if settings.DEBUG: - from .test_reset_view import test_reset_view_router + from etebase_fastapi.test_reset_view import test_reset_view_router app.include_router(test_reset_view_router, prefix=f"{BASE_PATH}/test/authentication") + app.add_middleware( CORSMiddleware, allow_origin_regex="https?://.*", allow_credentials=True, allow_methods=["*"], allow_headers=["*"] ) app.add_middleware(TrustedHostMiddleware, allowed_hosts=settings.ALLOWED_HOSTS) -app.mount("/", WSGIMiddleware(application)) @app.exception_handler(CustomHttpException) diff --git a/etebase_server/asgi.py b/etebase_server/asgi.py index 0bf63ec..92fad1c 100644 --- a/etebase_server/asgi.py +++ b/etebase_server/asgi.py @@ -1,16 +1,17 @@ -""" -ASGI config for etebase_server project. - -It exposes the ASGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ -""" - import os from django.core.asgi import get_asgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "etebase_server.settings") +django_application = get_asgi_application() + + +def create_application(): + from etebase_fastapi.main import app + + app.mount("/", django_application) + + return app + -application = get_asgi_application() +application = create_application() diff --git a/etebase_server/urls.py b/etebase_server/urls.py index f285977..443763d 100644 --- a/etebase_server/urls.py +++ b/etebase_server/urls.py @@ -1,8 +1,12 @@ +import os + from django.conf import settings from django.conf.urls import include, url from django.contrib import admin -from django.urls import path +from django.urls import path, re_path from django.views.generic import TemplateView +from django.views.static import serve +from django.contrib.staticfiles import finders urlpatterns = [ url(r"^api/", include("django_etebase.urls")), @@ -14,3 +18,12 @@ if settings.DEBUG: urlpatterns += [ url(r"^api-auth/", include("rest_framework.urls", namespace="rest_framework")), ] + + def serve_static(request, path): + filename = finders.find(path) + dirname = os.path.dirname(filename) + basename = os.path.basename(filename) + + return serve(request, basename, dirname) + + urlpatterns += [re_path(r"^static/(?P.*)$", serve_static)] diff --git a/etebase_server/wsgi.py b/etebase_server/wsgi.py deleted file mode 100644 index 908f88c..0000000 --- a/etebase_server/wsgi.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -WSGI config for etebase_server project. - -It exposes the WSGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ -""" - -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "etebase_server.settings") - -application = get_wsgi_application()