design-look-a-likes / server.py
koenverhagen's picture
Update server.py
9087412
raw
history blame
1.65 kB
import asyncio
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI, Response, Request
from starlette.middleware.cors import CORSMiddleware
# import json
# from load_data import get_design_data, get_design_resolutions_for_shop
from starlette.responses import RedirectResponse
from createlookalike import create_feature_files
from schemas import Shop
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
API_TOKEN = '34dsadfFTGREGEFGE'
# ---------------------------------------------------------------------------
# - Token auth method -
# ---------------------------------------------------------------------------
def check_auth(authorization_header):
if authorization_header == API_TOKEN:
return True
else:
return False
@app.get("/")
async def root():
# return {"message": "where are you looking for?"}
response = RedirectResponse(url='/docs')
return response
@app.post("/lal_for_shop")
async def submit_async_task(request: Request, response: Response, shop: Shop):
loop = asyncio.new_event_loop()
authorization_token = request.headers.get("Authorization", None)
check_token = check_auth(authorization_token)
if check_token:
task = asyncio.create_task(create_feature_files(shop))
return {"message": f"shop calculation for shop {shop.id} submitted"}
else:
print(authorization_token)
response.status_code = 401
return response