|
| 1 | +# Deployment |
| 2 | + |
| 3 | +After building a Connect service, you still need to deploy it to production. This guide covers how to |
| 4 | +use a Python application server to run a service. |
| 5 | + |
| 6 | +## Application Servers |
| 7 | + |
| 8 | +Connect services are standard ASGI or WSGI applications that can be run with an off-the-shelf Python |
| 9 | +application server. This means generally, any experience in running a Python application can be applied |
| 10 | +to running Connect as-is. |
| 11 | + |
| 12 | +### HTTP/1.1 |
| 13 | + |
| 14 | +Connect only requires HTTP/2 for bidirectional streaming RPCs - the vast majority of services can be run |
| 15 | +using HTTP/1.1 without issue. Two servers have long been in use with HTTP/1.1 and have a proven track |
| 16 | +record. If you are unsure what server to use, it is generally a safe bet to use one of them: |
| 17 | + |
| 18 | +- [gunicorn](http://www.gunicorn.org/) for WSGI applications |
| 19 | +- [uvicorn](https://uvicorn.dev/) for ASGI applications |
| 20 | + |
| 21 | +### HTTP/2 |
| 22 | + |
| 23 | +If your service uses bidirectional or otherwise want to use HTTP/2, the above servers will not work. |
| 24 | +HTTP/2 support in the Python ecosystem is still relatively young - servers known to support HTTP/2 |
| 25 | +with bidirectional streaming are: |
| 26 | + |
| 27 | +- [granian](https://github.com/emmett-framework/granian) |
| 28 | +- [hypercorn](https://hypercorn.readthedocs.io/en/latest/) |
| 29 | +- [pyvoy](https://github.com/curioswitch/pyvoy) |
| 30 | + |
| 31 | +Connect has an extensive test suite to verify compatibility of connect-python with the Connect protocol. |
| 32 | +Unfortunately, we are only able to reliably pass the suite with pyvoy, with other servers occasionally |
| 33 | +having hung requests or stream ordering issues. pyvoy was built with connect-python in mind but is |
| 34 | +very new and needs more time with real-world applications to verify stability. |
| 35 | + |
| 36 | +Keep the above in mind when picking an HTTP/2 server and let us know how it goes if you give any a try. |
| 37 | +When in doubt, if you do not use bidirectional streaming, we recommend one of the HTTP/1.1 servers. |
| 38 | + |
| 39 | +## CORS |
| 40 | + |
| 41 | +Connect services are standard ASGI and WSGI applications so any CORS middleware can be used to |
| 42 | +enable it. |
| 43 | + |
| 44 | +For example, with an ASGI application using Starlette: |
| 45 | + |
| 46 | +```python |
| 47 | +from starlette.middleware.cors import CORSMiddleware |
| 48 | +from starlette.applications import Starlette |
| 49 | + |
| 50 | +app = Starlette() |
| 51 | +app.add_middleware( |
| 52 | + CORSMiddleware, |
| 53 | + allow_origins=["*"], |
| 54 | + allow_methods=["GET", "POST"], |
| 55 | + allow_headers=["*"], |
| 56 | +) |
| 57 | +# Mount your Connect application |
| 58 | +``` |
0 commit comments