fastapi-maintenance

Flexible maintenance mode middleware for FastAPI applications.

https://github.com/msamsami/fastapi-maintenance

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.4%) to scientific vocabulary

Keywords

503-error fastapi maintenance offline unavailable
Last synced: 6 months ago · JSON representation ·

Repository

Flexible maintenance mode middleware for FastAPI applications.

Basic Info
Statistics
  • Stars: 56
  • Watchers: 1
  • Forks: 3
  • Open Issues: 1
  • Releases: 5
Topics
503-error fastapi maintenance offline unavailable
Created 10 months ago · Last pushed 7 months ago
Metadata Files
Readme Contributing License Citation Security

README.md

FastAPI Maintenance

Flexible maintenance mode middleware for FastAPI applications.

Package version Supported Python versions Test Coverage License


Documentation: https://msamsami.github.io/fastapi-maintenance

Source Code: https://github.com/msamsami/fastapi-maintenance


FastAPI Maintenance is a lightweight middleware for FastAPI applications that provides a flexible way to handle maintenance mode.

The package provides a simple yet powerful solution to temporarily disable your API endpoints during application maintenance states. It ensures a smooth experience for API consumers through customizable responses and fine-grained control over which routes remain accessible. The package is designed to be easy to integrate, highly customizable, and extensible to fit various use cases.

[!WARNING] FastAPI Maintenance is currently in experimental status. Although it's actively developed and tested, the API may undergo changes between releases. Be cautious when upgrading in production environments and always review the changelog carefully.

Features

  • ⚙️ Simple to use: Add just a few lines of code to enable maintenance mode.
  • 🔌 Flexible state management: Manage maintenance mode via environment variables, local files, or create your own custom backend.
  • 🚦 Per-route control: Force maintenance mode ON/OFF for specific routes.
  • 🎨 Customizable responses: Define your own maintenance page or custom JSON responses.
  • ⏱️ Context manager: Temporarily enable maintenance mode for critical operations.
  • 🧩 Extensible: Easy to extend with custom backends, handlers, and exemptions.

Install

bash pip install fastapi-maintenance

Quick Start

Middleware

Add the maintenance mode middleware to your FastAPI application:

```python from fastapi import FastAPI from fastapi_maintenance import MaintenanceModeMiddleware

app = FastAPI()

Add the middleware to your FastAPI application

app.add_middleware(MaintenanceModeMiddleware)

@app.get("/") def root(): return {"message": "Hello World"} ```

When maintenance mode is not active, endpoints function normally.

Enabling Maintenance Mode

You can enable maintenance mode in several ways:

1. Direct Configuration

Explicitly enable maintenance mode when adding the middleware: python app.add_middleware(MaintenanceModeMiddleware, enable_maintenance=True)

2. Environment Variables

Set the FASTAPI_MAINTENANCE_MODE environment variable before starting your application: bash export FASTAPI_MAINTENANCE_MODE=1 uvicorn main:app

Maintenance Response

When maintenance mode is active, all endpoints (unless explicitly exempted) will return a 503 Service Unavailable response: json {"detail":"Service temporarily unavailable due to maintenance"}

Decorators

You can control maintenance mode behavior for specific routes using decorators:

```python from fastapi import FastAPI from fastapimaintenance import ( MaintenanceModeMiddleware, forcemaintenancemodeoff, forcemaintenancemode_on, )

app = FastAPI() app.add_middleware(MaintenanceModeMiddleware)

Always accessible, even during maintenance

@app.get("/status") @forcemaintenancemode_off def status(): return {"status": "operational"}

Always returns maintenance response

@app.get("/deprecated") @forcemaintenancemodeon async def deprecatedendpoint(): return {"message": "This endpoint is deprecated"} ```

The force_maintenance_mode_off decorator keeps an endpoint accessible even when maintenance mode is enabled globally. Conversely, the force_maintenance_mode_on decorator forces an endpoint to always return the maintenance response, regardless of the global maintenance state.

Context Manager

You can use context managers to temporarily enforce the maintenance state for specific operations:

```python from fastapi import FastAPI from fastapimaintenance import MaintenanceModeMiddleware, maintenancemode_on

app = FastAPI() app.add_middleware(MaintenanceModeMiddleware)

@app.post("/sync") async def syncdata(): # Enable maintenance mode during data sync async with maintenancemodeon(): # Data sync logic here await performsync()

# Maintenance mode is automatically disabled after the block
return {"status": "completed"}

@app.get("/health") def health_check(): return {"status": "healthy"} ```

The maintenance_mode_on context manager temporarily enables maintenance mode for critical operations.

License

This project is licensed under the terms of the MIT license.

