api: use proper connection shutdown for downstream services

This commit is contained in:
lilly 2026-05-19 13:09:00 +02:00
commit 44d484cfc1
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
3 changed files with 94 additions and 22 deletions

View file

@ -2,7 +2,6 @@ from typing import Optional, List
import logging
import secrets
import sys
import os
from datetime import datetime, UTC
from fastapi import FastAPI, Request, Response, status
from fastapi.responses import RedirectResponse
@ -42,11 +41,13 @@ async def lifespan(app: FastAPI):
auth=BasicAuth(app_cfg.ccujack_user, app_cfg.ccujack_password),
mqtt_conn=app_cfg.ccujack_mqtt,
)
await app.extra["ccujack"].find_locks()
await app.extra["ccujack"].connect_mqtt()
await app.extra["ccujack"].find_locks()
yield
await app.extra["ccujack"].close_connections()
app = FastAPI(
title="Dooris",
@ -71,9 +72,7 @@ async def get_user_info(
) -> models.UserStatus:
return models.UserStatus(
is_authorized=current_user.may_operate_locks,
guaranteed_session_until=datetime.fromtimestamp(
current_user.id_token.exp, UTC
),
guaranteed_session_until=datetime.fromtimestamp(current_user.id_token.exp, UTC),
username=current_user.id_token.preferred_username,
ccchh_roles=current_user.ccchh_roles,
)
@ -122,7 +121,9 @@ async def login_init(
response_class=RedirectResponse,
status_code=302,
)
async def login_callback(req: Request, resp: Response, oidc_client: deps.OpenidClient) -> str:
async def login_callback(
req: Request, resp: Response, oidc_client: deps.OpenidClient
) -> str:
# check that the user is currently in an authenticating state
# these cookies are set by the login_init() view
if (