34 lines
858 B
Python
34 lines
858 B
Python
from fastapi import FastAPI, Depends, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import sqlite3
|
|
from recipe import parse_recipe
|
|
import product, db
|
|
|
|
app = FastAPI()
|
|
|
|
# Add CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"]
|
|
)
|
|
|
|
# Dependency to create SQLite connection
|
|
async def get_db():
|
|
sql_db = await db.connect()
|
|
try:
|
|
yield sql_db
|
|
finally:
|
|
sql_db.close()
|
|
|
|
@app.get("/recipes/parse")
|
|
async def parse_recipe_handler(url: str, conn: sqlite3.Connection = Depends(get_db)):
|
|
return await parse_recipe(conn, url)
|
|
|
|
@app.post("/product/")
|
|
async def create_product(url: str, conn: sqlite3.Connection = Depends(get_db)):
|
|
p = await product.create_product(url)
|
|
db.insert_product(conn, p)
|
|
return p
|