-
-
Save adriangb/a12d0c51fb852e679f0fc8b653d4d2eb to your computer and use it in GitHub Desktop.
Run Xpresso and Uvicorn Programmatically
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
""" 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()) |
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
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