Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ services:
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
ports:
- "8000:8000"
depends_on:
- post-observer-service
env_file:
- .env
# frontend:
Expand All @@ -46,12 +48,12 @@ services:
# env_file:
# - .env

post-observer:
post-observer-service:
build: ./post_observer
container_name: jandi-observer
command: python3 -u main.py
depends_on:
- ai-server
container_name: jandi-post-observer-service
command: uvicorn server:app --host 0.0.0.0 --port 8001
ports:
- "8001:8001"
env_file:
- .env

Expand Down
65 changes: 64 additions & 1 deletion main_server/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi import FastAPI, HTTPException, Request
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from sqlalchemy import text

from app.routers.auth_router import router as auth_router
Expand All @@ -11,6 +13,7 @@
from app.routers.user_router import router as user_router
from app.routers.ui import router as ui_router
from app.routers.trend_router import router as trend_router
from app.routers.analytics_router import router as analytics_router

import app.models

Expand Down Expand Up @@ -47,6 +50,66 @@ async def lifespan(app: FastAPI):
app.include_router(router=user_router)
app.include_router(router=ui_router)
app.include_router(router=trend_router)
app.include_router(router=analytics_router)

def _is_analytics_path(request: Request) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요 함수의 역할은 뭔가요

"""
analytics API 경로 여부를 반환합니다.

:param request: FastAPI 요청 객체.
:return: analytics 경로면 True.
"""
return request.url.path.startswith("/api/analytics")


@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
"""
HTTPException 응답 형식을 처리합니다.

:param request: FastAPI 요청 객체.
:param exc: 발생한 HTTPException.
:return: JSON 응답.
"""
if _is_analytics_path(request):
detail = exc.detail
if isinstance(detail, str):
message = detail
elif isinstance(detail, dict) and "message" in detail:
message = str(detail["message"])
else:
message = "Request failed"
return JSONResponse(
status_code=exc.status_code,
content={"message": message},
headers=exc.headers,
)
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail},
headers=exc.headers,
)


@app.exception_handler(RequestValidationError)
async def request_validation_exception_handler(
request: Request,
exc: RequestValidationError,
) -> JSONResponse:
"""
요청 검증 오류 응답 형식을 처리합니다.

:param request: FastAPI 요청 객체.
:param exc: 발생한 RequestValidationError.
:return: JSON 응답.
"""
if _is_analytics_path(request):
return JSONResponse(
status_code=400,
content={"message": "Invalid request parameters"},
)
return JSONResponse(status_code=422, content={"detail": exc.errors()})


@app.get("/")
async def root():
Expand Down
Loading