1.0.27 • Published 4 years ago

@gbozee/now-python-asgi v1.0.27

Weekly downloads
26
License
MIT
Repository
github
Last release
4 years ago

now-python-asgi

A Now builder for Python ASGI applications

Quickstart

If you have an existing WSGI app, getting this builder to work for you is a piece of 🍰!

1. Add a Now configuration

Add a now.json file to the root of your application:

{
    "version": 2,
    "name": "python-asgi-app",
    "builds": [{
        "src": "index.py",
        "use": "@gbozee/now-python-asgi",
        "config": { "maxLambdaSize": "15mb" }
    }]
}

NB: For ASGI2 support, Use @gbozee/now-python-asgi@1.0.4

This configuration is doing a few things in the "builds" part:

  1. "src": "index.py" This tells Now that there is one entrypoint to build for. index.py is a file we'll create shortly.
  2. "use": "@ardent-labs/now-python-wsgi" Tell Now to use this builder when deploying your application
  3. "config": { "maxLambdaSize": "15mb" } 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.

2. Add a Now entrypoint

Add index.py to the root of your application. This entrypoint should make available an object named application that is an instance of your WSGI application. E.g.:

# For 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'})

# Replace `django_app` with the appropriate name to point towards your project's
# wsgi.py file

If 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. Deploy!

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

runtime

Select the lambda runtime. Defaults to python3.7.

{
    "builds": [{
        "config": { "runtime": "python3.7" }
    }]
}

wsgiApplicationName

Select the WSGI application to run from your entrypoint. Defaults to application.

{
    "builds": [{
        "config": { "asgiApplicationName": "application" }
    }]
}

Additional considerations

Routing

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:

{
    "version": 2,
    "name": "python-asgi-app",
    "builds": [{
        "src": "index.py",
        "use": "@gbozee/now-python-asgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/"
    }]
}

Avoiding the index.py file

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:

{
    "version": 2,
    "name": "python-asgi-app",
    "builds": [{
        "src": "now_app/asgi.py",
        "use": "@gbozee/now-python-asgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/now_app/asgi.py"
    }]
}

Lambda environment limitations

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

Contributing

To-dos

  • Add tests for various types of requests

Attribution

This implementation draws upon work from:

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago