Use server side events
This commit is contained in:
parent
d8b8c0f801
commit
49a49f7ea6
|
@ -1,4 +1,5 @@
|
|||
from fastapi import APIRouter, HTTPException, status, WebSocket
|
||||
from fastapi import APIRouter, HTTPException, status, WebSocket, Request
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
import base64
|
||||
import docker
|
||||
import aiodocker
|
||||
|
@ -42,23 +43,25 @@ async def commnd(server, command):
|
|||
return f"{server} {base64.urlsafe_b64decode(command).decode('utf_8')}"
|
||||
|
||||
|
||||
@router.websocket("/server/{server}/logs")
|
||||
async def logs(websocket: WebSocket, server: str):
|
||||
await websocket.accept()
|
||||
@router.get("/server/{server}/logs")
|
||||
async def logs(server: str, request: Request):
|
||||
container = await getContainer(server)
|
||||
async for line in container.log(stdout=True, follow=True, tail=5000):
|
||||
#print(line)
|
||||
await websocket.send_bytes(line)
|
||||
print("Closed")
|
||||
return server
|
||||
#event_generator = logGenerator(request, server)
|
||||
return EventSourceResponse(container.log(stdout=True, follow=True, tail=5000))
|
||||
|
||||
@router.get("/server/{server}/stats")
|
||||
async def stats(websocket: WebSocket, server: str):
|
||||
await websocket.accept()
|
||||
async def logGenerator(request, server):
|
||||
container = await getContainer(server)
|
||||
async for line in container.log():
|
||||
await websocket.send_bytes(line)
|
||||
return server
|
||||
for line in container.log(stdout=True, follow=True, tail=5000):
|
||||
if await request.is_disconnected():
|
||||
print("client disconnected!!!")
|
||||
break
|
||||
yield line
|
||||
|
||||
# @router.get("/server/{server}/stats")
|
||||
# async def stats(server: str):
|
||||
|
||||
# container = await getContainer(server)
|
||||
# return EventSourceResponse(container.stats(stream=True))
|
||||
|
||||
|
||||
async def getContainer(server):
|
||||
|
|
Loading…
Reference in New Issue