toddler_shop_api/src/main.py

31 lines
778 B
Python
Raw Normal View History

2024-08-04 08:43:04 +02:00
from fastapi import FastAPI
2024-08-04 10:06:07 +02:00
from fastapi.middleware.cors import CORSMiddleware
from src.classes import product_classes
from src import data_connection
2024-08-04 08:43:04 +02:00
app = FastAPI()
2024-08-04 10:06:07 +02:00
origins = [
2024-08-04 10:13:59 +02:00
"http://127.0.0.1:8080" #Will become something like 'shop.yarnecoppens.com'
2024-08-04 10:06:07 +02:00
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
2024-08-04 10:13:59 +02:00
allow_credentials=True,
2024-08-04 10:06:07 +02:00
allow_methods=["*"],
allow_headers=["*"],
)
2024-08-04 08:43:04 +02:00
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/products", response_model=list[product_classes.Product])
2024-08-04 08:47:49 +02:00
def get_all_products():
return data_connection.get_all_products()
2024-08-04 08:47:49 +02:00
2024-08-04 10:23:58 +02:00
@app.get("/products/{barcode}", response_model=product_classes.Product)
def get_single_product(barcode: int):
return data_connection.get_single_product(barcode)