-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathmain.py
45 lines (35 loc) · 1.16 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import logging
import pathlib
from fastapi import FastAPI, Form, HTTPException
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
logger = logging.getLogger("uvicorn")
logger.level = logging.INFO
images = pathlib.Path(__file__).parent.resolve() / "images"
origins = [os.environ.get("FRONT_URL", "http://localhost:3000")]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=False,
allow_methods=["GET", "POST", "PUT", "DELETE"],
allow_headers=["*"],
)
@app.get("/")
def root():
return {"message": "Hello, world!"}
@app.post("/items")
def add_item(name: str = Form(...)):
logger.info(f"Receive item: {name}")
return {"message": f"item received: {name}"}
@app.get("/image/{image_name}")
async def get_image(image_name):
# Create image path
image = images / image_name
if not image_name.endswith(".jpg"):
raise HTTPException(status_code=400, detail="Image path does not end with .jpg")
if not image.exists():
logger.debug(f"Image not found: {image}")
image = images / "default.jpg"
return FileResponse(image)