File size: 622 Bytes
daaa6ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""

Auto-discovery of API routers.



Open/Closed Principle: adding a new feature = adding a new file in this package.

No need to modify app.py — routers are discovered automatically.

"""

import importlib
import pkgutil

from fastapi import APIRouter


def discover_routers() -> list[APIRouter]:
    """Scan this package for modules exposing a `router` attribute."""
    routers = []
    for _, modname, _ in pkgutil.iter_modules(__path__):
        mod = importlib.import_module(f".{modname}", __package__)
        if hasattr(mod, "router"):
            routers.append(mod.router)
    return routers