2022-12-31 12:27:38 +03:00
|
|
|
from fastapi import FastAPI, Request, Response
|
|
|
|
from fastapi.routing import APIRoute
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from typing import Callable
|
|
|
|
from voice_changer.VoiceChangerManager import VoiceChangerManager
|
|
|
|
|
|
|
|
from restapi.MMVC_Rest_Hello import MMVC_Rest_Hello
|
|
|
|
from restapi.MMVC_Rest_VoiceChanger import MMVC_Rest_VoiceChanger
|
2022-12-31 12:56:23 +03:00
|
|
|
from restapi.MMVC_Rest_Fileuploader import MMVC_Rest_Fileuploader
|
2022-12-31 14:25:28 +03:00
|
|
|
from restapi.MMVC_Rest_Trainer import MMVC_Rest_Trainer
|
2023-01-05 19:37:29 +03:00
|
|
|
from const import frontend_path
|
2022-12-31 14:25:28 +03:00
|
|
|
|
|
|
|
|
2022-12-31 12:27:38 +03:00
|
|
|
class ValidationErrorLoggingRoute(APIRoute):
|
|
|
|
def get_route_handler(self) -> Callable:
|
|
|
|
original_route_handler = super().get_route_handler()
|
|
|
|
|
|
|
|
async def custom_route_handler(request: Request) -> Response:
|
|
|
|
try:
|
|
|
|
return await original_route_handler(request)
|
|
|
|
except Exception as exc:
|
|
|
|
print("Exception", request.url, str(exc))
|
|
|
|
body = await request.body()
|
|
|
|
detail = {"errors": exc.errors(), "body": body.decode()}
|
|
|
|
raise HTTPException(status_code=422, detail=detail)
|
|
|
|
|
|
|
|
return custom_route_handler
|
|
|
|
|
|
|
|
class MMVC_Rest:
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_instance(cls, voiceChangerManager:VoiceChangerManager):
|
|
|
|
if not hasattr(cls, "_instance"):
|
|
|
|
app_fastapi = FastAPI()
|
|
|
|
app_fastapi.router.route_class = ValidationErrorLoggingRoute
|
|
|
|
app_fastapi.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=["*"],
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
|
|
|
app_fastapi.mount(
|
2023-01-05 19:37:29 +03:00
|
|
|
"/front", StaticFiles(directory=f'{frontend_path}', html=True), name="static")
|
2022-12-31 12:27:38 +03:00
|
|
|
|
|
|
|
app_fastapi.mount(
|
2023-01-05 19:37:29 +03:00
|
|
|
"/trainer", StaticFiles(directory=f'{frontend_path}', html=True), name="static")
|
2022-12-31 12:27:38 +03:00
|
|
|
|
|
|
|
app_fastapi.mount(
|
2023-01-05 19:37:29 +03:00
|
|
|
"/recorder", StaticFiles(directory=f'{frontend_path}', html=True), name="static")
|
2022-12-31 12:27:38 +03:00
|
|
|
|
|
|
|
restHello = MMVC_Rest_Hello()
|
|
|
|
app_fastapi.include_router(restHello.router)
|
|
|
|
restVoiceChanger = MMVC_Rest_VoiceChanger(voiceChangerManager)
|
|
|
|
app_fastapi.include_router(restVoiceChanger.router)
|
2022-12-31 12:56:23 +03:00
|
|
|
fileUploader = MMVC_Rest_Fileuploader(voiceChangerManager)
|
|
|
|
app_fastapi.include_router(fileUploader.router)
|
2022-12-31 14:25:28 +03:00
|
|
|
trainer = MMVC_Rest_Trainer()
|
|
|
|
app_fastapi.include_router(trainer.router)
|
2022-12-31 12:27:38 +03:00
|
|
|
|
|
|
|
cls._instance = app_fastapi
|
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
return cls._instance
|