refactor(#14): FastAPI backend 라우터, 서비스, DB 모듈 분리#22
Merged
Conversation
- DB connection pool 및 공통 connection helper를 database.py로 분리 - YOLO 이미지 디코딩과 추론 로직을 inference_service.py로 분리 - 통계 조회 및 추론 결과 저장 로직을 stats_service.py로 분리 - public API와 device API를 각각 전용 router 모듈로 분리 - main.py를 FastAPI 앱 초기화와 라우터 등록 중심으로 정리 - 기존 API 경로와 응답 구조 유지
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🍀 이슈 번호
📝 변경 사항
database.py로 분리services/stats_service.py로 분리services/stats_service.py로 분리services/inference_service.py로 분리routers/public.py로 분리GET /api/v2/count/currentGET /api/v2/stats/todayGET /api/v2/stats/weeklyrouters/device.py로 분리POST /api/v2/device/uploadGET /api/v2/device/commandPOST /api/v2/test/triggermain.py를 FastAPI 앱 초기화, middleware 설정, 정적 파일 제공, 라우터 등록 중심으로 정리🎯 목적
기존 backend의 FastAPI 앱 설정, DB 접근, YOLO 추론, 통계 처리, API 라우팅 로직이
main.py에 집중되어 있어 기능별 책임 구분이 어려웠습니다.향후 public API와 device API를 독립적으로 관리하고 배포 환경별 설정을 분리할 수 있도록 backend 구조를 라우터, 서비스, DB 모듈 단위로 정리했습니다.
이번 변경은 기능 추가가 아닌 구조 리팩토링이며, 기존 프론트엔드와 ESP32-CAM이 사용하는 API 경로와 응답 형식은 변경하지 않았습니다.
📂 적용 범위
backend/main.pybackend/database.pybackend/routers/__init__.pybackend/routers/public.pybackend/routers/device.pybackend/services/__init__.pybackend/services/inference_service.pybackend/services/stats_service.py🖥️ 주요 코드 설명
FastAPI 라우터 등록
main.py에서는 각 API를 직접 정의하지 않고 기능별 라우터를 등록하도록 변경했습니다.Public API 분리
프론트엔드가 사용하는 현재 인원 및 통계 API를
routers/public.py에서 관리합니다.Device API 분리
ESP32-CAM의 명령 조회와 이미지 업로드 API를
routers/device.py에서 관리합니다.서비스 계층 분리
라우터에서는 요청과 응답 처리를 담당하고, 이미지 디코딩과 YOLO 추론은
inference_service.py에 위임합니다.DB 조회와 추론 결과 저장 및 통계 갱신은
stats_service.py에서 담당합니다.📋 체크리스트
📌 참고 사항