Skip to content

Commit

Permalink
add frontend code
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarriba committed Oct 4, 2023
1 parent de2faed commit cb0f03f
Show file tree
Hide file tree
Showing 13 changed files with 4,873 additions and 17 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@
from farm_ng.core.events_file_reader import proto_from_json_file
from farm_ng.core.uri_pb2 import Uri
from fastapi import FastAPI
from fastapi import WebSocket
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.responses import StreamingResponse
from google.protobuf.json_format import MessageToJson

app = FastAPI()

app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)

# to store the events clients
clients: dict[str, EventClient] = {}

Expand All @@ -57,27 +66,19 @@ async def list_uris() -> JSONResponse:
return JSONResponse(content=all_uris, status_code=200)


@app.get("/subscribe/{service_name}/{uri_path}")
async def subscribe(service_name: str, uri_path: str, every_n: int = 1):

if service_name not in clients:
return JSONResponse(content={"error": f"service {service_name} is not available"}, status_code=404)
@app.websocket("/subscribe/{service_name}/{uri_path}")
async def subscribe(websocket: WebSocket, service_name: str, uri_path: str, every_n: int = 1):

client: EventClient = clients[service_name]

uris = await client.list_uris()

if not any(uri.path == f"/{uri_path}" for uri in uris):
return JSONResponse(content={"error": f"uri {uri_path} is not available"}, status_code=404)
await websocket.accept()

# subscribe to the uri
async def generate_data():
async for event, message in client.subscribe(
request=SubscribeRequest(uri=Uri(path=f"/{uri_path}"), every_n=every_n), decode=True
):
yield MessageToJson(message)
async for event, message in client.subscribe(
request=SubscribeRequest(uri=Uri(path=f"/{uri_path}"), every_n=every_n), decode=True
):
await websocket.send_json(MessageToJson(message))

return StreamingResponse(generate_data(), media_type="text/event-stream")
await websocket.close()


@app.get("/")
Expand Down
File renamed without changes.
52 changes: 52 additions & 0 deletions py/examples/monitor_app/ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

# Generated
src/generated
12 changes: 12 additions & 0 deletions py/examples/monitor_app/ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Messages Streaming App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading

0 comments on commit cb0f03f

Please sign in to comment.