Skip to content

Commit

Permalink
fix socket-io server setup
Browse files Browse the repository at this point in the history
  • Loading branch information
lucafaggianelli committed Jul 28, 2024
1 parent 7d7313f commit fd7a0aa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
1 change: 1 addition & 0 deletions frontend/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { getWebsocketUrl } from './repository'

export const socket = io(getWebsocketUrl().toString(), {
transports: ['websocket'],
path: '/ws/socket.io',
})
4 changes: 2 additions & 2 deletions src/plombery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from apscheduler.schedulers.base import SchedulerAlreadyRunningError
from pydantic import BaseModel

from .api import combined_app, app
from .api import app
from .config import settings
from .logger import get_logger # noqa F401
from .notifications import NotificationRule, notification_manager
Expand Down Expand Up @@ -52,7 +52,7 @@ def stop(self):
# Wrap FastAPI ASGI interface so the Plombery object
# can be served directly by uvicorn
async def __call__(self, scope, receive, send):
await combined_app.__call__(scope, receive, send)
await app.__call__(scope, receive, send)


_app = _Plombery()
Expand Down
17 changes: 9 additions & 8 deletions src/plombery/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from fastapi import FastAPI

from plombery.api.authentication import init_auth
from plombery.api.authentication import build_auth_router
from plombery._version import __version__
from plombery.websocket import install_socketio_server
from .middlewares import SPAStaticFiles, setup_cors
from .routers import pipelines, runs
from plombery.websocket import asgi
from plombery.api.middlewares import SPAStaticFiles, setup_cors
from plombery.api.routers import pipelines, runs


API_PREFIX = "/api"

app = FastAPI(title="Plombery", version=__version__, redirect_slashes=False)

# Mount the websocket before the other middlewares
# to avoid conflicts
app.mount("/ws", asgi, name="socket")

setup_cors(app)
auth_router = init_auth(app)

app.include_router(pipelines.router, prefix=API_PREFIX)
app.include_router(runs.router, prefix=API_PREFIX)
app.include_router(auth_router, prefix=API_PREFIX)

combined_app = install_socketio_server(app)
app.include_router(build_auth_router(app), prefix=API_PREFIX)

app.mount("/", SPAStaticFiles(api_prefix=API_PREFIX))
2 changes: 1 addition & 1 deletion src/plombery/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from plombery.config import settings


def init_auth(app: FastAPI) -> APIRouter:
def build_auth_router(app: FastAPI) -> APIRouter:
router = APIRouter(
prefix="/auth",
tags=["Authentication"],
Expand Down
6 changes: 1 addition & 5 deletions src/plombery/websocket.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from fastapi import FastAPI
import socketio


sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")


def install_socketio_server(app: FastAPI):
return socketio.ASGIApp(socketio_server=sio, other_asgi_app=app)
asgi = socketio.ASGIApp(socketio_server=sio)

0 comments on commit fd7a0aa

Please sign in to comment.