Skip to content

Commit fe87a3d

Browse files
committed
feat(passport): switch wsgi to asgi
1 parent 3b43465 commit fe87a3d

10 files changed

Lines changed: 33 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ develop-eggs
1414
lib
1515
lib64
1616
.env
17+
.vscode
1718

1819
# coverage reports
1920
.coverage

rootfs/api/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for passport project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings.production')
15+
16+
application = get_asgi_application()

rootfs/api/settings/production.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@
284284
# https://docs.djangoproject.com/en/2.2/howto/static-files/
285285
STATIC_URL = '/assets/'
286286
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'web', 'dist', 'assets'))
287-
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
287+
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
288288

289289
# Avatar URL
290290
AVATAR_URL = "https://cravatar.cn/avatar/"

rootfs/api/urls.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from django.conf.urls import include
2-
from django.urls import re_path
1+
from django.urls import re_path, include
32
from rest_framework.routers import DefaultRouter
43

54
from api import views

rootfs/api/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from django.shortcuts import redirect, get_object_or_404, render
1313
from django.template.loader import render_to_string
1414
from django.http import HttpResponse, HttpResponseRedirect
15-
from django.utils.encoding import force_bytes, force_text
15+
from django.utils.encoding import force_bytes, force_str
1616
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
1717
from django.views.generic import View
1818
from django.views.generic.edit import CreateView
@@ -109,7 +109,7 @@ class ActivateAccount(View):
109109

110110
def get(self, request, uidb64, token, *args, **kwargs):
111111
try:
112-
uid = force_text(urlsafe_base64_decode(uidb64))
112+
uid = force_str(urlsafe_base64_decode(uidb64))
113113
user = User.objects.get(pk=uid)
114114
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
115115
user = None
@@ -182,7 +182,7 @@ def update(self, request, *args, **kwargs):
182182
class UpdateAccount(View):
183183

184184
def get(self, request, uidb64, token, *args, **kwargs):
185-
user = get_object_or_404(User, pk=force_text(urlsafe_base64_decode(uidb64)))
185+
user = get_object_or_404(User, pk=force_str(urlsafe_base64_decode(uidb64)))
186186
if user is not None and token_generator.check_token(user, token):
187187
cache_key = "user:serializer:%s" % user.pk
188188
data = cache.get(cache_key, None)

rootfs/api/wsgi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
2-
WSGI config for mysite project.
2+
WSGI config for passport project.
33
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
7+
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
88
"""
99

1010
import os

rootfs/bin/boot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ python /workspace/manage.py create_oauth2_application
4343
# spawn a gunicorn server in the background
4444
echo ""
4545
echo "Starting up Gunicorn"
46-
gunicorn -c /workspace/passport/gunicorn/config.py api.wsgi &
46+
gunicorn -c /workspace/passport/gunicorn/config.py api.asgi &
4747

4848
# smart shutdown on SIGTERM (SIGINT is handled by gunicorn)
4949
function on_exit() {

rootfs/passport/gunicorn/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
bind = '0.0.0.0'
88
workers = int(os.environ.get('GUNICORN_WORKERS', 4))
9+
worker_class = "uvicorn.workers.UvicornWorker"
910

1011
pythonpath = dirname(dirname(dirname(realpath(__file__))))
1112
timeout = 1200

rootfs/passport/urls.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
1. Import the include() function: from django.urls import include, path
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
16-
from django.urls import path
16+
from django.urls import path, re_path, include
1717
from django.conf import settings
18-
from django.conf.urls import include, url, re_path
1918
from django.contrib import admin
2019
from django.views.generic.base import TemplateView
2120
from api.views import LivenessCheckView, ReadinessCheckView
@@ -27,8 +26,8 @@
2726
urlpatterns = []
2827

2928
urlpatterns += [
30-
url(r'^healthz$', LivenessCheckView.as_view()),
31-
url(r'^readiness$', ReadinessCheckView.as_view()),
29+
re_path(r'^healthz$', LivenessCheckView.as_view()),
30+
re_path(r'^readiness$', ReadinessCheckView.as_view()),
3231
re_path(r"settings/?$", views.SettingsViewSet.as_view({'get': 'retrieve'})),
3332
re_path(r"^user/", include('api.urls')),
3433
re_path(r'^oauth/', include('oauth2_provider.urls', namespace='oauth2_provider')),

rootfs/requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Drycc passport requirements
2-
django==3.2.5
2+
django==4.1
3+
pytz==2022.2.1
34
django-auth-ldap==3.0.0
45
django-cors-headers==3.7.0
56
djangorestframework==3.12.4
67
gunicorn==20.1.0
8+
uvicorn==0.18.2
9+
asgiref==3.5.2
710
psycopg2-binary==2.9.1
811
requests==2.26.0
912
requests-toolbelt==0.9.1

0 commit comments

Comments
 (0)