From 1acf044461b6bef48a8e4e8815422e0ef1eb25cc Mon Sep 17 00:00:00 2001 From: suk kyun hong <38815540+sukkyun2@users.noreply.github.com> Date: Fri, 5 Jul 2024 15:17:21 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20CORS=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * doc: README.md workflow badge 추가 * fix: cors 에러 해결 - localhost와 remote server 추가 - 추후 dotenv multi value 깔끔하게 해결 필요 * fix: cors 에러 해결시 포트 번호 추가 - localhost -> localhost:5173 --- .env | 3 ++- README.md | 1 + app/config.py | 5 +++-- app/main.py | 10 ++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.env b/.env index cbd8dbf..dbe797f 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ HISTORY_API_HOST=http://3.34.196.131:8080 -YOLO_WEIGHT_PATH=yolov5su.pt \ No newline at end of file +YOLO_WEIGHT_PATH=yolov5su.pt +ALLOW_ORIGINS=http://localhost:5173;http://13.124.103.220 \ No newline at end of file diff --git a/README.md b/README.md index 00063c0..4dbe9a9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # APAP-AI +[![Deploy with Docker](https://github.com/sukkyun2/APAP-ai/actions/workflows/deploy.yml/badge.svg)](https://github.com/sukkyun2/APAP-ai/actions/workflows/deploy.yml) This is a repository for an object detection inference API using `YOLOv5` and `FastAPI` diff --git a/app/config.py b/app/config.py index 1f1474d..1e70f1a 100644 --- a/app/config.py +++ b/app/config.py @@ -3,8 +3,9 @@ class Settings(BaseSettings): - history_api: str = Field(alias='HISTORY_API_HOST', default='http://127.0.0.1:8080') - yolo_weight_path: str = Field(default='yolov5su.pt') + history_api: str = Field(alias='HISTORY_API_HOST', default='http://localhost:8080') + yolo_weight_path: str = Field(alias='YOLO_WEIGHT_PATH', default='yolov5su.pt') + allow_origins: str = Field(alias='ALLOW_ORIGINS', default='') # TODO Convert to List class Config: env_file = '.env' diff --git a/app/main.py b/app/main.py index 77bb126..4e8d3dc 100644 --- a/app/main.py +++ b/app/main.py @@ -4,14 +4,24 @@ from PIL import Image from fastapi import FastAPI from fastapi import UploadFile, File +from fastapi.middleware.cors import CORSMiddleware from app.api_response import ApiListResponse +from app.config import settings from app.detection import Detection from app.history import HistorySaveRequest, save_history from model.detect import detect app = FastAPI() +app.add_middleware( + CORSMiddleware, + allow_origins=settings.allow_origins.split(';'), + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + def convert(result: dict): return Detection(**result)