design-look-a-likes / server.py
koenverhagen's picture
wip
ffc41ed
raw
history blame contribute delete
No virus
1.99 kB
import asyncio
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI, Response, Request, BackgroundTasks
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.get("/active")
async def active(request: Request, response: Response):
authorization_token = request.headers.get("Authorization", None)
check_token = check_auth(authorization_token)
return {"application":"active"}
@app.post("/lal_for_shop")
async def submit_async_task(request: Request, response: Response,background_tasks: BackgroundTasks, 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))
background_tasks.add_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