semvec/pypi-packages huggingface dataset:
fastapi-auto-router 0.2.2
Automatic router configuration for FastAPI based on filesystem structure
License: MIT
Requires Python: >=3.7
Keywords: automatic, configuration, fastapi, router

# FastAPI Auto Router

Automatic router configuration for FastAPI based on filesystem structure.

## Installation

```bash
pip install fastapi-auto-router
```

## Usage

1. Create your router files in a directory structure that matches your desired API paths:

```
your_project/
└── routers/
    └── user_management
        ├── users.py
        └── {user_id}/
                └── profile.py
```

2. In your router files, create FastAPI routers:

routers/user_management/users.py

```python
from fastapi import APIRouter

router = APIRouter()


@router.get("")
async def list_users():
    return {"message": "List users"}
```

3. In your main FastAPI application, use AutoRouter:

```python
from fastapi import FastAPI
from fastapi_auto_router import AutoRouter

app = FastAPI()
# Initialize and load routers
auto_router = AutoRouter(
    app=app,
    routers_dir="routers",  # Path to your routers directory
    api_prefix="/api/v1",  # Optional: prefix for all routes
    base_path=None  # Optional: base path for resolving routers_dir (default: current working directory)
)
auto_router.load_routers()
```

This will create the following routes:

- `/api/v1/user-management/users`
- `/api/v1/user-management/{user_id}/profile`

## Features

- Automatic route configuration based on filesystem structure
- Converts underscores to hyphens in route paths
- Supports path parameters using {parameter_name} folders
- Customizable API prefix
- Flexible path resolution with `base_path` parameter
- Works with any FastAPI application

## Configuration Options

### base_path

The `base_path` parameter allows you to specify the directory where `routers_dir` is located. This is useful when your router files are not in the current working directory.

**Behavior:**
- If `routers_dir` is a relative path: the final path will be `base_path/routers_dir`
- If `routers_dir` is an absolute path: `base_path` is ignored and `routers_dir` is used as-is

**Example:**

```python
from fastapi import FastAPI
from fastapi_auto_router import AutoRouter
from pathlib import Path

app = FastAPI()

# Use base_path to specify where routers directory is located
auto_router = AutoRouter(
    app=app,
    routers_dir="routers",
    api_prefix="/api/v1",
    base_path=str(Path(__file__).parent)  # Resolve routers relative to current file
)
auto_router.load_routers()
```

## License

MIT License
dipdup 8.5.1
Modular framework for creating selective indexers and featureful backends for dapps
License: MIT
Requires Python: <3.13,>=3.12
Keywords: api, backend, blockchain, crypto, cryptocurrencies, dapp, ethereum, evm, framework, indexer, indexers, starknet, sdk, smart-contracts, tezos, web3

[![Twitter](https://badgen.net/badge/icon/dipdup_io?icon=twitter&label=)](https://twitter.com/dipdup_io)
[![Monthly downloads](https://static.pepy.tech/badge/dipdup/month)](https://pepy.tech/project/dipdup)
[![GitHub stars](https://img.shields.io/github/stars/dipdup-io/dipdup?color=2c2c2c&style=plain)](https://github.com/dipdup-io/dipdup)
[![Python Version](https://img.shields.io/pypi/pyversions/dipdup?color=2c2c2c)](https://www.python.org)
[![License: MIT](https://img.shields.io/github/license/dipdup-io/dipdup?color=2c2c2c)](https://github.com/dipdup-io/dipdup/blob/next/LICENSE)
<br>
[![Latest stable release](https://img.shields.io/github/v/release/dipdup-io/dipdup?label=stable%20release&color=2c2c2c)](https://github.com/dipdup-io/dipdup/releases)
[![Latest pre-release](https://img.shields.io/github/v/release/dipdup-io/dipdup?include_prereleases&label=latest%20release&color=2c2c2c)](https://github.com/dipdup-io/dipdup/releases)
[![GitHub issues](https://img.shields.io/github/issues/dipdup-io/dipdup?color=2c2c2c)](https://github.com/dipdup-io/dipdup/issues)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/dipdup-io/dipdup?color=2c2c2c)](https://github.com/dipdup-io/dipdup/pulls)

<h3 align="center"><img src="https://actual-docs.interface-1bp.pages.dev/_nuxt/logo.9b02ab5e.svg" alt="DipDup logo"></h3>

DipDup is a Python framework for building smart contract indexers. It helps developers focus on business logic instead of writing a boilerplate to store and serve data. DipDup-based indexers are selective, which means only required data is requested. This approach allows to achieve faster indexing times and decreased load on underlying APIs.

- **Ready to build your first indexer?** Head to [Quickstart](https://dipdup.io/docs/quickstart).

- **Looking for examples?** Check out [Demo Projects](https://dipdup.io/docs/examples/demos) and [Built with DipDup](https://dipdup.io/docs/examples/built-with-dipdup) pages.

- **Want to participate?** Vote for [open issues](https://github.com/dipdup-io/dipdup/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc), join [discussions](https://github.com/dipdup-io/dipdup/discussions) or [become a sponsor](https://github.com/sponsors/dipdup-io).

- **Have a question?** Join our [Discord](https://discord.gg/aG8XKuwsQd) or tag @dipdup_io on [Twitter](https://twitter.com/dipdup_io).

This project is maintained by the [Baking Bad](https://bakingbad.dev/) team.
<br>
Development is supported by [Tezos Foundation](https://tezos.foundation/) and [OnlyDust](https://www.onlydust.xyz).