import os from typing import Annotated import django from django.conf import settings as django_settings from fastapi import Cookie, Depends, FastAPI, Header from fastapi.responses import HTMLResponse, RedirectResponse from sqlalchemy.orm import Session from starlette.requests import Request from redirect.db.curd import LinkNotFoundException, get_link_by_slug from redirect.db.dependency import get_db from redirect.settings import settings os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local") django.setup() app = FastAPI() from akarpov.tools.shortener.tasks import ( # noqa: This has to be imported strictly AFTER django setup save_view_meta, ) settings.relative_base = django_settings.SHORTENER_REDIRECT_TO @app.exception_handler(LinkNotFoundException) async def unicorn_exception_handler(request: Request, exc: LinkNotFoundException): return HTMLResponse( status_code=404, # language=HTML content="""