Skip to content

Commit

Permalink
Integrate asgi-correlation-id.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Jan 23, 2024
1 parent 97ad75b commit 84f98ce
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ all = [
"aiosqlite",
"alembic",
"anyio",
"asgi-correlation-id",
"asyncpg",
"appdirs",
"awkward >=2.4.3",
Expand Down Expand Up @@ -183,6 +184,7 @@ minimal-server = [
"alembic",
"anyio",
"appdirs",
"asgi-correlation-id",
"cachey",
"cachetools",
"click !=8.1.0",
Expand Down Expand Up @@ -218,6 +220,7 @@ server = [
"alembic",
"anyio",
"appdirs",
"asgi-correlation-id",
"asyncpg",
"awkward >=2.4.3",
"blosc",
Expand Down
1 change: 1 addition & 0 deletions tiled/adapters/tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def metadata(self):
return d

def read(self, slice=None):
raise Exception("BOOM!")
# TODO Is there support for reading less than the whole array
# if we only want a slice? I do not think that is possible with a
# single-page TIFF but I'm not sure. Certainly it *is* possible for
Expand Down
5 changes: 4 additions & 1 deletion tiled/commandline/_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def serve_directory(
),
),
port: int = typer.Option(8000, help="Bind to a socket with this port."),
log_config: Optional[str] = typer.Option(
None, help="Custom uvicorn logging configuration file"
),
object_cache_available_bytes: Optional[float] = typer.Option(
None,
"--data-cache",
Expand Down Expand Up @@ -243,7 +246,7 @@ async def walk_and_serve():

import uvicorn

uvicorn.run(web_app, host=host, port=port)
uvicorn.run(web_app, host=host, port=port, log_config=log_config)


def serve_catalog(
Expand Down
23 changes: 23 additions & 0 deletions tiled/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import anyio
import packaging.version
import yaml
from asgi_correlation_id import CorrelationIdMiddleware, correlation_id
from fastapi import APIRouter, FastAPI, HTTPException, Request, Response, Security
from fastapi.exception_handlers import http_exception_handler
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import HTMLResponse, JSONResponse
Expand Down Expand Up @@ -296,7 +298,28 @@ async def unsupported_query_type_exception_handler(
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
# allow_headers=["X-Requested-With", "X-Request-ID"],
expose_headers=["X-Tiled-Request-ID"],
)
app.add_middleware(
CorrelationIdMiddleware,
header_name="X-Tiled-Request-ID",
update_request_header=True,
# generator=lambda: uuid4().hex,
)

@app.exception_handler(Exception)
async def unhandled_exception_handler(
request: Request, exc: Exception
) -> JSONResponse:
return await http_exception_handler(
request,
HTTPException(
500,
"Internal server error",
headers={"X-Request-ID": correlation_id.get() or ""},
),
)

app.include_router(router, prefix="/api/v1")

Expand Down

0 comments on commit 84f98ce

Please sign in to comment.