[Serve] Produtionize Starlette Middlewares (#10529)

This commit is contained in:
Simon Mo
2020-09-03 17:31:38 -07:00
committed by GitHub
parent 0d93e92720
commit eff4375c3d
7 changed files with 71 additions and 8 deletions
+28
View File
@@ -316,3 +316,31 @@ To call a method via Python, do the following:
handle = serve.get_handle("backend_name")
handle.options(method_name="other_method").remote(5)
How do I enable CORS and other HTTP features?
---------------------------------------------
Serve supports arbitrary `Starlette middlewares <https://www.starlette.io/middleware/>`_
and custom middlewares in Starlette format. The example below shows how to enable
`Cross-Origin Resource Sharing (CORS) <https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS>`_.
You can follow the same pattern for other Starlette middlewares.
.. note::
Serve does not list ``Starlette`` as one of its dependencies. To utilize this feature,
you will need to:
.. code-block:: bash
pip install starlette
.. code-block:: python
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
serve.init(
http_middlewares=[
Middleware(
CORSMiddleware, allow_origins=["*"], allow_methods=["*"])
])