design-look-a-likes / server.py
koenverhagen's picture
wip
28bbb93
raw
history blame
1.63 kB
import asyncio
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI, Response, Request, Header, Form, UploadFile, Body, BackgroundTasks
from starlette.middleware.cors import CORSMiddleware
# import json
# from load_data import get_design_data, get_design_resolutions_for_shop
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 = '34dsadfF$$%#$TGREGEFGE%Q*)(*&%'
executor = ThreadPoolExecutor()
# ---------------------------------------------------------------------------
# - 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?"}
@app.post("/lal_for_shop")
async def submit_async_task(background_tasks: BackgroundTasks, request: Request, response: Response, shop: Shop):
loop = asyncio.new_event_loop()
authorization_token = request.headers.get("Authorization", None)
check_token = True # 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:
response.status_code = 401
return response