CORS support for bareasgi
Project description
# bareasgi-cors
CORS support for [bareASGI](http://github.com/rob-blackbourn/bareasgi) (read the [documentation](https://bareasgi-cors.readthedocs.io/en/latest/))
## Usage
A convenience function is provided.
```python
import json
import logging
import uvicorn
from bareasgi import (
Application,
text_reader,
text_writer
)
from bareasgi_cors import CORSMiddleware
logging.basicConfig(level=logging.DEBUG)
async def get_info(scope, info, matches, content):
text = json.dumps(info)
return 200, [(b'content-type', b'application/json')], text_writer(text)
async def set_info(scope, info, matches, content):
text = await text_reader(content)
data = json.loads(text)
info.update(data)
return 204, None, None
cors_middleware = CORSMiddleware()
app = Application(info={'name': 'Michael Caine'}, middlewares=[cors_middleware])
app.http_router.add({'GET'}, '/info', get_info)
app.http_router.add({'POST', 'OPTIONS'}, '/info', set_info)
uvicorn.run(app, port=9010)
```
## The POST method
In the above example an OPTION method is included with the POST. This
is always required with a POST as a browser will try first with an OPTION.
CORS support for [bareASGI](http://github.com/rob-blackbourn/bareasgi) (read the [documentation](https://bareasgi-cors.readthedocs.io/en/latest/))
## Usage
A convenience function is provided.
```python
import json
import logging
import uvicorn
from bareasgi import (
Application,
text_reader,
text_writer
)
from bareasgi_cors import CORSMiddleware
logging.basicConfig(level=logging.DEBUG)
async def get_info(scope, info, matches, content):
text = json.dumps(info)
return 200, [(b'content-type', b'application/json')], text_writer(text)
async def set_info(scope, info, matches, content):
text = await text_reader(content)
data = json.loads(text)
info.update(data)
return 204, None, None
cors_middleware = CORSMiddleware()
app = Application(info={'name': 'Michael Caine'}, middlewares=[cors_middleware])
app.http_router.add({'GET'}, '/info', get_info)
app.http_router.add({'POST', 'OPTIONS'}, '/info', set_info)
uvicorn.run(app, port=9010)
```
## The POST method
In the above example an OPTION method is included with the POST. This
is always required with a POST as a browser will try first with an OPTION.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
bareasgi_cors-1.0.0.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for bareasgi_cors-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 381288d7afdac035759bb033c0d7e4fb646d8fbbef9ce7b6e07177e3ac822b39 |
|
MD5 | dba4ec6c9895449d0a16968c31a1592f |
|
BLAKE2b-256 | 13139bc1486a5d7046e80ee124908277a672ec500e28859a79f2858cd3a4da48 |