Added broadcaster dependency #17

Merged
Luis merged 4 commits from Luis-Hebendanz-main into main 2023-11-26 13:12:30 +00:00
13 changed files with 320 additions and 57 deletions

35
flake.lock generated
View File

@@ -7,11 +7,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1696343447, "lastModified": 1698882062,
"narHash": "sha256-B2xAZKLkkeRFG5XcHHSXXcP7To9Xzr59KXeZiRf4vdQ=", "narHash": "sha256-HkhafUayIqxXyHH1X8d9RDl1M2CkFgZLjKD3MzabiEo=",
"owner": "hercules-ci", "owner": "hercules-ci",
"repo": "flake-parts", "repo": "flake-parts",
"rev": "c9afaba3dfa4085dbd2ccb38dfade5141e33d9d4", "rev": "8c9fa2545007b49a5db5f650ae91f227672c3877",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -42,11 +42,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1697059129, "lastModified": 1700390070,
"narHash": "sha256-9NJcFF9CEYPvHJ5ckE8kvINvI84SZZ87PvqMbH6pro0=", "narHash": "sha256-de9KYi8rSJpqvBfNwscWdalIJXPo8NjdIZcEJum1mH0=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "5e4c2ada4fcd54b99d56d7bd62f384511a7e2593", "rev": "e4ad989506ec7d71f7302cc3067abd82730a4beb",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -56,11 +56,28 @@
"type": "github" "type": "github"
} }
}, },
"nixpkgs-for-iosl": {
"locked": {
"lastModified": 1700515317,
"narHash": "sha256-DSnKT3glZCKE0/Rc6ainSJUbUGC248HNKVRSbo9kxkM=",
"owner": "Luis-Hebendanz",
"repo": "nixpkgs",
"rev": "bcd6be7a5ab22c94c775945fb16a61b5867f15d2",
"type": "github"
},
"original": {
"owner": "Luis-Hebendanz",
"ref": "iosl",
"repo": "nixpkgs",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"flake-parts": "flake-parts", "flake-parts": "flake-parts",
"floco": "floco", "floco": "floco",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"nixpkgs-for-iosl": "nixpkgs-for-iosl",
"treefmt-nix": "treefmt-nix" "treefmt-nix": "treefmt-nix"
} }
}, },
@@ -71,11 +88,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1695822946, "lastModified": 1699786194,
"narHash": "sha256-IQU3fYo0H+oGlqX5YrgZU3VRhbt2Oqe6KmslQKUO4II=", "narHash": "sha256-3h3EH1FXQkIeAuzaWB+nK0XK54uSD46pp+dMD3gAcB4=",
"owner": "numtide", "owner": "numtide",
"repo": "treefmt-nix", "repo": "treefmt-nix",
"rev": "720bd006d855b08e60664e4683ccddb7a9ff614a", "rev": "e82f32aa7f06bbbd56d7b12186d555223dc399d1",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@@ -7,6 +7,7 @@
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
floco.url = "github:aakropotkin/floco"; floco.url = "github:aakropotkin/floco";
floco.inputs.nixpkgs.follows = "nixpkgs"; floco.inputs.nixpkgs.follows = "nixpkgs";
nixpkgs-for-iosl.url = "github:Luis-Hebendanz/nixpkgs/iosl";
flake-parts.url = "github:hercules-ci/flake-parts"; flake-parts.url = "github:hercules-ci/flake-parts";
flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
treefmt-nix.url = "github:numtide/treefmt-nix"; treefmt-nix.url = "github:numtide/treefmt-nix";

View File

@@ -18,5 +18,5 @@
"python.linting.mypyPath": "mypy", "python.linting.mypyPath": "mypy",
"python.linting.mypyEnabled": true, "python.linting.mypyEnabled": true,
"python.linting.enabled": true, "python.linting.enabled": true,
"python.defaultInterpreterPath": "python" "python.defaultInterpreterPath": "/nix/store/k34qdl5397mwg3k00jsl3xcynij7n0z9-python3-3.11.6-env/bin/python"
} }

