Python builder for ASGI applications on Zeit Now
npm install @gbozee/now-python-asgiIf you have an existing WSGI app, getting this builder to work for you is a
piece of 🍰!
Add a now.json file to the root of your application:
``json`
{
"version": 2,
"name": "python-asgi-app",
"builds": [{
"src": "index.py",
"use": "@gbozee/now-python-asgi",
"config": { "maxLambdaSize": "15mb" }
}]
}@gbozee/now-python-asgi@1.0.4
NB: For ASGI2 support, Use
This configuration is doing a few things in the "builds" part:
1. "src": "index.py"index.py
This tells Now that there is one entrypoint to build for. is a"use": "@ardent-labs/now-python-wsgi"
file we'll create shortly.
2. "config": { "maxLambdaSize": "15mb" }
Tell Now to use this builder when deploying your application
3.
Bump up the maximum size of the built application to accommodate some larger
python WSGI libraries (like Django or Flask). This may not be necessary for
you.
Add index.py to the root of your application. This entrypoint should makeapplication
available an object named that is an instance of your WSGI
application. E.g.:
`pythonFor a sample Starlette app
from starlette.applications import Starlette
from starlette.responses import JSONResponse
application = Starlette()
@application.route('/')
async def homepage(request):
return JSONResponse({'hello': 'world'})
with the appropriate name to point towards your project'sIf the ASGI instance isn't named
application you can set the
wsgiApplicationName configuration option to match your application's name (see
the configuration section below).
$3
That's it, you're ready to go:
`
$ now
> Deploying python-asgi-app
...
> Success! Deployment ready [57s]
`
Requirements
Your project may optionally include a
requirements.txt file to declare any
dependencies.
Configuration options
$3
Select the lambda runtime. Defaults to
python3.7.
`json
{
"builds": [{
"config": { "runtime": "python3.7" }
}]
}
`
$3
Select the WSGI application to run from your entrypoint. Defaults to
application.
`json
{
"builds": [{
"config": { "asgiApplicationName": "application" }
}]
}
`
Additional considerations
$3
You'll likely want all requests arriving at your deployment url to be routed to
your application. You can do this by adding a route rewrite to the Now
configuration:
`json
{
"version": 2,
"name": "python-asgi-app",
"builds": [{
"src": "index.py",
"use": "@gbozee/now-python-asgi"
}],
"routes" : [{
"src" : "/(.*)", "dest":"/"
}]
}
`$3
If having an extra file in your project is troublesome or seems unecessary, it's
also possible to configure Now to use your application directly, without passing
it through
index.py.If your WSGI application lives in
now_app/wsgi.py and is named application,
then you can configure it as the entrypoint and adjust routes accordingly:
`json
{
"version": 2,
"name": "python-asgi-app",
"builds": [{
"src": "now_app/asgi.py",
"use": "@gbozee/now-python-asgi"
}],
"routes" : [{
"src" : "/(.*)", "dest":"/now_app/asgi.py"
}]
}
``At the time of writing, Zeit Now runs on AWS Lambda. This has a number of
implications on what libaries will be available to you, notably:
- PostgreSQL, so psycopg2 won't work out of the box
- MySQL, so MySQL adapters won't work out of the box either
- [ ] Add tests for various types of requests
This implementation draws upon work from:
- @erm on mangum
- @ardent-co on
now-python-wsgi