Skip to main content

valar for morghulis

Project description

valar for morghulis

1. installation

pip install valar

1. settings

from pathlib import Path

""" Compulsory settings """
DEBUG = True
BASE_DIR = Path(__file__).resolve().parent.parent
BASE_APP = str(BASE_DIR.name)
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
SECRET_KEY = 'django-insecure-of@tfouoq^_f$l!yki#m=6j7)@&kjri$1_$!mca-=%7=+@f@5^'

""" Minimized compulsory settings """

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

INSTALLED_APPS = [
    'django.contrib.sessions',
    "corsheaders",
    'channels',
    'valar.apps.ValarConfig',
]

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
]

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    }
}

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
ROOT_URLCONF = "%s.urls" % BASE_APP
ASGI_APPLICATION = "%s.asgi.application" % BASE_APP

""" Optional settings """
ALLOWED_HOSTS = ['*']
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_TZ = False
SESSION_SAVE_EVERY_REQUEST = True
SESSION_COOKIE_AGE = 60 * 60
FILE_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 100
DATA_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 100

""" Valar Options """

HANDLER_MAPPING = "%s.urls.channel_mapping" % BASE_APP
MONGO_URI = 'mongodb://username:password@host:27017'
MINIO_URL = "s3://username:password@host:9000"
MINIO_ROOT = "https://host:9001"

2. asgi

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import re_path

from valar.channels.consumer import ValarConsumer

application = ProtocolTypeRouter({
    'http': get_asgi_application(),
    'websocket': URLRouter([
        re_path(r'(?P<client>\w+)/$', ValarConsumer.as_asgi()),
    ])
})

3. migrate

  • no need to makemigrations and migrate for valar, valar will auto migration

4. root urls

no need to provide urls for val, valar will auto set urlpatterns for Morghulis ( see https://www.npmjs.com/package/morghulis)

5. how to register a channel handler for Morghulis async methods

5.1 create a handler

import time
from valar.channels.sender import ValarChannelSender
from valar.channels.counter import Counter


def valar_test_handler(sender: ValarChannelSender):
    data = sender.data
    length = data.get('length', 100)
    counter = Counter(length)
    for i in range(length):
        time.sleep(0.1)
        tick = counter.tick()
        tick.update({'name': 'test1'})
        sender.load(tick)

5.2 create a dict (e.g. using the name 'channel_mapping') to save your handler

  • I'd like to put it in the root urls.py, you can put it anywhere
channel_mapping = {
    'test': valar_test_handler,
}

5.2 register the channel_mapping in the settings.py

HANDLER_MAPPING = "%s.urls.channel_mapping" % BASE_APP

5.4 you can copy the following codes to your urls.py

import json

from django.urls import path

from valar.classes.valar_response import ValarResponse
from valar.views.handler import valar_test_handler


def test_request(request):
    body = json.loads(request.body)
    return ValarResponse(body)


urlpatterns = [
    path('test', test_request),
]

channel_mapping = {
    'test_handler': valar_test_handler,
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

valar-1.2.4.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

valar-1.2.4-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

File details

Details for the file valar-1.2.4.tar.gz.

File metadata

  • Download URL: valar-1.2.4.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for valar-1.2.4.tar.gz
Algorithm Hash digest
SHA256 8ed8403593d29eb5f8abfd21a816f81e757ea6abeec4484ce95289f28f488a8f
MD5 3410592b83d24dd57878eb6da676c2e2
BLAKE2b-256 b83b213109fbf9925e0ddc5fa9c4f9e22c1f570e03c4c4f8ffe9aa4f7bfc62fd

See more details on using hashes here.

File details

Details for the file valar-1.2.4-py3-none-any.whl.

File metadata

  • Download URL: valar-1.2.4-py3-none-any.whl
  • Upload date:
  • Size: 33.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for valar-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f54a67fefdda0a12d9ed3d9a27ac9fdb5aad00de45a2d76f586cd769eb161337
MD5 daab3081c88231acf907c3d3ddf079fc
BLAKE2b-256 792e14cda097f8f0b17499a9941650a2426b173ca3436b45b80099a90c4294ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page