Skip to content

Commit 25ba448

Browse files
Tomasz Wojciktomwojcik
authored andcommitted
improve readme
1 parent 0c5471b commit 25ba448

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ from starlette.applications import Starlette
3535
from starlette.middleware import Middleware
3636
from starlette.requests import Request
3737
from starlette.responses import JSONResponse
38+
from starlette.routing import Route
3839

3940
from starlette_context import context, plugins
4041
from starlette_context.middleware import ContextMiddleware
4142

43+
async def index(request: Request):
44+
# Access and store data in context
45+
context["custom_value"] = "example"
46+
return JSONResponse(context.data)
47+
48+
# Define routes
49+
routes = [
50+
Route("/", endpoint=index)
51+
]
52+
53+
# Configure middleware
4254
middleware = [
4355
Middleware(
4456
ContextMiddleware,
@@ -49,13 +61,11 @@ middleware = [
4961
)
5062
]
5163

52-
app = Starlette(middleware=middleware)
53-
54-
55-
@app.route("/")
56-
async def index(request: Request):
57-
return JSONResponse(context.data)
58-
64+
# Create application with routes and middleware
65+
app = Starlette(
66+
routes=routes,
67+
middleware=middleware
68+
)
5969

6070
if __name__ == "__main__":
6171
uvicorn.run(app, host="0.0.0.0")
@@ -66,7 +76,8 @@ In this example the response contains a JSON with:
6676
```json
6777
{
6878
"X-Correlation-ID": "5ca2f0b43115461bad07ccae5976a990",
69-
"X-Request-ID": "21f8d52208ec44948d152dc49a713fdd"
79+
"X-Request-ID": "21f8d52208ec44948d152dc49a713fdd",
80+
"custom_value": "example"
7081
}
7182
```
7283

0 commit comments

Comments
 (0)