dough-calc/main.py

69 lines
1.9 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from bread import BreadRecipe
from config import Settings
app = FastAPI(
title="Dough Calculator API",
description="""Calculate bread ingredient proportions
based on baker's percentages""",
version="1.0.0",
root_path="/api"
)
settings = Settings()
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/calculate", summary="Calculate bread ingredients")
def calculate_bread_ingredients(recipe: BreadRecipe):
"""
Calculate the exact quantities of ingredients needed for bread.
**Inputs (what the user provides):**
- total_flour: Total flour weight desired (grams)
- hydration: Total hydration percentage
- sourdough_percentage: Sourdough as % of total flour
- sourdough_ratio: Flour:water ratio in the starter
- salt: Salt as % of total flour
**Outputs (what to use):**
- flour_to_add: Flour to add (excluding flour in sourdough)
- water_to_add: Water to add (excluding water in sourdough)
- sourdough_weight: Total sourdough needed
- salt_weight: Salt needed
"""
return {
"ingredients_to_add": {
"flour": recipe.flour_to_add,
"water": recipe.water_to_add,
"sourdough": recipe.sourdough_weight,
"salt": recipe.salt_weight
},
"details": {
"total_flour": recipe.total_flour,
"total_water": recipe.total_water,
"flour_in_sourdough": recipe.flour_in_sourdough,
"water_in_sourdough": recipe.water_in_sourdough
}
}
@app.get("/", summary="API information")
def root():
"""Root endpoint with API information."""
return {
"message": "Dough Calculator API",
"docs": "/docs",
"version": "1.0.0"
}