Skip to content

Instantly share code, notes, and snippets.

@danielballan
Created March 10, 2023 15:21

Revisions

  1. danielballan created this gist Mar 10, 2023.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    ## Tasks

    1. Run `python middlware.py`.
    2. Using HTTPie, request data: `http :8000/data`. This should return the list of numbers from the server.
    3. Add a simple [middleware](https://fastapi.tiangolo.com/tutorial/middleware/) that just uses `print` to print a message in the server logs before and after a request is seen.
    4. Add a second middleware that also prints.

    Question: Do the middlewares get run in the order that they are added in `middlware.py` or in the _reverse_ order?
    14 changes: 14 additions & 0 deletions middleware.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    from fastapi import FastAPI, Request

    app = FastAPI()


    @app.get("/data")
    def data():
    return [1, 2, 3]


    if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)