voice-changer/server/sio/MMVC_SocketIOApp.py

81 lines
3.3 KiB
Python
Raw Permalink Normal View History

2022-12-31 12:27:38 +03:00
import socketio
2023-08-14 05:23:53 +03:00
from mods.log_control import VoiceChangaerLogger
2024-03-18 23:52:22 +03:00
from mods.origins import compute_local_origins, normalize_origins
2022-12-31 12:27:38 +03:00
2024-03-18 23:52:22 +03:00
from typing import Sequence, Optional
2022-12-31 12:27:38 +03:00
from sio.MMVC_SocketIOServer import MMVC_SocketIOServer
from voice_changer.VoiceChangerManager import VoiceChangerManager
2023-03-08 03:48:50 +03:00
from const import getFrontendPath
2022-12-31 12:27:38 +03:00
2023-08-14 05:23:53 +03:00
logger = VoiceChangaerLogger.get_instance().getLogger()
2023-02-16 21:47:43 +03:00
2023-05-04 06:29:12 +03:00
class MMVC_SocketIOApp:
_instance: socketio.ASGIApp | None = None
2022-12-31 12:27:38 +03:00
@classmethod
2024-03-18 23:52:22 +03:00
def get_instance(
cls,
app_fastapi,
voiceChangerManager: VoiceChangerManager,
allowedOrigins: Optional[Sequence[str]] = None,
port: Optional[int] = None,
):
2023-05-04 06:29:12 +03:00
if cls._instance is None:
2023-08-14 05:23:53 +03:00
logger.info("[Voice Changer] MMVC_SocketIOApp initializing...")
2024-03-18 23:52:22 +03:00
allowed_origins: set[str] = set()
local_origins = compute_local_origins(port)
allowed_origins.update(local_origins)
if allowedOrigins is not None:
normalized_origins = normalize_origins(allowedOrigins)
allowed_origins.update(normalized_origins)
sio = MMVC_SocketIOServer.get_instance(voiceChangerManager, list(allowed_origins))
2022-12-31 12:27:38 +03:00
app_socketio = socketio.ASGIApp(
2023-02-16 21:47:43 +03:00
sio,
other_asgi_app=app_fastapi,
static_files={
2023-05-04 06:29:12 +03:00
"/assets/icons/github.svg": {
"filename": f"{getFrontendPath()}/assets/icons/github.svg",
"content_type": "image/svg+xml",
2023-02-16 21:47:43 +03:00
},
2023-05-04 06:29:12 +03:00
"/assets/icons/help-circle.svg": {
"filename": f"{getFrontendPath()}/assets/icons/help-circle.svg",
"content_type": "image/svg+xml",
2023-02-16 21:47:43 +03:00
},
2023-05-04 06:29:12 +03:00
"/assets/icons/tool.svg": {
"filename": f"{getFrontendPath()}/assets/icons/tool.svg",
"content_type": "image/svg+xml",
},
2023-06-09 13:57:08 +03:00
"/assets/icons/folder.svg": {
"filename": f"{getFrontendPath()}/assets/icons/folder.svg",
"content_type": "image/svg+xml",
},
2023-05-04 06:29:12 +03:00
"/buymeacoffee.png": {
"filename": f"{getFrontendPath()}/assets/buymeacoffee.png",
"content_type": "image/png",
2023-02-16 21:47:43 +03:00
},
2023-10-09 06:15:03 +03:00
"/ort-wasm-simd.wasm": {
"filename": f"{getFrontendPath()}/ort-wasm-simd.wasm",
"content_type": "application/wasm",
},
"/assets/beatrice/female-clickable.svg": {
"filename": f"{getFrontendPath()}/assets/beatrice/female-clickable.svg",
"content_type": "image/svg+xml",
},
"/assets/beatrice/male-clickable.svg": {
"filename": f"{getFrontendPath()}/assets/beatrice/male-clickable.svg",
"content_type": "image/svg+xml",
},
2023-05-04 06:29:12 +03:00
"": f"{getFrontendPath()}",
"/": f"{getFrontendPath()}/index.html",
},
2023-02-16 21:47:43 +03:00
)
2022-12-31 12:27:38 +03:00
cls._instance = app_socketio
2023-08-14 05:23:53 +03:00
logger.info("[Voice Changer] MMVC_SocketIOApp initializing... done.")
2022-12-31 12:27:38 +03:00
return cls._instance
return cls._instance