Skip to content

Add lifespan support to FastAPI app creation#30

Merged
HosseinNejatiJavaremi merged 3 commits intoSyntaxArc:masterfrom
Mohammadreza-kh94:feature/add-lifespan-support
Apr 15, 2025
Merged

Add lifespan support to FastAPI app creation#30
HosseinNejatiJavaremi merged 3 commits intoSyntaxArc:masterfrom
Mohammadreza-kh94:feature/add-lifespan-support

Conversation

@Mohammadreza-kh94
Copy link
Copy Markdown
Contributor

Description

This PR adds support for optional lifespan context managers when creating FastAPI applications through the create_fastapi_app utility function. The implementation allows developers to define setup and teardown operations that run at application startup and shutdown, making it easier to manage resources like database connections, external APIs, and background tasks throughout the application's lifecycle.

Fixes #27

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

  • Tested with a simple lifespan context manager that initializes and cleans up resources
  • Tested with database connection lifecycle management
  • Tested with background task initialization and cleanup
  • Tested without a lifespan parameter (backward compatibility)

Sample test code:

from contextlib import asynccontextmanager
from fastapi import FastAPI

@asynccontextmanager
async def test_lifespan(app: FastAPI):
    # Setup
    app.state.test_value = "initialized"
    yield
    # Cleanup
    app.state.test_value = "cleaned up"

# Test app creation with lifespan
app = AppUtils.create_fastapi_app(config, lifespan=test_lifespan)
assert app.state.test_value == "initialized"

# Test cleanup on shutdown
await app.shutdown()
assert app.state.test_value == "cleaned up"

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my feature works
  • New and existing unit tests pass locally with my changes

Additional context

Example usage with database connections:

@asynccontextmanager
async def db_lifespan(app: FastAPI):
    # Initialize database connection
    engine = create_async_engine(config.DB_URL)
    app.state.db = engine
    
    yield  # Application is running
    
    # Cleanup
    await engine.dispose()

app = AppUtils.create_fastapi_app(config, lifespan=db_lifespan)

Add optional lifespan parameter to create_fastapi_app function to support setup and teardown operations in the FastAPI application lifecycle. This enables proper resource management and initialization when creating FastAPI applications.
@HosseinNejatiJavaremi HosseinNejatiJavaremi merged commit 68d768b into SyntaxArc:master Apr 15, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add lifespan support to FastAPI app creation

2 participants