Created
April 23, 2024 21:54
-
-
Save joshuadavidthomas/0ceee3f4a948aa9a624f7c71e9dd49d6 to your computer and use it in GitHub Desktop.
Single file Django application with `django-allauth`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from pathlib import Path | |
from django import conf | |
from django import http | |
from django import setup | |
from django import urls | |
from django.contrib.auth.decorators import login_required | |
from django.core.handlers.wsgi import WSGIHandler | |
BASE_DIR = Path(__file__).parent | |
DOCS_ROOT = BASE_DIR / "_build" / "html" | |
conf.settings.configure( | |
ALLOWED_HOSTS="*", | |
AUTHENTICATION_BACKENDS=[ | |
"allauth.account.auth_backends.AuthenticationBackend", | |
], | |
BASE_DIR=BASE_DIR, | |
DATABASES={ | |
"default": { | |
"ENGINE": "django.db.backends.sqlite3", | |
"NAME": BASE_DIR / "db.sqlite3", | |
}, | |
}, | |
INSTALLED_APPS=[ | |
"allauth", | |
"allauth.account", | |
"allauth.socialaccount", | |
"allauth.socialaccount.providers.okta", | |
"django.contrib.auth", | |
"django.contrib.contenttypes", | |
"django.contrib.messages", | |
"django.contrib.sessions", | |
"django.contrib.staticfiles", | |
], | |
LOGIN_REDIRECT_URL="serve", | |
MEDIA_URL=None, | |
MIDDLEWARE=[ | |
"django.middleware.security.SecurityMiddleware", | |
"whitenoise.middleware.WhiteNoiseMiddleware", | |
"django.contrib.sessions.middleware.SessionMiddleware", | |
"django.middleware.common.CommonMiddleware", | |
"django.middleware.csrf.CsrfViewMiddleware", | |
"django.contrib.auth.middleware.AuthenticationMiddleware", | |
"django.contrib.messages.middleware.MessageMiddleware", | |
"django.middleware.clickjacking.XFrameOptionsMiddleware", | |
"allauth.account.middleware.AccountMiddleware", | |
], | |
ROOT_URLCONF=__name__, | |
SECRET_KEY="secret", | |
SOCIALACCOUNT_PROVIDERS={ | |
"okta": { | |
"APP": { | |
"client_id": "id", | |
"secret": "secret", | |
}, | |
"OKTA_BASE_URL": "org-name.okta.com", | |
"OAUTH_PKCE_ENABLED": True, | |
} | |
}, | |
STATIC_URL="/_static/", | |
STATIC_ROOT=DOCS_ROOT / "_static", | |
STORAGES={ | |
"staticfiles": { | |
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", | |
}, | |
}, | |
TEMPLATES=[ | |
{ | |
"BACKEND": "django.template.backends.django.DjangoTemplates", | |
"APP_DIRS": True, | |
"OPTIONS": { | |
"context_processors": [ | |
"django.template.context_processors.debug", | |
"django.template.context_processors.request", | |
"django.contrib.auth.context_processors.auth", | |
"django.contrib.messages.context_processors.messages", | |
], | |
}, | |
}, | |
], | |
) | |
setup() | |
app = WSGIHandler() | |
@login_required | |
def serve(request: http.HttpRequest, path: str) -> http.HttpResponse: | |
if not path: | |
path = "index.html" | |
try: | |
urls.resolve(f"/{path}") | |
except urls.Resolver404: | |
pass | |
else: | |
if not path.endswith(".html"): | |
return http.HttpResponseRedirect(f"/{path}/") | |
file_path = DOCS_ROOT / path | |
if not file_path.is_file(): | |
raise http.Http404("File not found") | |
with file_path.open("rb") as file: | |
return http.HttpResponse(file.read()) | |
urlpatterns = [ | |
urls.path("accounts/", urls.include("allauth.urls")), | |
urls.re_path(r"^(?P<path>.*)$", serve, name="serve"), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment