Skip to content

Instantly share code, notes, and snippets.

@includeamin
Created October 27, 2020 10:11
Show Gist options
  • Save includeamin/3a827d3d4fba3b8842e48c68aa50b8bc to your computer and use it in GitHub Desktop.
Save includeamin/3a827d3d4fba3b8842e48c68aa50b8bc to your computer and use it in GitHub Desktop.
Gunicorn and Uvicorn multi port binding
from fastapi import FastAPI
from starlette_exporter import PrometheusMiddleware, handle_metrics
external = FastAPI()
internal = FastAPI()
external.add_middleware(PrometheusMiddleware)
@external.get("/users/information")
async def login(user_id: str):
return {"user_id": user_id}
@internal.get("/login")
async def login():
return {"login": "login"}
internal.add_route("/metrics", handle_metrics)
def run(scope):
if scope['server'][1] == 8080:
handler = internal
elif scope['server'][1] == 80:
handler = external
async def asgi(receive, send):
await handler(scope, receive, send)
return asgi
"""
gunicorn sample:run -w 2 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:80 --bind 0.0.0.0:8080 --threads=2
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment