-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_websocket.py
More file actions
52 lines (44 loc) · 1.63 KB
/
main_websocket.py
File metadata and controls
52 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
WebSocket server for docker-compose
"""
import asyncio
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fraq import FileSearchAdapter, FraqNode
from fraq.generators import HashGenerator
app = FastAPI(title="fraq WebSocket")
@app.websocket("/ws/stream")
async def ws_stream(websocket: WebSocket):
await websocket.accept()
try:
while True:
msg = await websocket.receive_json()
if msg.get("action") == "stream":
count = msg.get("count", 10)
root = FraqNode(position=(0.0, 0.0, 0.0), generator=HashGenerator())
cursor = root.cursor()
for i in range(count):
cursor.advance()
await websocket.send_json({"index": i, "value": cursor.current.value})
await asyncio.sleep(0.1)
except WebSocketDisconnect:
pass
@app.websocket("/ws/files")
async def ws_files(websocket: WebSocket):
await websocket.accept()
try:
while True:
msg = await websocket.receive_json()
if msg.get("action") == "search":
path = msg.get("path", "/data")
ext = msg.get("ext")
limit = msg.get("limit", 10)
adapter = FileSearchAdapter(base_path=path, recursive=True)
for record in adapter.stream(extension=ext, count=limit):
await websocket.send_json(record)
await asyncio.sleep(0.01)
await websocket.send_json({"done": True})
except WebSocketDisconnect:
pass
@app.get("/health")
def health():
return {"status": "ok"}