Refactor app.py to include user and product routers, update greeting message, and clean up commented code.
Browse files- app.py +6 -9
- routers/__init__.py +4 -0
- routers/products.py +11 -0
- routers/users.py +11 -0
app.py
CHANGED
@@ -1,16 +1,13 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
# Run the server (use Uvicorn)
|
11 |
-
# Command: uvicorn app:app --reload
|
12 |
|
13 |
@app.get("/")
|
14 |
def read_root():
|
15 |
-
return {"message": "
|
16 |
-
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from routers import users, products # Import your routers
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
6 |
+
# Include routers
|
7 |
+
app.include_router(users.router, prefix="/users", tags=["Users"])
|
8 |
+
app.include_router(products.router, prefix="/products", tags=["Products"])
|
9 |
+
# app.include_router(orders.router, prefix="/orders", tags=["Orders"])
|
|
|
|
|
|
|
10 |
|
11 |
@app.get("/")
|
12 |
def read_root():
|
13 |
+
return {"message": "Welcome to the API"}
|
|
routers/__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .users import router as users_router
|
2 |
+
from .products import router as products_router
|
3 |
+
|
4 |
+
__all__ = ["users_router", "products_router"]
|
routers/products.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter
|
2 |
+
|
3 |
+
router = APIRouter()
|
4 |
+
|
5 |
+
@router.get("/products")
|
6 |
+
def get_products():
|
7 |
+
return {"message": "List of products"}
|
8 |
+
|
9 |
+
@router.post("/products")
|
10 |
+
def create_product():
|
11 |
+
return {"message": "Product created"}
|
routers/users.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter
|
2 |
+
|
3 |
+
router = APIRouter()
|
4 |
+
|
5 |
+
@router.get("/users")
|
6 |
+
def get_users():
|
7 |
+
return {"message": "List of users"}
|
8 |
+
|
9 |
+
@router.post("/users")
|
10 |
+
def create_user():
|
11 |
+
return {"message": "User created"}
|