-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
100 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from datetime import datetime | ||
import json | ||
from fastapi import APIRouter, Request | ||
from fastapi.responses import StreamingResponse | ||
from loguru import logger | ||
|
||
import logging | ||
|
||
from pydantic import BaseModel | ||
from utils.sse_manager import sse_manager | ||
|
||
|
||
router = APIRouter( | ||
responses={404: {"description": "Not found"}}, | ||
prefix="/stream", | ||
tags=["stream"], | ||
) | ||
|
||
class EventResponse(BaseModel): | ||
data: dict | ||
|
||
class SSELogHandler(logging.Handler): | ||
def emit(self, record: logging.LogRecord): | ||
log_entry = { | ||
"time": datetime.fromtimestamp(record.created).isoformat(), | ||
"level": record.levelname, | ||
"message": record.msg | ||
} | ||
sse_manager.publish_event("logging", json.dumps(log_entry)) | ||
|
||
logger.add(SSELogHandler()) | ||
|
||
@router.get("/event_types") | ||
async def get_event_types(): | ||
return {"message": list(sse_manager.event_queues.keys())} | ||
|
||
@router.get("/{event_type}") | ||
async def stream_events(_: Request, event_type: str) -> EventResponse: | ||
return StreamingResponse(sse_manager.subscribe(event_type), media_type="text/event-stream") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import asyncio | ||
from typing import Dict, Any | ||
|
||
class ServerSentEventManager: | ||
def __init__(self): | ||
self.event_queues: Dict[str, asyncio.Queue] = {} | ||
|
||
def publish_event(self, event_type: str, data: Any): | ||
if not data: | ||
return | ||
if event_type not in self.event_queues: | ||
self.event_queues[event_type] = asyncio.Queue() | ||
self.event_queues[event_type].put_nowait(data) | ||
|
||
async def subscribe(self, event_type: str): | ||
if event_type not in self.event_queues: | ||
self.event_queues[event_type] = asyncio.Queue() | ||
|
||
while True: | ||
try: | ||
data = await asyncio.wait_for(self.event_queues[event_type].get(), timeout=1.0) | ||
yield f"data: {data}\n\n" | ||
except asyncio.TimeoutError: | ||
pass | ||
|
||
sse_manager = ServerSentEventManager() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.