Verified Commit 4caebeb6 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

WIP ASGI

parent 4f83d1bb
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.base")
import django # noqa
django.setup()
from .routing import application # noqa
import os
import sys
app_path = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
)
sys.path.append(os.path.join(app_path, "retribute_api"))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.base")
from django.conf.urls import url from django.conf.urls import url
from channels.routing import ProtocolTypeRouter, URLRouter from channels.routing import ProtocolTypeRouter, URLRouter, AsgiHandler
from retribute_api.search import consumers from retribute_api.search import consumers
...@@ -11,7 +21,8 @@ application = ProtocolTypeRouter( ...@@ -11,7 +21,8 @@ application = ProtocolTypeRouter(
url( url(
r"^api/v1/search/(?P<lookup_type>.+):(?P<lookup>.+)$", r"^api/v1/search/(?P<lookup_type>.+):(?P<lookup>.+)$",
consumers.SearchSingleConsumer, consumers.SearchSingleConsumer,
) ),
url(r"", AsgiHandler),
] ]
) )
} }
......
...@@ -52,7 +52,7 @@ DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # no ...@@ -52,7 +52,7 @@ DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # no
ROOT_URLCONF = "config.urls" ROOT_URLCONF = "config.urls"
# https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application # https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = "config.wsgi.application" WSGI_APPLICATION = "config.wsgi.application"
ASGI_APPLICATION = "config.routing.application"
# APPS # APPS
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
DJANGO_APPS = [ DJANGO_APPS = [
...@@ -63,7 +63,7 @@ DJANGO_APPS = [ ...@@ -63,7 +63,7 @@ DJANGO_APPS = [
"django.contrib.staticfiles", "django.contrib.staticfiles",
"django.contrib.admin", "django.contrib.admin",
] ]
THIRD_PARTY_APPS = ["rest_framework"] THIRD_PARTY_APPS = ["rest_framework", "channels"]
LOCAL_APPS = [ LOCAL_APPS = [
# Your stuff: custom apps go here # Your stuff: custom apps go here
] ]
...@@ -180,15 +180,6 @@ EMAIL_BACKEND = env( ...@@ -180,15 +180,6 @@ EMAIL_BACKEND = env(
"DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.console.EmailBackend" "DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.console.EmailBackend"
) )
# ADMIN
# ------------------------------------------------------------------------------
# Django Admin URL.
ADMIN_URL = "admin/"
# https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = [("""Eliot Berriot""", "contact@eliotberriot.com")]
# https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS
# LOGGING # LOGGING
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#logging # https://docs.djangoproject.com/en/dev/ref/settings/#logging
...@@ -228,6 +219,13 @@ ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["retribute.me"]) ...@@ -228,6 +219,13 @@ ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["retribute.me"])
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
CACHES = {"default": env.cache()} CACHES = {"default": env.cache()}
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {"hosts": [CACHES["default"]["LOCATION"]]},
}
}
# SECURITY # SECURITY
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header # https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
...@@ -280,7 +278,7 @@ EMAIL_SUBJECT_PREFIX = env("DJANGO_EMAIL_SUBJECT_PREFIX", default="[Retribute AP ...@@ -280,7 +278,7 @@ EMAIL_SUBJECT_PREFIX = env("DJANGO_EMAIL_SUBJECT_PREFIX", default="[Retribute AP
# ADMIN # ADMIN
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Django Admin URL regex. # Django Admin URL regex.
ADMIN_URL = env("DJANGO_ADMIN_URL", default="^api/admin/") ADMIN_URL = env("DJANGO_ADMIN_URL", default="admin/")
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise # http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise
......
"""
WSGI config for Retribute API project.
This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.
Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.
"""
import os
import sys
from django.core.wsgi import get_wsgi_application
# This allows easy placement of apps within the interior
# retribute_api directory.
app_path = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
)
sys.path.append(os.path.join(app_path, "retribute_api"))
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.production"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
application = get_wsgi_application()
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
...@@ -3,6 +3,7 @@ import json ...@@ -3,6 +3,7 @@ import json
from channels.generic.http import AsyncHttpConsumer from channels.generic.http import AsyncHttpConsumer
from . import exceptions
from . import sources from . import sources
...@@ -23,9 +24,13 @@ class SearchSingleConsumer(AsyncHttpConsumer): ...@@ -23,9 +24,13 @@ class SearchSingleConsumer(AsyncHttpConsumer):
except KeyError: except KeyError:
await json_response(self, 400, {"detail": "Invalid lookup"}) await json_response(self, 400, {"detail": "Invalid lookup"})
try: try:
import ipdb
ipdb.set_trace()
async with aiohttp.client.ClientSession() as session: async with aiohttp.client.ClientSession() as session:
data = await source.get(lookup, session) data = await source.get(lookup, session)
except Exception: except exceptions.SearchError as e:
await json_response(self, 400, {"detail": e.message})
raise raise
try: try:
profile = sources.result_to_retribute_profile(lookup_type, lookup, data) profile = sources.result_to_retribute_profile(lookup_type, lookup, data)
......
class SearchError(ValueError): class SearchError(ValueError):
pass message = "Error while retrieving remote profile"
class InvalidLookup(SearchError):
message = "Invalid lookup"
class MeansNotFound(SearchError): class MeansNotFound(SearchError):
pass message = "No payment means found on remote profile"
class SkippedProfile(SearchError): class SkippedProfile(SearchError):
pass message = "Profile skipped"
from rest_framework import serializers from rest_framework import serializers
from . import exceptions
async def lookup(name, session): async def lookup(name, session):
username, domain = name.split("@") try:
username, domain = name.split("@")
except ValueError:
raise exceptions.InvalidLookup()
async with session.get( async with session.get(
"https://{}/.well-known/webfinger".format(domain), "https://{}/.well-known/webfinger".format(domain),
params={"resource": "acct:{}".format(name)}, params={"resource": "acct:{}".format(name)},
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment