toddler_shop_api/src/main.py

38 lines
1 KiB
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
2024-08-04 17:23:56 +02:00
from fastapi.responses import FileResponse
2024-08-04 10:06:07 +02:00
from src.classes import product_classes
2024-08-04 17:23:56 +02:00
from src.config import definitions
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):
2024-08-04 17:23:56 +02:00
return data_connection.get_single_product(barcode)
@app.get("/icons/{icon_filename}", response_class=FileResponse)
def get_icon(icon_filename: str):
full_filepath = f"{definitions.ICONS_PATH}/{icon_filename}"
return full_filepath