Skip to content

Commit

Permalink
[ENH] Add favicon and welcome message at root endpoint (#27)
Browse files Browse the repository at this point in the history
* add favicon and welcome message at root

* add comment about overriding docs
  • Loading branch information
alyssadai authored Sep 17, 2024
1 parent 698acee commit 585ebeb
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import ORJSONResponse
from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html
from fastapi.responses import HTMLResponse, ORJSONResponse, RedirectResponse

from app.api.utility import set_gh_credentials

from .api.routers import openneuro

FAVICON_URL = "https://raw.githubusercontent.com/neurobagel/documentation/main/docs/imgs/logo/neurobagel_favicon.png"


@asynccontextmanager
async def lifespan(app: FastAPI):
Expand All @@ -17,7 +20,13 @@ async def lifespan(app: FastAPI):
yield


app = FastAPI(default_response_class=ORJSONResponse, lifespan=lifespan)
app = FastAPI(
default_response_class=ORJSONResponse,
lifespan=lifespan,
# We will override the default docs with our own endpoints with a custom favicon
docs_url=None,
redoc_url=None,
)

app.add_middleware(
CORSMiddleware,
Expand All @@ -27,6 +36,55 @@ async def lifespan(app: FastAPI):
allow_headers=["*"],
)


# TODO: Should we exclude the root endpoint from the schema for a cleaner docs?
@app.get("/", response_class=HTMLResponse)
def root():
"""
Display a welcome message and a link to the API documentation.
"""
return """
<html>
<body>
<h1>Welcome to the API for <a href="https://github.com/OpenNeuroDatasets-JSONLD" target="_blank">Neurobagel-annotated OpenNeuro Datasets!</a></h1>
<p>Please visit the <a href="/docs">documentation</a> to view available API endpoints.</p>
</body>
</html>
"""


@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
"""
Overrides the default favicon with a custom one.
"""
return RedirectResponse(url=FAVICON_URL)


@app.get("/docs", include_in_schema=False)
def overridden_swagger():
"""
Overrides the Swagger UI HTML for the "/docs" endpoint.
"""
return get_swagger_ui_html(
openapi_url="/openapi.json",
title="Neurobagel OpenNeuro Datasets API",
swagger_favicon_url=FAVICON_URL,
)


@app.get("/redoc", include_in_schema=False)
def overridden_redoc():
"""
Overrides the Redoc HTML for the "/redoc" endpoint.
"""
return get_redoc_html(
openapi_url="/openapi.json",
title="Neurobagel OpenNeuro Datasets API",
redoc_favicon_url=FAVICON_URL,
)


app.include_router(openneuro.router)

if __name__ == "__main__":
Expand Down

0 comments on commit 585ebeb

Please sign in to comment.