Created
October 14, 2020 14:49
-
-
Save FlorianLudwig/96702ecc9da9034fad0ea4c625fc962b to your computer and use it in GitHub Desktop.
FastAPI With two ports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
hypercorn main:multi_port -k uvloop -b 127.0.0.1:8000 -b 127.0.0.1:8001 | |
""" | |
from fastapi import FastAPI | |
from starlette_exporter import PrometheusMiddleware, handle_metrics | |
app = FastAPI() | |
app.add_middleware(PrometheusMiddleware) | |
@app.get("/") | |
def read_root(): | |
return {"Hello": "World"} | |
app_meta = FastAPI() | |
app_meta.add_route("/metrics", handle_metrics) | |
def multi_port(scope): | |
if scope["server"][1] == 8000: | |
handler = app | |
elif scope["server"][1] == 8001: | |
handler = app_meta | |
async def asgi(receive, send): | |
await handler(scope, receive, send) | |
return asgi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample.py