Getting Started with bareASGI

ASGI Servers

$ pip install hypercorn

Hello, World!

import asyncio
from hypercorn.asyncio import serve
from hypercorn.config import Config
from bareasgi import Application, bytes_writer
app = Application()@app.on_http_request({'GET'}, '/hello-world')
async def http_request_handler(scope, info, matches, content):
headers = [
(b'content-type', b'text/plain')
]
return 200, headers, bytes_writer(b'Hello, World!'), None
config = Config()
config.bind = ["0.0.0.0:9009"]
asyncio.run(serve(app, config))

The HTTP Request Handler

The HTTP Response

async def bytes_writer(buf, chunk_size = -1):
if chunk_size == -1:
yield buf
else:
start, end = 0, chunk_size
while start < len(buf):
yield buf[start:end]
start, end = end, end + chunk_size

Routing

@app.on_http_request({'GET'}, '/hello-world')
import asyncio
from hypercorn.asyncio import serve
from hypercorn.config import Config
from bareasgi import Application, bytes_writer
async def http_request_callback(scope, info, matches, content):
headers = [
(b'content-type', b'text/plain')
]
return 200, headers, bytes_writer(b'Hello, World!')
app = Application()
app.http_router.add({'GET'}, '/hello-world')
config = Config()
config.bind = ["0.0.0.0:9009"]
asyncio.run(serve(app, config))
'/foo/{name}/{id:int}/{created:datetime:%Y-%m-%d}'

REST

import asyncio
import json
from hypercorn.asyncio import serve
from hypercorn.config import Config
from bareasgi import Application, text_reader, text_writer
import bareutils.header as header
async def get_info(scope, info, matches, content):
accept = header.find(b'accept', scope['headers'])
if accept != b'application/json':
return 500
text = json.dumps(info)
headers = [
(b'content-type', b'application/json')
]
return 200, headers, text_writer(text)
async def set_info(scope, info, matches, content):
content_type = header.find(b'content-type', scope['headers'])
if content_type != b'application/json':
return 500
text = await text_reader(content)
data = json.loads(text)
info.update(data)
return 204
app = Application(info={'name': 'Michael Caine'})
app.http_router.add({'GET'}, '/info', get_info)
app.http_router.add({'POST'}, '/info', set_info)
config = Config()
config.bind = ["0.0.0.0:9009"]
asyncio.run(serve(app, config))

Wrapping Up

--

--

--

GitHub: https://github.com/rob-blackbourn

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How we migrated our Drupal 8 sites to use Composer (in 8 steps)

10+ Best Python Tutorials for Beginners — Learn Python Online

What are the popular Content Management Systems (CMS) in PHP?

Rabbit Vs Kafka

The best IDEs and Text Editors for Python

A Short Discussion About C++

SOLID principle in GO

Build a RESTful API with the Serverless Framework

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rob Blackbourn

Rob Blackbourn

GitHub: https://github.com/rob-blackbourn

More from Medium

Python’s powerful “yield” keyword

Creating a Book Class 📚

Object Oriented Concepts

Glob Module in Python-Explained

a man holding bunch of files and looking for specific files.