first commit

This commit is contained in:
bluebamus committed 2025-12-19 09:36:23 +09:00
commit 1ff70c7925
71 files changed
+4175

No files matched your search

View File
Whitespace-only changes.
+41
View File
@@ -0,0 +1,41 @@
# app/main.py
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
"""FastAPI 애플리케이션 생명주기 관리"""
# Startup - 애플리케이션 시작 시
print("Starting up...")
try:
pass
# # 데이터베이스 테이블 생성
# from app.database.session import create_db_tables
# await create_db_tables()
# print("Database tables ready")
except asyncio.TimeoutError:
print("Database initialization timed out")
# 타임아웃 시 앱 시작 중단하려면 raise, 계속하려면 pass
raise
except Exception as e:
print(f"Database initialization failed: {e}")
# 에러 시 앱 시작 중단하려면 raise, 계속하려면 pass
raise
yield # 애플리케이션 실행 중
# Shutdown - 애플리케이션 종료 시
print("Shutting down...")
from app.database.session import engine
await engine.dispose()
print("Database engine disposed")
# FastAPI 앱 생성 (lifespan 적용)
app = FastAPI(title="CastAD", lifespan=lifespan)
+114
View File
@@ -0,0 +1,114 @@
from fastapi import FastAPI, HTTPException, Request, Response, status
from fastapi.responses import JSONResponse
class FastShipError(Exception):
"""Base exception for all exceptions in fastship api"""
# status_code to be returned for this exception
# when it is handled
status = status.HTTP_400_BAD_REQUEST
class EntityNotFound(FastShipError):
"""Entity not found in database"""
status = status.HTTP_404_NOT_FOUND
class BadPassword(FastShipError):
"""Password is not strong enough or invalid"""
status = status.HTTP_400_BAD_REQUEST
class ClientNotAuthorized(FastShipError):
"""Client is not authorized to perform the action"""
status = status.HTTP_401_UNAUTHORIZED
class ClientNotVerified(FastShipError):
"""Client is not verified"""
status = status.HTTP_401_UNAUTHORIZED
class NothingToUpdate(FastShipError):
"""No data provided to update"""
class BadCredentials(FastShipError):
"""User email or password is incorrect"""
status = status.HTTP_401_UNAUTHORIZED
class InvalidToken(FastShipError):
"""Access token is invalid or expired"""
status = status.HTTP_401_UNAUTHORIZED
class DeliveryPartnerNotAvailable(FastShipError):
"""Delivery partner/s do not service the destination"""
status = status.HTTP_406_NOT_ACCEPTABLE
class DeliveryPartnerCapacityExceeded(FastShipError):
"""Delivery partner has reached their max handling capacity"""
status = status.HTTP_406_NOT_ACCEPTABLE
def _get_handler(status: int, detail: str):
# Define
def handler(request: Request, exception: Exception) -> Response:
# DEBUG PRINT STATEMENT 👇
from rich import print, panel
print(
panel.Panel(
exception.__class__.__name__,
title="Handled Exception",
border_style="red",
),
)
# DEBUG PRINT STATEMENT 👆
# Raise HTTPException with given status and detail
# can return JSONResponse as well
raise HTTPException(
status_code=status,
detail=detail,
)
# Return ExceptionHandler required with given
# status and detail for HTTPExcetion above
return handler
def add_exception_handlers(app: FastAPI):
# Get all subclass of 👇, our custom exceptions
exception_classes = FastShipError.__subclasses__()
for exception_class in exception_classes:
# Add exception handler
app.add_exception_handler(
# Custom exception class
exception_class,
# Get handler function
_get_handler(
status=exception_class.status,
detail=exception_class.__doc__,
),
)
@app.exception_handler(status.HTTP_500_INTERNAL_SERVER_ERROR)
def internal_server_error_handler(request, exception):
return JSONResponse(
content={"detail": "Something went wrong..."},
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
headers={
"X-Error": f"{exception}",
}
)
View File
Whitespace-only changes.