2026-04-06 12:41:49 +01:00
"""
Application root file : Inits the FastAPI application
"""
2026-06-08 15:31:37 +01:00
2026-04-06 12:41:49 +01:00
from contextlib import asynccontextmanager
from typing import AsyncGenerator
from fastapi import FastAPI
from starlette . middleware . sessions import SessionMiddleware
from starlette . middleware . cors import CORSMiddleware
2026-05-29 11:50:16 +01:00
from src . constants import Environment
2026-04-06 12:41:49 +01:00
from src . config import settings
from src . api import api_router
from src . auth . config import auth_settings
2026-05-26 11:42:49 +01:00
from src . auth . service import get_current_user , get_dev_user
2026-04-06 12:41:49 +01:00
@asynccontextmanager
async def lifespan ( _application : FastAPI ) - > AsyncGenerator :
# Startup
yield
# Shutdown
if settings . ENVIRONMENT . is_deployed :
2026-05-26 11:42:49 +01:00
# Just a precaution, should be False anyway
settings . DISABLE_AUTH = False
2026-04-06 12:41:49 +01:00
2026-05-20 15:23:40 +01:00
tags_metadata = [
{
" name " : " User " ,
" description " : " User related operations, includes getting information about the current user " ,
2026-05-28 15:25:29 +01:00
} ,
2026-06-11 13:07:48 +01:00
{
" name " : " Organisation " ,
" description " : " Organisation related operations, includes getting lists of users etc associated with orgs " ,
} ,
2026-05-28 15:25:29 +01:00
{
" name " : " Service " ,
" description " : " Services related operations, includes registering services and reissuing API keys " ,
} ,
2026-05-28 16:46:44 +01:00
{
2026-06-11 13:07:48 +01:00
" name " : " IAM " ,
" description " : " Operations related to the role based identity and access management system. This includes management of groups, permissions, and related users. " ,
2026-05-28 16:46:44 +01:00
} ,
2026-05-20 15:23:40 +01:00
]
2026-04-06 12:41:49 +01:00
app = FastAPI (
swagger_ui_init_oauth = {
" clientId " : auth_settings . CLIENT_ID ,
" usePkceWithAuthorizationCodeGrant " : True ,
" scopes " : " openid profile email " ,
2026-05-20 15:23:40 +01:00
} ,
openapi_tags = tags_metadata ,
2026-04-06 12:41:49 +01:00
)
2026-05-19 12:10:06 +01:00
# Type inspection disabled for middleware injection.
# Known bug in FastAPI type checking: https://github.com/astral-sh/ty/issues/1635
# noinspection PyTypeChecker
2026-04-06 12:41:49 +01:00
app . add_middleware ( SessionMiddleware , secret_key = settings . SECRET_KEY . get_secret_value ( ) )
2026-05-19 12:10:06 +01:00
# noinspection PyTypeChecker
2026-04-06 12:41:49 +01:00
app . add_middleware (
CORSMiddleware ,
allow_origins = settings . CORS_ORIGINS ,
allow_origin_regex = settings . CORS_ORIGINS_REGEX ,
allow_credentials = True ,
allow_methods = ( " GET " , " POST " , " PUT " , " PATCH " , " DELETE " , " OPTIONS " ) ,
allow_headers = settings . CORS_HEADERS ,
)
2026-05-29 15:18:10 +01:00
if settings . DISABLE_AUTH and ( settings . ENVIRONMENT == Environment . LOCAL ) :
2026-05-26 11:42:49 +01:00
app . dependency_overrides [ get_current_user ] = get_dev_user
2026-04-06 12:41:49 +01:00
app . include_router ( api_router )