Skip to content

Instantly share code, notes, and snippets.

@adriangb
Forked from Kludex/main.py
Last active February 10, 2022 17:03
Show Gist options
  • Save adriangb/a12d0c51fb852e679f0fc8b653d4d2eb to your computer and use it in GitHub Desktop.
Save adriangb/a12d0c51fb852e679f0fc8b653d4d2eb to your computer and use it in GitHub Desktop.
Run Xpresso and Uvicorn Programmatically
""" Snippet that demonstrates how to use Uvicorn in code.
Feel free to run:
- `python main.py`
"""
import asyncio
import uvicorn
from pydantic import BaseSettings
from xpresso import Path, App
class Config(BaseSettings):
port: int
async def home(config: Config) -> str:
return f"Hello World from {config.port}!"
home_path_item = Path("/", get=home)
async def main() -> None:
config = Config()
app = App(routes=[home_path_item])
app.dependency_overrides[Config] = lambda: config
await uvicorn.Server(uvicorn.Config(app, port=config.port)).serve()
if __name__ == "__main__":
asyncio.run(main())
uvicorn==0.17.4
xpresso==0.16.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment