'FastAPI application won't start when importing routers
I'm trying to import some authentication related routers to my main class in a FastAPI project, but if I add this import, the application starts without giving any results:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api.v1.users import routers
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(routers.get_users_router())
This is what api.v1.users.routers looks like:
from fastapi import APIRouter
from auth import auth_backend, fastapi_users
users_router = APIRouter()
def get_users_router():
users_router.include_router(fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"])
users_router.include_router(fastapi_users.get_register_router(), prefix="auth", tags=["auth"])
users_router.include_router(fastapi_users.get_reset_password_router(), prefix="auth", tags=["auth"])
users_router.include_router(fastapi_users.get_verify_router(), prefix="auth", tags=["auth"])
users_router.include_router(fastapi_users.get_users_router(), prefix="users", tags=["users"])
I don't see any error. If I run the application it just won't load.
What am I doing wrong?? I would appreciate any help thanks!
Solution 1:[1]
This would seem more likely to work
from fastapi import APIRouter
from auth import auth_backend, fastapi_users
def get_users_router():
users_router = APIRouter()
users_router.include_router(fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"])
users_router.include_router(fastapi_users.get_register_router(), prefix="auth", tags=["auth"])
users_router.include_router(fastapi_users.get_reset_password_router(), prefix="auth", tags=["auth"])
users_router.include_router(fastapi_users.get_verify_router(), prefix="auth", tags=["auth"])
users_router.include_router(fastapi_users.get_users_router(), prefix="users", tags=["users"])
return users_router
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HuguesG |
