Skip to content

Commit

Permalink
Merge pull request #31 from edu-pi/feature/issue30
Browse files Browse the repository at this point in the history
[#30] analysis 서버에서 가져온 code를 까서 까져와 결과에 담아서 반환
  • Loading branch information
SongGwanSeok authored Nov 4, 2024
2 parents 8834cbe + fb4ce0d commit c098e3c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
IMAGE_TAG: latest
run: |
# Build a docker container and
# push it to ECR so that it can
Expand Down
4 changes: 3 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi import FastAPI
from starlette.responses import JSONResponse

from app.config.settings import Settings
from app.web import exception_handlers
from app.route.advice.router import router as advice_router
from app.route.execute.router import router as execute_router
Expand All @@ -25,7 +26,8 @@

# 미들웨어 등록
app.middleware("http")(log_request)
app.middleware("http")(log_response)
if Settings.ENVIRONMENT == "dev":
app.middleware("http")(log_response)

# 라우터 등록
app.include_router(advice_router, prefix="/edupi-assist")
Expand Down
11 changes: 11 additions & 0 deletions app/route/execute/exception/code_visualize_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from starlette.status import HTTP_400_BAD_REQUEST

from app.web.exception.base_exception import BaseCustomException
from app.web.exception.enum.error_enum import ErrorEnum


class CodeVisualizeError(BaseCustomException):
def __init__(self, error_enum: ErrorEnum, result: dict = None):
super().__init__(
status_code=HTTP_400_BAD_REQUEST, error_enum=error_enum, result={} if result is None else result
)
2 changes: 1 addition & 1 deletion app/route/execute/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def visualize(code_request: CodeRequest):

success_response = SuccessResponse(
detail="success code analysis",
result={"code": analysis_result.json()}
result={"code": analysis_result}
)

return JSONResponse(
Expand Down
8 changes: 6 additions & 2 deletions app/route/execute/service/analsys_service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import requests
from app.config.settings import Settings
from app.route.execute.exception.code_visualize_error import CodeVisualizeError
from app.web.exception.enum.error_enum import ErrorEnum


def analyze_code(source_code: str, user_input: str):
visualise_url = "/".join([Settings.ENGINE_SERVER, "v1", "python"])
success_response = requests.post(
response = requests.post(
visualise_url,
json={"source_code": source_code, "input": user_input}
)
return success_response
if not response.ok:
raise CodeVisualizeError(ErrorEnum.CODE_VISUALIZE_ERROR)

return response.json().get("result").get("code")
1 change: 1 addition & 0 deletions app/web/exception/enum/error_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ErrorEnum(Enum):

CODE_EXEC_ERROR = "CS-400004", "지원하지 않는 형식입니다."
INPUT_SIZE_MATCHING_ERROR = "CS-400005", "사용자 입력 개수가 일치하지 않습니다."
CODE_VISUALIZE_ERROR = "CS-400006", "아직 시각화할 수 없는 문법이 포함되어 있습니다."

#500
OPENAI_SERVER_ERROR = ("CS-504001", "Open AI internal server error")
Expand Down
7 changes: 4 additions & 3 deletions app/web/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from venv import logger

from openai import OpenAIError
from starlette import status
Expand All @@ -7,12 +7,12 @@

from app.route.execute.exception.code_execute_error import CodeExecuteError
from app.route.execute.exception.code_syntax_error import CodeSyntaxError
from app.route.execute.exception.code_visualize_error import CodeVisualizeError
from app.web.exception.base_exception import BaseCustomException
from app.web.exception.enum.error_enum import ErrorEnum
from app.web.exception.invalid_exception import InvalidException
from app.route.advice.exception.openai_exception import OpenaiException
from app.web.models.error_response import ErrorResponse
from app.web.logger import logger


def setup_exception_handlers(app: FastAPI):
Expand Down Expand Up @@ -44,7 +44,8 @@ async def openai_exception_handler(request: Request, exc: OpenaiException):

@app.exception_handler(CodeExecuteError)
@app.exception_handler(CodeSyntaxError)
async def code_error_exception_handler(request: Request, exc: CodeExecuteError | CodeSyntaxError):
@app.exception_handler(CodeVisualizeError)
async def code_error_exception_handler(request: Request, exc: CodeExecuteError | CodeSyntaxError | CodeVisualizeError):
response = ErrorResponse(code=exc.error_enum.code, detail=exc.error_enum.detail, result=exc.result)
return JSONResponse(status_code=exc.status, content=response.to_dict())

Expand Down
13 changes: 6 additions & 7 deletions app/web/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ def get_logger():

# Request 로깅 미들웨어 - request 로깅 필요할 시 주석 제거
async def log_request(request: Request, call_next):
pass
# current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# body = await request.body()
# logger.info(f"[{current_time}] Request: {request.method} {request.url} {body.decode()}")
#
# response = await call_next(request)
# return response
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
body = await request.body()
logger.info(f"[{current_time}] Request: {request.method} {request.url} {body.decode()}")

response = await call_next(request)
return response


# Response 로깅 미들웨어
Expand Down

0 comments on commit c098e3c

Please sign in to comment.