refactor: cleaned main file
This commit is contained in:
4
core/__init__.py
Normal file
4
core/__init__.py
Normal 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
11
core/app_settings.py
Normal 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
17
core/exceptions.py
Normal 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
16
core/lifespan.py
Normal 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
19
core/middlewares.py
Normal 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,
|
||||
)
|
||||
@ -1,7 +0,0 @@
|
||||
class Singleton(type):
|
||||
_instances = {}
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
if cls not in cls._instances:
|
||||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
||||
return cls._instances[cls]
|
||||
Reference in New Issue
Block a user