Skip to content

Commit c8f552f

Browse files
committed
Add debugger support for fastapi
1 parent 5cc1a1d commit c8f552f

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

.vscode/launch.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"name": "Python: Attach",
8+
"name": "OL: Attach to Web Container",
99
"type": "debugpy",
1010
"request": "attach",
1111
"connect": {
@@ -18,6 +18,21 @@
1818
"remoteRoot": "/openlibrary/"
1919
}
2020
]
21+
},
22+
{
23+
"name": "OL: Attach to FastAPI Container",
24+
"type": "debugpy",
25+
"request": "attach",
26+
"connect": {
27+
"host": "localhost",
28+
"port": 3001
29+
},
30+
"pathMappings": [
31+
{
32+
"localRoot": "${workspaceFolder}",
33+
"remoteRoot": "/openlibrary/"
34+
}
35+
]
2136
}
2237
]
2338
}

compose.override.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ services:
3030
build:
3131
context: .
3232
dockerfile: docker/Dockerfile.oldev
33+
ports:
34+
# Debugger
35+
- 3001:3000
3336
volumes:
3437
- .:/openlibrary
3538
environment:

compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ services:
2020
image: "${OLIMAGE:-oldev:latest}"
2121
environment:
2222
- OL_CONFIG=${OL_CONFIG:-/openlibrary/conf/openlibrary.yml}
23-
- GUNICORN_OPTS=${GUNICORN_OPTS:- --reload --workers 2 --timeout 180 --max-requests 500}
23+
- GUNICORN_OPTS=${GUNICORN_OPTS:- --reload --workers 1 --timeout 180 --max-requests 500}
2424
command: docker/ol-web-fastapi-start.sh
2525
ports:
2626
- ${FAST_WEB_PORT:-18080}:8080

openlibrary/asgi_app.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,20 @@ async def set_context(request: Request, call_next):
178178
def health() -> dict[str, str]:
179179
return {"status": "ok"}
180180

181-
from openlibrary.fastapi.account import router as account_router # type: ignore
182-
from openlibrary.fastapi.languages import router as languages_router # type: ignore
183-
from openlibrary.fastapi.publishers import router as publishers_router # type: ignore
184-
from openlibrary.fastapi.search import router as search_router # type: ignore
185-
from openlibrary.fastapi.subjects import router as subjects_router # type: ignore
181+
from openlibrary.fastapi.account import router as account_router
182+
from openlibrary.fastapi.dev import router as dev_router
183+
from openlibrary.fastapi.languages import router as languages_router
184+
from openlibrary.fastapi.publishers import router as publishers_router
185+
from openlibrary.fastapi.search import router as search_router
186+
from openlibrary.fastapi.subjects import router as subjects_router
186187

187188
# Include routers
188189
app.include_router(languages_router)
189190
app.include_router(publishers_router)
190191
app.include_router(search_router)
191192
app.include_router(subjects_router)
192193
app.include_router(account_router)
194+
app.include_router(dev_router)
193195

194196
return app
195197

openlibrary/fastapi/dev.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
import debugpy # noqa: T100
4+
from fastapi import APIRouter
5+
6+
router = APIRouter()
7+
8+
9+
@router.get("/admin/attach_debugger")
10+
async def attach_debugger():
11+
"""
12+
Attach the debugger to the running container.
13+
"""
14+
if os.environ.get('LOCAL_DEV') != 'true':
15+
return {"status": "Debugger attachment is only allowed in LOCAL_DEV mode."}
16+
17+
debugpy.listen(('0.0.0.0', 3000)) # noqa: T100
18+
debugpy.wait_for_client() # noqa: T100
19+
return {"status": "Debugger attached."}

0 commit comments

Comments
 (0)