From 88423145697f30754261f2e8eae49748e5354fcc Mon Sep 17 00:00:00 2001 From: agabrielcorujo Date: Tue, 3 Feb 2026 16:55:21 -0500 Subject: [PATCH] changed to modular routing instead of having everything in app.py --- backend/src/app.py | 21 +++++---------------- backend/src/business_logic.py | 2 ++ backend/src/business_router.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 backend/src/business_logic.py diff --git a/backend/src/app.py b/backend/src/app.py index 747ceb3..37de515 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -1,9 +1,8 @@ -from fastapi import Depends,FastAPI -from fastapi.security import OAuth2PasswordBearer -from jwt_auth.db import safe_query +from fastapi import FastAPI from jwt_auth.auth_router import router as auth_router -from jwt_auth.jwt_auth import decode_access_token +from business_router import router as business_router from fastapi.middleware.cors import CORSMiddleware + oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login") app = FastAPI() @@ -20,22 +19,12 @@ allow_headers=["*"], # Authorization, Content-Type ) -#mount auth endpoints (login, logout,register,refresh) +#mount routers app.include_router(auth_router) +app.include_router(business_router) -@app.get("example/endpoint") - -def example_endpoint(token: str = Depends(oauth2_scheme)): - """ - how endpoints should be defined. - access token is extracted from cookies using Depends. - access token is validated using auth package. - if token is invalid, exception will be raised. - """ - user_id = decode_access_token(token) - return f"some action based on {user_id}" diff --git a/backend/src/business_logic.py b/backend/src/business_logic.py new file mode 100644 index 0000000..0c4c45d --- /dev/null +++ b/backend/src/business_logic.py @@ -0,0 +1,2 @@ +from jwt_auth.db import safe_query + diff --git a/backend/src/business_router.py b/backend/src/business_router.py index e69de29..b0b6545 100644 --- a/backend/src/business_router.py +++ b/backend/src/business_router.py @@ -0,0 +1,18 @@ +from fastapi import Depends,FastAPI,APIRouter, HTTPException +from fastapi.security import OAuth2PasswordBearer +from jwt_auth.jwt_auth import decode_access_token +import business_logic + +router = APIRouter() + +oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login") + +@router.get("/sample-endpoint") +def sample_endpoint(token:str = Depends(oauth2_scheme)): + + user_id = decode_access_token(token) + + #do something by calling functions in business_logic.py + + pass +