diff --git a/.gitignore b/.gitignore index 4d2fe1b..03bd8e3 100644 --- a/.gitignore +++ b/.gitignore @@ -199,7 +199,7 @@ cython_debug/ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore # and can be added to the global gitignore or merged into this file. However, if you prefer, # you could uncomment the following to ignore the entire vscode folder -# .vscode/ +.vscode/ # Ruff stuff: .ruff_cache/ diff --git a/src/control_backend/api/v1/endpoints/sse_ping.py b/src/control_backend/api/v1/endpoints/sse_ping.py new file mode 100644 index 0000000..32d3805 --- /dev/null +++ b/src/control_backend/api/v1/endpoints/sse_ping.py @@ -0,0 +1,26 @@ +from fastapi import APIRouter, Request +from fastapi.responses import StreamingResponse +import datetime +import asyncio + +router = APIRouter() + +@router.get("/sse_ping") +async def sse_ping(request: Request): + """ + Endpoint for Server-Sent Events. + """ + async def event_generator(): + while True: + # If connection to client closes, stop sending events + if await request.is_disconnected(): + break + + # Send message containing current time every second + current_time = datetime.datetime.now().strftime("%H:%M:%S") + yield f"data: Server time: {current_time}\n\n" # \n\n is needed to separate events (SSE is text-based) + await asyncio.sleep(1) + + return StreamingResponse(event_generator(), media_type="text/event-stream") # media_type specifies that this connection is for event streams + + diff --git a/src/control_backend/api/v1/router.py b/src/control_backend/api/v1/router.py index 559b4d3..68f047e 100644 --- a/src/control_backend/api/v1/router.py +++ b/src/control_backend/api/v1/router.py @@ -1,6 +1,6 @@ from fastapi.routing import APIRouter -from control_backend.api.v1.endpoints import message, sse +from control_backend.api.v1.endpoints import message, sse, sse_ping api_router = APIRouter() @@ -13,3 +13,8 @@ api_router.include_router( sse.router, tags=["SSE"] ) + +api_router.include_router( + sse_ping.router, + tags=["SSE_ping"] +) diff --git a/test/unit/test_ri_commands_agent.py b/test/unit/agents/test_ri_commands_agent.py similarity index 100% rename from test/unit/test_ri_commands_agent.py rename to test/unit/agents/test_ri_commands_agent.py diff --git a/test/unit/test_ri_communication_agent.py b/test/unit/agents/test_ri_communication_agent.py similarity index 100% rename from test/unit/test_ri_communication_agent.py rename to test/unit/agents/test_ri_communication_agent.py