-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ switch from Flask to FastAPI for server-sent events; web chat
- Loading branch information
Showing
20 changed files
with
253 additions
and
140 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
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 |
---|---|---|
@@ -1,20 +1,10 @@ | ||
"""Default userland web module""" | ||
|
||
# 3rd party | ||
from apiflask import APIBlueprint | ||
|
||
# api | ||
from xthulu.models.user import User | ||
from xthulu.web.auth import auth | ||
|
||
api = APIBlueprint("userland", __name__, url_prefix="/user") | ||
# stdlib | ||
from importlib import import_module | ||
|
||
# 3rd party | ||
from fastapi import APIRouter | ||
|
||
@api.route("/") | ||
@api.auth_required(auth) | ||
def userland_demo(): | ||
user: User = auth.current_user # type: ignore | ||
return { | ||
"userland": True, | ||
"whoami": user.name, | ||
} | ||
api = APIRouter(prefix="/user") | ||
import_module(".routes", __name__) |
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,8 @@ | ||
"""Routes""" | ||
|
||
from . import chat, index | ||
|
||
__all__ = ( | ||
"chat", | ||
"index", | ||
) |
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,62 @@ | ||
"""Web chat""" | ||
|
||
# typing | ||
from typing import Annotated | ||
|
||
# stdlib | ||
from asyncio import sleep | ||
import json | ||
|
||
# 3rd party | ||
from fastapi import Depends, Request | ||
from sse_starlette.sse import EventSourceResponse | ||
|
||
# api | ||
from xthulu.models.user import User | ||
from xthulu.resources import Resources | ||
from xthulu.web.auth import login_user | ||
|
||
# local | ||
from ...scripts.chat import ChatMessage | ||
from .. import api | ||
|
||
|
||
@api.get("/chat/") | ||
def chat(user: Annotated[User, Depends(login_user)], request: Request): | ||
async def generate(): | ||
redis = Resources().cache | ||
pubsub = redis.pubsub() | ||
pubsub.subscribe("chat") | ||
redis.publish( | ||
"chat", | ||
json.dumps( | ||
ChatMessage( | ||
user=None, message=f"{user.name} has joined" | ||
).__dict__ | ||
), | ||
) | ||
|
||
try: | ||
while not await request.is_disconnected(): | ||
message = pubsub.get_message(True) | ||
data: bytes | ||
|
||
if not message: | ||
await sleep(0.1) | ||
continue | ||
|
||
data = message["data"] | ||
yield data.decode("utf-8") | ||
finally: | ||
redis.publish( | ||
"chat", | ||
json.dumps( | ||
ChatMessage( | ||
user=None, message=f"{user.name} has left" | ||
).__dict__ | ||
), | ||
) | ||
pubsub.close() | ||
redis.close() | ||
|
||
return EventSourceResponse(generate()) |
Oops, something went wrong.