Skip to content

Commit

Permalink
Merge pull request #1877 from zauberzeug/run-with-lifespan
Browse files Browse the repository at this point in the history
Add startup and shutdown handlers to existing FastAPI app
  • Loading branch information
rodja authored Oct 26, 2023
2 parents 65cf7fd + 21d3bb3 commit 4a00988
Showing 1 changed file with 12 additions and 0 deletions.
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

0 comments on commit 4a00988

Please sign in to comment.