View File

@@ -4,8 +4,8 @@ import sys
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
from .clan_types import FlakeName
from .errors import ClanError from .errors import ClanError
from .types import FlakeName
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@@ -5,8 +5,8 @@ from typing import Any
from pydantic import AnyUrl, BaseModel, validator from pydantic import AnyUrl, BaseModel, validator
from pydantic.tools import parse_obj_as from pydantic.tools import parse_obj_as
from ..clan_types import validate_path
from ..dirs import clan_data_dir, clan_flakes_dir from ..dirs import clan_data_dir, clan_flakes_dir
from ..types import validate_path
DEFAULT_URL = parse_obj_as(AnyUrl, "http://localhost:8000") DEFAULT_URL = parse_obj_as(AnyUrl, "http://localhost:8000")

View File

@@ -1,4 +1,6 @@
import logging import logging
from contextlib import asynccontextmanager
from typing import Any
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
@@ -8,7 +10,7 @@ from fastapi.staticfiles import StaticFiles
from ..errors import ClanError from ..errors import ClanError
from .assets import asset_path from .assets import asset_path
from .error_handlers import clan_error_handler from .error_handlers import clan_error_handler
from .routers import health, root from .routers import health, root, socket_manager2
origins = [ origins = [
"http://localhost:3000", "http://localhost:3000",
@@ -17,8 +19,15 @@ origins = [
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI) -> Any:
await socket_manager2.brd.connect()
yield
await socket_manager2.brd.disconnect()
def setup_app() -> FastAPI: def setup_app() -> FastAPI:
app = FastAPI() app = FastAPI(lifespan=lifespan)
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=origins, allow_origins=origins,
@@ -29,9 +38,11 @@ def setup_app() -> FastAPI:
app.include_router(health.router) app.include_router(health.router)
app.include_router(socket_manager2.router)
# Needs to be last in register. Because of wildcard route # Needs to be last in register. Because of wildcard route
app.include_router(root.router) app.include_router(root.router)
app.add_exception_handler(ClanError, clan_error_handler) app.add_exception_handler(ClanError, clan_error_handler) # type: ignore
app.mount("/static", StaticFiles(directory=asset_path()), name="static") app.mount("/static", StaticFiles(directory=asset_path()), name="static")

View File

@@ -0,0 +1,205 @@
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
if (typeof WebSocket != "undefined") {
$("#ask").show();
} else {
$("#error").show();
}
// join on enter
$("#ask input").keydown(function (event) {
if (event.keyCode == 13) {
$("#ask a").click();
}
});
// join on click
$("#ask a").click(function () {
join($("#ask input").val());
$("#ask").hide();
$("#channel").show();
$("input#message").focus();
});
function join(name) {
var host = window.location.host.split(":")[0];
var ws = new WebSocket("ws://localhost:2979/ws2");
var container = $("div#msgs");
ws.onmessage = function (evt) {
var obj = JSON.parse(evt.data);
if (typeof obj != "object") return;
console.log("Received: " + obj);
var action = obj["action"];
var struct = container.find("li." + action + ":first");
if (struct.length < 1) {
console.log("Could not handle: " + evt.data);
return;
}
var msg = struct.clone();
msg.find(".time").text(new Date().toString("HH:mm:ss"));
if (action == "message") {
var matches;
if ((matches = obj["message"].match(/^\s*[\/\\]me\s(.*)/))) {
msg.find(".user").text(obj["user"] + " " + matches[1]);
msg.find(".user").css("font-weight", "bold");
} else {
msg.find(".user").text(obj["user"]);
msg.find(".message").text(": " + obj["message"]);
}
} else if (action == "control") {
msg.find(".user").text(obj["user"]);
msg.find(".message").text(obj["message"]);
msg.addClass("control");
}
if (obj["user"] == name) msg.find(".user").addClass("self");
container.find("ul").append(msg.show());
container.scrollTop(container.find("ul").innerHeight());
};
$("#channel form").submit(function (event) {
event.preventDefault();
var input = $(this).find(":input");
var msg = input.val();
if (msg) {
ws.send(
JSON.stringify({ action: "message", user: name, message: msg }),
);
}
input.val("");
});
}
});
</script>
<style type="text/css" media="screen">
* {
font-family: Georgia;
}
a {
color: #000;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
div.bordered {
margin: 0 auto;
margin-top: 100px;
width: 600px;
padding: 20px;
text-align: center;
border: 10px solid #ddd;
-webkit-border-radius: 20px;
}
#error {
background-color: #ba0000;
color: #fff;
font-weight: bold;
}
#ask {
font-size: 20pt;
}
#ask input {
font-size: 20pt;
padding: 10px;
margin: 0 10px;
}
#ask span.join {
padding: 10px;
background-color: #ddd;
-webkit-border-radius: 10px;
}
#channel {
margin-top: 100px;
height: 480px;
position: relative;
}
#channel div#descr {
position: absolute;
left: -10px;
top: -190px;
font-size: 13px;
text-align: left;
line-height: 20px;
padding: 5px;
width: 630px;
}
div#msgs {
overflow-y: scroll;
height: 400px;
}
div#msgs ul {
list-style: none;
padding: 0;
margin: 0;
text-align: left;
}
div#msgs li {
line-height: 20px;
}
div#msgs li span.user {
color: #ff9900;
}
div#msgs li span.user.self {
color: #aa2211;
}
div#msgs li span.time {
float: right;
margin-right: 5px;
color: #aaa;
font-family: "Courier New";
font-size: 12px;
}
div#msgs li.control {
text-align: center;
}
div#msgs li.control span.message {
color: #aaa;
}
div#input {
text-align: left;
margin-top: 20px;
}
div#input #message {
width: 600px;
border: 5px solid #bbb;
-webkit-border-radius: 3px;
font-size: 30pt;
}
</style>
</head>
<body>
<div id="error" class="bordered" style="display: none">
This browser has no native WebSocket support.<br />
Use a WebKit nightly or Google Chrome.
</div>
<div id="ask" class="bordered" style="display: none">
Name: <input type="text" id="name" />
<a href="#"><span class="join">Join!</span></a>
</div>
<div id="channel" class="bordered" style="display: none">
<div id="descr" class="bordered">
<strong>Tip:</strong> Open up another browser window to chat.
</div>
<div id="msgs">
<ul>
<li class="message" style="display: none">
<span class="user"></span><span class="message"></span>
<span class="time"></span>
</li>
</ul>
</div>
<div id="input">
<form><input type="text" id="message" /></form>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,52 @@
# Requires: `starlette`, `uvicorn`, `jinja2`
# Run with `uvicorn example:app`
import logging
import os
import anyio
from broadcaster import Broadcast
from fastapi import APIRouter, WebSocket
from fastapi.responses import HTMLResponse
log = logging.getLogger(__name__)
router = APIRouter()
brd = Broadcast("memory://")
@router.get("/ws2_example")
async def get() -> HTMLResponse:
html = open(f"{os.getcwd()}/webui/routers/messenger.html").read()
return HTMLResponse(html)
@router.websocket("/ws2")
async def chatroom_ws(websocket: WebSocket) -> None:
await websocket.accept()
async with anyio.create_task_group() as task_group:
# run until first is complete
async def run_chatroom_ws_receiver() -> None:
await chatroom_ws_receiver(websocket=websocket)
task_group.cancel_scope.cancel()
task_group.start_soon(run_chatroom_ws_receiver)
log.warning("Started chatroom_ws_sender")
await chatroom_ws_sender(websocket)
async def chatroom_ws_receiver(websocket: WebSocket) -> None:
async for message in websocket.iter_text():
log.warning(f"Received message: {message}")
await brd.publish(channel="chatroom", message=message)
async def chatroom_ws_sender(websocket: WebSocket) -> None:
async with brd.subscribe(channel="chatroom") as subscriber:
if subscriber is None:
log.error("Subscriber is None")
return
async for event in subscriber: # type: ignore
await websocket.send_text(event.message)

