Lycodon/backend/src/lycodon/api/main.py

39 lines
926 B
Python

'''Lycodon Web API Server'''
from typing import Awaitable, Callable
import fastapi
from .. import __version__
from ..config import Configuration
from .context import Context
app = fastapi.FastAPI(
title='Lycodon',
description='Lycodon is a music library manager.',
version=__version__,
docs_url='/api-doc/',
redoc_url=None,
)
# Application context
context = Context()
context.config = Configuration()
@app.middleware('http')
async def context_middleware(
request: fastapi.Request,
call_next: Callable[[fastapi.Request], Awaitable[fastapi.Response]],
) -> fastapi.Response:
'''Middleware to add the Application Context to the request scope
This middleware function adds the :py:class:`Context` to the
ASGI request scope. To retrieve it, use
:py:func:`lycodon.api.context.get_context`.
'''
request.scope['lycodon_ctx'] = context
return await call_next(request)