This commit is contained in:
Michael Zhang 2023-08-10 18:37:53 -05:00
parent 52a292a4d5
commit 39200524ad
8 changed files with 197 additions and 8 deletions

170
Cargo.lock generated
View file

@ -32,6 +32,55 @@ dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is-terminal",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd"
[[package]]
name = "anstyle-parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c677ab05e09154296dd37acecd46420c17b9713e8366facafa8fc0885167cf4c"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.72"
@ -85,7 +134,7 @@ dependencies = [
"axum-core",
"axum-macros",
"base64",
"bitflags",
"bitflags 1.3.2",
"bytes",
"futures-util",
"http",
@ -167,6 +216,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42"
[[package]]
name = "block-buffer"
version = "0.10.4"
@ -252,6 +307,53 @@ dependencies = [
"half",
]
[[package]]
name = "clap"
version = "4.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c27cdf28c0f604ba3f512b0c9a409f8de8513e4816705deb0498b627e7c3a3fd"
dependencies = [
"clap_builder",
"clap_derive",
"once_cell",
]
[[package]]
name = "clap_builder"
version = "4.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08a9f1ab5e9f01a9b81f202e8562eb9a10de70abf9eaeac1be465c28b75aa4aa"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.28",
]
[[package]]
name = "clap_lex"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
@ -332,6 +434,27 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "errno"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "flate2"
version = "1.0.26"
@ -506,6 +629,7 @@ dependencies = [
"axum",
"chrono",
"ciborium",
"clap",
"dashmap",
"derivative",
"flume",
@ -676,6 +800,17 @@ dependencies = [
"hashbrown 0.12.3",
]
[[package]]
name = "is-terminal"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"hermit-abi",
"rustix",
"windows-sys",
]
[[package]]
name = "itertools"
version = "0.10.5"
@ -712,6 +847,12 @@ version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "linux-raw-sys"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503"
[[package]]
name = "lock_api"
version = "0.4.10"
@ -949,7 +1090,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
"bitflags",
"bitflags 1.3.2",
]
[[package]]
@ -992,6 +1133,19 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.38.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19ed4fa021d81c8392ce04db050a3da9a60299050b7ae1cf482d862b54a7218f"
dependencies = [
"bitflags 2.3.3",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "rustversion"
version = "1.0.14"
@ -1165,6 +1319,12 @@ dependencies = [
"lock_api",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.109"
@ -1434,6 +1594,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "uuid"
version = "1.4.1"

View file

@ -9,6 +9,7 @@ automerge = "0.5.1"
axum = { version = "0.6.20", features = ["http2", "macros", "ws"] }
chrono = { version = "0.4.26", features = ["serde"] }
ciborium = "0.2.1"
clap = { version = "4.3.21", features = ["derive"] }
dashmap = "5.5.0"
derivative = "2.2.0"
flume = "0.10.14"

View file

@ -5,6 +5,7 @@ import * as Automerge from "@automerge/automerge";
import * as uuid from "uuid";
import {} from "libsodium";
import Upload from "./Upload";
import { wsUrl } from "./constants";
const connectionStatusMap = {
[ReadyState.CONNECTING]: "Connecting",
@ -34,7 +35,7 @@ export default function Room({ roomId }) {
sendMessage,
lastJsonMessage,
readyState: newReadyState,
} = useWebSocket("ws://localhost:3100/ws", {
} = useWebSocket(wsUrl, {
share: true,
onOpen: ({}) => {
console.log("Shiet, connected.");

View file

@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from "react";
import * as sodium from "libsodium-wrappers";
import useWebSocket from "react-use-websocket";
import CBOR from "cbor";
import { wsUrl } from "./constants";
export default function Upload({ roomId }) {
const [selectedFile, setSelectedFile] = useState<File | null>(null);
@ -25,7 +26,7 @@ export default function Upload({ roomId }) {
sendMessage,
lastJsonMessage,
readyState: newReadyState,
} = useWebSocket("ws://localhost:3100/ws", {
} = useWebSocket(wsUrl, {
share: true,
onMessage: async (event) => {
const message = CBOR.decode(await event.data.arrayBuffer());

1
client/constants.ts Normal file
View file

@ -0,0 +1 @@
export const wsUrl = `ws://${location.host}/ws`;

View file

@ -3,7 +3,7 @@ pub mod message;
#[macro_use]
extern crate derivative;
use std::{io::Cursor, mem, sync::Arc};
use std::{io::Cursor, mem, net::SocketAddr, sync::Arc};
use automerge::{
sync::{State as SyncState, SyncDoc},
@ -19,6 +19,7 @@ use axum::{
Router,
};
use chrono::Utc;
use clap::Parser;
use dashmap::{DashMap, DashSet};
use flume::r#async::SendSink;
use frontend::frontend;
@ -27,6 +28,12 @@ use message::Message;
use mzlib::axum_error::Result;
use uuid::Uuid;
#[derive(Debug, Parser)]
struct Opt {
#[clap(long, default_value = "3000")]
port: u16,
}
#[derive(Default, Clone)]
struct AppState {
clients: Clients,
@ -60,6 +67,8 @@ impl Room {
#[tokio::main]
async fn main() -> Result<()> {
let opts = Opt::parse();
let state = AppState::default();
let app = Router::new()
@ -67,7 +76,9 @@ async fn main() -> Result<()> {
.fallback(frontend)
.with_state(state);
axum::Server::bind(&"0.0.0.0:3100".parse().unwrap())
let bind_addr: SocketAddr = format!("0.0.0.0:{}", opts.port).parse()?;
println!("Listening on {}...", bind_addr);
axum::Server::bind(&bind_addr)
.serve(app.into_make_service())
.await?;

View file

@ -2,8 +2,6 @@
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "NodeNext",
"jsx": "react-jsx"
}
}

View file

@ -5,4 +5,14 @@ import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react(), wasm(), topLevelAwait()],
server: {
proxy: {
"/ws": {
target: "ws://localhost:3000/ws",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/ws/, ""),
ws: true,
},
},
},
});