View File

@@ -36,6 +36,8 @@
, mypy , mypy
, sqlalchemy , sqlalchemy
, websockets , websockets
, deal
, broadcaster
}: }:
let let
@@ -45,6 +47,8 @@ let
uvicorn # optional dependencies: if not enabled, webui subcommand will not work uvicorn # optional dependencies: if not enabled, webui subcommand will not work
sqlalchemy sqlalchemy
websockets websockets
broadcaster
deal
]; ];
pytestDependencies = runtimeDependencies ++ dependencies ++ [ pytestDependencies = runtimeDependencies ++ dependencies ++ [

View File

@@ -1,6 +1,6 @@
{ inputs, ... }: { inputs, ... }:
{ {
perSystem = { self', pkgs, ... }: { perSystem = { self', pkgs, system, ... }: {
devShells.clan-cli = pkgs.callPackage ./shell.nix { devShells.clan-cli = pkgs.callPackage ./shell.nix {
inherit (self'.packages) clan-cli ui-assets nix-unit; inherit (self'.packages) clan-cli ui-assets nix-unit;
}; };
@@ -8,6 +8,8 @@
clan-cli = pkgs.python3.pkgs.callPackage ./default.nix { clan-cli = pkgs.python3.pkgs.callPackage ./default.nix {
inherit (self'.packages) ui-assets; inherit (self'.packages) ui-assets;
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs;
inherit (inputs.nixpkgs-for-iosl.legacyPackages.${system}.python3Packages) deal;
inherit (inputs.nixpkgs-for-iosl.legacyPackages.${system}.python3Packages) broadcaster;
}; };
inherit (self'.packages.clan-cli) clan-openapi; inherit (self'.packages.clan-cli) clan-openapi;
default = self'.packages.clan-cli; default = self'.packages.clan-cli;

View File

@@ -49,6 +49,11 @@ ignore_missing_imports = true
module = "pytest.*" module = "pytest.*"
ignore_missing_imports = true ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "anyio.*"
ignore_missing_imports = true
[[tool.mypy.overrides]] [[tool.mypy.overrides]]
module = "setuptools.*" module = "setuptools.*"
ignore_missing_imports = true ignore_missing_imports = true

View File

@@ -1,43 +1,9 @@
{ stdenv { callPackage }:
, lib let
, nixVersions nix-unit-src = builtins.fetchGit {
, fetchFromGitHub url = "https://github.com/adisbladis/nix-unit";
, nlohmann_json rev = "7e2ee1c70f930b9b65b9fc33c3f3eca0dfae00d1";
, boost
, meson
, pkg-config
, ninja
, cmake
, clang-tools
}:
stdenv.mkDerivation {
pname = "nix-unit";
version = "0.1";
src = fetchFromGitHub {
owner = "adisbladis";
repo = "nix-unit";
rev = "3ed2378bddad85257fc508a291408f9ed9673d01";
sha256 = "sha256-HvMq0TJGYSx37zHm4j2d+JUZx4/6X7xKEt/0DeCiwjQ=";
}; };
buildInputs = [ in
nlohmann_json callPackage nix-unit-src { }
nixVersions.stable
boost
];
nativeBuildInputs = [
meson
pkg-config
ninja
# nlohmann_json can be only discovered via cmake files
cmake
] ++ (lib.optional stdenv.cc.isClang [ clang-tools ]);
meta = {
description = "Nix unit test runner";
homepage = "https://github.com/adisbladis/nix-unit";
license = lib.licenses.gpl3;
maintainers = with lib.maintainers; [ adisbladis ];
platforms = lib.platforms.unix;
};
}