refactor: cleaned main file

This commit is contained in:
2025-10-31 11:29:24 +04:00
parent 36b3e056dc
commit ef657c4939
8 changed files with 84 additions and 53 deletions

4
core/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from .lifespan import lifespan as lifespan
from .app_settings import settings as settings
from .middlewares import register_middlewares as register_middlewares
from .exceptions import register_exception_handlers as register_exception_handlers

11
core/app_settings.py Normal file
View File

@ -0,0 +1,11 @@
from fastapi.responses import ORJSONResponse
class Settings:
ROOT_PATH = "/api"
DEFAULT_RESPONSE_CLASS = ORJSONResponse
ORIGINS = ["http://localhost:3000"]
STATIC_DIR = "static"
settings = Settings()

17
core/exceptions.py Normal file
View File

@ -0,0 +1,17 @@
from fastapi import Request
from fastapi.applications import AppType
from starlette.responses import JSONResponse
from utils.exceptions import ObjectNotFoundException, ForbiddenException
def register_exception_handlers(app: AppType):
@app.exception_handler(ObjectNotFoundException)
async def not_found_exception_handler(
request: Request, exc: ObjectNotFoundException
):
return JSONResponse(status_code=404, content={"detail": exc.name})
@app.exception_handler(ForbiddenException)
async def forbidden_exception_handler(request: Request, exc: ForbiddenException):
return JSONResponse(status_code=403, content={"detail": exc.name})

16
core/lifespan.py Normal file
View File

@ -0,0 +1,16 @@
import contextlib
from fastapi.applications import AppType
from task_management import broker
@contextlib.asynccontextmanager
async def lifespan(app: AppType):
if not broker.is_worker_process:
await broker.startup()
yield
if not broker.is_worker_process:
await broker.shutdown()

19
core/middlewares.py Normal file
View File

@ -0,0 +1,19 @@
from fastapi.applications import AppType
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from .app_settings import settings
def register_middlewares(app: AppType):
app.add_middleware(
CORSMiddleware,
allow_origins=settings.ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(
GZipMiddleware,
minimum_size=1_000,
)

View File

@ -10,7 +10,7 @@ from logger.constants import (
from logger.formatter import JsonFormatter from logger.formatter import JsonFormatter
from logger.gunzip_rotating_file_handler import GunZipRotatingFileHandler from logger.gunzip_rotating_file_handler import GunZipRotatingFileHandler
from logger.filters import LevelFilter, RequestIdFilter from logger.filters import LevelFilter, RequestIdFilter
from core.singleton import Singleton from utils.singleton import Singleton
class LoggerBuilder(metaclass=Singleton): class LoggerBuilder(metaclass=Singleton):

66
main.py
View File

@ -1,64 +1,28 @@
import contextlib
import taskiq_fastapi import taskiq_fastapi
from fastapi import FastAPI, Request from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import ORJSONResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from starlette.responses import JSONResponse
import routers import routers
from core import lifespan, settings, register_middlewares, register_exception_handlers
from task_management import broker from task_management import broker
from utils.auto_include_routers import auto_include_routers from utils.auto_include_routers import auto_include_routers
from utils.exceptions import *
origins = ["http://localhost:3000"]
@contextlib.asynccontextmanager def create_app() -> FastAPI:
async def lifespan(app): app = FastAPI(
if not broker.is_worker_process:
await broker.startup()
yield
if not broker.is_worker_process:
await broker.shutdown()
app = FastAPI(
separate_input_output_schemas=True, separate_input_output_schemas=True,
default_response_class=ORJSONResponse, default_response_class=settings.DEFAULT_RESPONSE_CLASS,
root_path="/api", root_path=settings.ROOT_PATH,
# lifespan=lifespan, lifespan=lifespan,
) )
app.add_middleware( register_middlewares(app)
CORSMiddleware, register_exception_handlers(app)
allow_origins=origins, auto_include_routers(app, routers, full_path=True)
allow_credentials=True, app.mount("/static", StaticFiles(directory=settings.STATIC_DIR), name="static")
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(
GZipMiddleware,
minimum_size=1_000,
)
taskiq_fastapi.init(broker, "main:app") taskiq_fastapi.init(broker, "main:app")
return app
@app.exception_handler(ObjectNotFoundException) app = create_app()
async def not_found_exception_handler(request: Request, exc: ObjectNotFoundException):
return JSONResponse(status_code=404, content={"detail": exc.name})
@app.exception_handler(ForbiddenException)
async def forbidden_exception_handler(request: Request, exc: ForbiddenException):
return JSONResponse(status_code=403, content={"detail": exc.name})
auto_include_routers(app, routers, True)
app.mount("/static", StaticFiles(directory="static"), name="static")