diff options
| author | Pinapelz <yukais@pinapelz.com> | 2026-04-23 20:19:57 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2026-04-23 20:43:24 -0700 |
| commit | 1ff76ea759b966bc7683fcaff17314788687c950 (patch) | |
| tree | bcb68e699895cd8ccddebce3ed93c8f38324c391 /routes.py | |
| parent | acbc2ed1a5e720670be87e87b4653d4cd310950f (diff) | |
move flask server to blueprint module
Diffstat (limited to 'routes.py')
| -rw-r--r-- | routes.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/routes.py b/routes.py new file mode 100644 index 0000000..2a51766 --- /dev/null +++ b/routes.py @@ -0,0 +1,36 @@ +from typing import Callable, Generator, Optional + +from typing_extensions import Any +from flask import Blueprint, Response, stream_with_context, Flask + +SSEGenerator = Callable[[], Generator[str, None, None]] + + +def make_sse_blueprint( + event_stream_func: SSEGenerator, + blueprint_name: str = "sse_routes", + url_prefix: Optional[str] = None, +) -> Blueprint: + bp = Blueprint(blueprint_name, __name__, url_prefix=url_prefix) + + @bp.get("/events") + def events() -> Response: + headers = { + "Cache-Control": "no-cache", + "Connection": "keep-alive", + "Access-Control-Allow-Origin": "*", + } + return Response(stream_with_context(event_stream_func()), mimetype="text/event-stream", headers=headers) + + @bp.get("/health") + def health() -> Response: + resp = Response("ok", mimetype="text/plain") + resp.headers["Access-Control-Allow-Origin"] = "*" + return resp + + return bp + + +def register_routes(app: Flask, event_stream_func: Any, url_prefix: Optional[str] = None) -> None: + bp = make_sse_blueprint(event_stream_func, url_prefix=url_prefix) + app.register_blueprint(bp) |
