Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add startup and shutdown handlers to existing FastAPI app #1877

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions nicegui/ui_run_with.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Optional, Union

from fastapi import FastAPI

from . import core, storage
from .language import Language
from .nicegui import _shutdown, _startup


def run_with(
Expand Down Expand Up @@ -52,3 +54,13 @@ def run_with(
storage.set_storage_secret(storage_secret)

app.mount(mount_path, core.app)
main_app_lifespan = app.router.lifespan_context

@asynccontextmanager
async def lifespan_wrapper(app):
_startup()
async with main_app_lifespan(app):
yield
_shutdown()

app.router.lifespan_context = lifespan_wrapper
Loading