diff options
Diffstat (limited to 'fatcat_scholar/web.py')
-rw-r--r-- | fatcat_scholar/web.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py index 124e269..dce732b 100644 --- a/fatcat_scholar/web.py +++ b/fatcat_scholar/web.py @@ -13,6 +13,7 @@ import babel.support from fastapi import FastAPI, APIRouter, Request, Depends, Response, HTTPException from fastapi.staticfiles import StaticFiles from fastapi.responses import PlainTextResponse, JSONResponse, FileResponse +from fastapi.middleware.cors import CORSMiddleware import sentry_sdk from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from starlette_prometheus import metrics, PrometheusMiddleware @@ -97,6 +98,8 @@ class HitsModel(BaseModel): @api.get("/search", operation_id="get_search", response_model=HitsModel) async def search(query: FulltextQuery = Depends(FulltextQuery)) -> FulltextHits: hits: Optional[FulltextHits] = None + if query.q is None: + raise HTTPException(status_code=400, detail="Expected a 'q' query parameter") try: hits = process_query(query) except ValueError as e: @@ -320,6 +323,14 @@ async def http_exception_handler(request: Request, exc: StarletteHTTPException) # configure middleware +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=False, + allow_methods=["GET"], + allow_headers=[], # some defaults always enabled +) + if settings.SENTRY_DSN: logger.info("Sentry integration enabled") sentry_sdk.init( |