Owner

  • Name: Mehdi Samsami
  • Login: msamsami
  • Kind: user
  • Company: Data Scientist

BSc & MSc in ECE

Citation (CITATION.cff)

# This CITATION.cff file was generated with cffinit.
# Visit https://bit.ly/cffinit to generate yours today!

cff-version: 1.2.0
title: FastAPI-Maintenance
message: >-
  If you use this software, please cite it using the
  metadata from this file.
type: software
authors:
  - given-names: Mehdi
    family-names: Samsami
    email: mehdisamsami@live.com
repository-code: 'https://github.com/msamsami/fastapi-maintenance'
abstract: >-
  Flexible maintenance mode middleware for FastAPI
  applications.
keywords:
  - fastapi
  - maintenance
  - offline
  - unavailable
  - '503'
license: MIT

GitHub Events

Total
  • Create event: 17
  • Issues event: 1
  • Release event: 3
  • Watch event: 48
  • Delete event: 12
  • Issue comment event: 11
  • Push event: 55
  • Public event: 1
  • Pull request review event: 1
  • Pull request event: 31
  • Fork event: 3
Last Year
  • Create event: 17
  • Issues event: 1
  • Release event: 3
  • Watch event: 48
  • Delete event: 12
  • Issue comment event: 11
  • Push event: 55
  • Public event: 1
  • Pull request review event: 1
  • Pull request event: 31
  • Fork event: 3

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 62
  • Total Committers: 1
  • Avg Commits per committer: 62.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 62
  • Committers: 1
  • Avg Commits per committer: 62.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Mehdi Samsami m****i@l****m 62

Issues and Pull Requests

Last synced: 6 months ago

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 191 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 5
  • Total maintainers: 1
pypi.org: fastapi-maintenance

Flexible maintenance mode middleware for FastAPI applications.

  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 191 Last month
Rankings
Dependent packages count: 9.1%
Forks count: 31.4%
Average: 33.4%
Stargazers count: 41.4%
Dependent repos count: 51.4%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/ci.yml actions
  • actions/checkout v4 composite
  • actions/download-artifact v4 composite
  • actions/setup-python v5 composite
  • actions/upload-artifact v4 composite
  • astral-sh/setup-uv v6 composite
pyproject.toml pypi
  • fastapi >=0.99.0
  • typing-extensions >=4.8.0 ; python_full_version < '3.10'
uv.lock pypi
  • anyio 4.5.2
  • babel 2.17.0
  • backrefs 5.7.post1
  • certifi 2025.4.26
  • cfgv 3.4.0
  • charset-normalizer 3.4.2
  • click 8.1.8
  • colorama 0.4.6
  • coverage 7.6.1
  • distlib 0.3.9
  • exceptiongroup 1.2.2
  • fastapi 0.99.0
  • fastapi-maintenance 0.1.0
  • filelock 3.16.1
  • ghp-import 2.1.0
  • h11 0.16.0
  • httpcore 1.0.9
  • httptools 0.6.4
  • httpx 0.28.1
  • identify 2.6.1
  • idna 3.10
  • importlib-metadata 8.5.0
  • iniconfig 2.1.0
  • isort 5.13.2
  • jinja2 3.1.6
  • markdown 3.7
  • markupsafe 2.1.5
  • mergedeep 1.3.4
  • mkdocs 1.6.1
  • mkdocs-get-deps 0.2.0
  • mkdocs-material 9.6.13
  • mkdocs-material-extensions 1.3.1
  • mypy 1.14.1
  • mypy-extensions 1.1.0
  • nodeenv 1.9.1
  • packaging 25.0
  • paginate 0.5.7
  • pathspec 0.12.1
  • platformdirs 4.3.6
  • pluggy 1.5.0
  • pre-commit 3.5.0
  • pydantic 1.10.22
  • pygments 2.19.1
  • pymdown-extensions 10.15
  • pytest 8.3.5
  • pytest-anyio 0.0.0
  • pytest-cov 5.0.0
  • python-dateutil 2.9.0.post0
  • python-dotenv 1.0.1
  • pytz 2025.2
  • pyyaml 6.0.2
  • pyyaml-env-tag 0.1
  • requests 2.32.3
  • ruff 0.11.8
  • six 1.17.0
  • sniffio 1.3.1
  • starlette 0.27.0
  • tomli 2.2.1
  • typing-extensions 4.13.2
  • urllib3 2.2.3
  • uvicorn 0.33.0
  • uvloop 0.21.0
  • virtualenv 20.31.2
  • watchdog 4.0.2
  • watchfiles 0.24.0
  • websockets 13.1
  • zipp 3.20.2