initial commit
This commit is contained in:
commit
a1c91f6d4e
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
*~
|
||||
.DS_Store
|
||||
*.swp
|
||||
/target
|
1249
Cargo.lock
generated
Normal file
1249
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
Cargo.toml
Normal file
16
Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "woweb-poc"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.6.7", features = ["ws", "headers"] }
|
||||
axum-extra = { version = "0.5.0", features = ["spa"] }
|
||||
futures = "0.3.26"
|
||||
headers = "0.3.8"
|
||||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
serde_json = "1.0.93"
|
||||
tokio = { version = "1.25.0", features = ["full"] }
|
||||
tower-http = { version = "0.3.5", features = ["trace"] }
|
||||
tracing = "0.1.37"
|
||||
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
|
25
Dockerfile
Normal file
25
Dockerfile
Normal file
@ -0,0 +1,25 @@
|
||||
FROM docker.io/rust:1.67-alpine3.17 AS builder
|
||||
|
||||
RUN apk add --no-cache \
|
||||
musl-dev
|
||||
|
||||
ADD . /src
|
||||
WORKDIR /src
|
||||
|
||||
RUN cargo build --release
|
||||
RUN strip target/release/woweb-poc
|
||||
|
||||
RUN adduser -D woweb
|
||||
|
||||
|
||||
FROM scratch
|
||||
|
||||
COPY --from=builder /etc/passwd /etc/passwd
|
||||
COPY --from=builder /src/target/release/woweb-poc "/opt/woweb-poc"
|
||||
COPY --from=builder /src/assets "/opt/assets"
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
WORKDIR /opt
|
||||
USER woweb
|
||||
CMD ["/opt/woweb-poc", "0.0.0.0:3000"]
|
14
assets/index.html
Normal file
14
assets/index.html
Normal file
@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>CRDT Playground</title>
|
||||
</head>
|
||||
<body onload="setup()">
|
||||
<textarea id="doctext" name="story" rows="5" cols="33" oninput="" >
|
||||
It was a dark and stormy night...
|
||||
</textarea>
|
||||
|
||||
<script src="/index.js"></script>
|
||||
</body>
|
||||
</html>
|
66
assets/index.js
Normal file
66
assets/index.js
Normal file
@ -0,0 +1,66 @@
|
||||
const areaId = "doctext";
|
||||
const validEvents = [
|
||||
"insertText",
|
||||
"insertFromPaste",
|
||||
"insertLineBreak",
|
||||
"deleteContentBackward",
|
||||
"deleteContentForward",
|
||||
];
|
||||
|
||||
let selectionStart = 0;
|
||||
let selectionEnd = 0;
|
||||
|
||||
let area;
|
||||
let ws;
|
||||
|
||||
const uuid = self.crypto.randomUUID();
|
||||
|
||||
function setup() {
|
||||
area = document.querySelector(`#${areaId}`);
|
||||
ws = new WebSocket("ws://localhost:3000/ws");
|
||||
|
||||
setupUi();
|
||||
setupWs();
|
||||
}
|
||||
|
||||
function setupUi() {
|
||||
document.addEventListener("selectionchange", onSelectionChange, false);
|
||||
area.addEventListener("input", onInput, false);
|
||||
}
|
||||
|
||||
function setupWs() {
|
||||
ws.onmessage = function (e) {
|
||||
let payload = JSON.parse(e.data);
|
||||
if (payload.client !== uuid) {
|
||||
area.value = payload.doc;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function onSelectionChange(event) {
|
||||
const activeElement = document.activeElement;
|
||||
|
||||
if (activeElement && activeElement.id === areaId) {
|
||||
selectionStart = area.selectionStart;
|
||||
selectionEnd = area.selectionEnd;
|
||||
}
|
||||
}
|
||||
|
||||
function onInput(event) {
|
||||
if (!validEvents.includes(event.inputType)) return;
|
||||
const payload = {
|
||||
client: uuid,
|
||||
time: performance.now(),
|
||||
action: event.inputType,
|
||||
data: event.data,
|
||||
start: selectionStart,
|
||||
end: selectionEnd,
|
||||
};
|
||||
|
||||
ws.send(JSON.stringify(payload));
|
||||
|
||||
console.log(event.inputType);
|
||||
console.log(selectionStart);
|
||||
console.log(selectionEnd);
|
||||
console.log(event.data);
|
||||
}
|
47
src/action.rs
Normal file
47
src/action.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::actions::delete::{DeleteContentBackward, DeleteContentForward};
|
||||
use crate::actions::insert::InsertText;
|
||||
use crate::actions::linebreak::InsertLineBreak;
|
||||
use crate::actions::paste::InsertFromPaste;
|
||||
use crate::actions::ActionRunner;
|
||||
use crate::AppState;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum ActionType {
|
||||
InsertText,
|
||||
InsertFromPaste,
|
||||
InsertLineBreak,
|
||||
DeleteContentBackward,
|
||||
DeleteContentForward,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub(crate) struct Action {
|
||||
pub(crate) client: String,
|
||||
time: u64,
|
||||
data: Option<String>,
|
||||
action: ActionType,
|
||||
start: usize,
|
||||
end: usize,
|
||||
}
|
||||
|
||||
impl Action {
|
||||
pub(crate) async fn execute(&self, state: Arc<AppState>) {
|
||||
{
|
||||
let mut doc = state.doc.write().await;
|
||||
let action: Box<dyn ActionRunner> = match self.action {
|
||||
ActionType::InsertText => Box::new(InsertText),
|
||||
ActionType::InsertFromPaste => Box::new(InsertFromPaste),
|
||||
ActionType::InsertLineBreak => Box::new(InsertLineBreak),
|
||||
ActionType::DeleteContentBackward => Box::new(DeleteContentBackward),
|
||||
ActionType::DeleteContentForward => Box::new(DeleteContentForward),
|
||||
};
|
||||
|
||||
*doc = action.run(self.start, self.end, self.data.clone(), doc.clone());
|
||||
}
|
||||
}
|
||||
}
|
22
src/actions/delete.rs
Normal file
22
src/actions/delete.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use super::ActionRunner;
|
||||
|
||||
pub(crate) struct DeleteContentBackward;
|
||||
pub(crate) struct DeleteContentForward;
|
||||
|
||||
impl ActionRunner for DeleteContentBackward {
|
||||
fn run(&self, start: usize, _end: usize, _data: Option<String>, mut doc: String) -> String {
|
||||
if start > 0 {
|
||||
doc.remove(start - 1);
|
||||
}
|
||||
doc
|
||||
}
|
||||
}
|
||||
|
||||
impl ActionRunner for DeleteContentForward {
|
||||
fn run(&self, start: usize, _end: usize, _data: Option<String>, mut doc: String) -> String {
|
||||
if doc.len() > start {
|
||||
doc.remove(start);
|
||||
}
|
||||
doc
|
||||
}
|
||||
}
|
18
src/actions/insert.rs
Normal file
18
src/actions/insert.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use super::ActionRunner;
|
||||
|
||||
pub(crate) struct InsertText;
|
||||
|
||||
impl ActionRunner for InsertText {
|
||||
fn run(&self, start: usize, end: usize, data: Option<String>, mut doc: String) -> String {
|
||||
if doc.len() < start || data.is_none() {
|
||||
return doc;
|
||||
}
|
||||
|
||||
if start < end {
|
||||
doc.replace_range(start..end, "");
|
||||
}
|
||||
|
||||
doc.insert_str(start, &data.unwrap());
|
||||
doc
|
||||
}
|
||||
}
|
12
src/actions/linebreak.rs
Normal file
12
src/actions/linebreak.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use super::ActionRunner;
|
||||
|
||||
pub(crate) struct InsertLineBreak;
|
||||
|
||||
impl ActionRunner for InsertLineBreak {
|
||||
fn run(&self, start: usize, _end: usize, _data: Option<String>, mut doc: String) -> String {
|
||||
if doc.len() >= start {
|
||||
doc.insert(start, '\n');
|
||||
}
|
||||
doc
|
||||
}
|
||||
}
|
8
src/actions/mod.rs
Normal file
8
src/actions/mod.rs
Normal file
@ -0,0 +1,8 @@
|
||||
pub(crate) mod delete;
|
||||
pub(crate) mod insert;
|
||||
pub(crate) mod linebreak;
|
||||
pub(crate) mod paste;
|
||||
|
||||
pub(crate) trait ActionRunner {
|
||||
fn run(&self, start: usize, end: usize, data: Option<String>, doc: String) -> String;
|
||||
}
|
18
src/actions/paste.rs
Normal file
18
src/actions/paste.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use super::ActionRunner;
|
||||
|
||||
pub(crate) struct InsertFromPaste;
|
||||
|
||||
impl ActionRunner for InsertFromPaste {
|
||||
fn run(&self, start: usize, end: usize, data: Option<String>, mut doc: String) -> String {
|
||||
if doc.len() < start || data.is_none() {
|
||||
return doc;
|
||||
}
|
||||
|
||||
if start < end {
|
||||
doc.replace_range(start..end, "");
|
||||
}
|
||||
|
||||
doc.insert_str(start, &data.unwrap());
|
||||
doc
|
||||
}
|
||||
}
|
57
src/main.rs
Normal file
57
src/main.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
use axum_extra::routing::SpaRouter;
|
||||
use std::env;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{broadcast, RwLock};
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod action;
|
||||
mod actions;
|
||||
mod ws;
|
||||
|
||||
struct AppState {
|
||||
doc: RwLock<String>,
|
||||
tx: broadcast::Sender<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "woweb_poc=debug,tower_http=debug".into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let (tx, _rx) = broadcast::channel(100);
|
||||
let app_state = Arc::new(AppState {
|
||||
doc: RwLock::new("".to_string()),
|
||||
tx,
|
||||
});
|
||||
|
||||
let app = Router::new()
|
||||
.merge(SpaRouter::new("/", "assets").index_file("index.html"))
|
||||
.route("/ws", get(ws::route))
|
||||
.with_state(app_state)
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let host = if args.len() > 1 {
|
||||
&args[1]
|
||||
} else {
|
||||
"127.0.0.1:3000"
|
||||
};
|
||||
let addr = host
|
||||
.parse()
|
||||
.unwrap_or_else(|_| SocketAddr::from(([127, 0, 0, 1], 3000)));
|
||||
tracing::debug!("listening on {}", addr);
|
||||
|
||||
axum::Server::bind(&addr)
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
77
src/ws.rs
Normal file
77
src/ws.rs
Normal file
@ -0,0 +1,77 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{
|
||||
ws::{Message, WebSocket, WebSocketUpgrade},
|
||||
State,
|
||||
},
|
||||
response::IntoResponse,
|
||||
};
|
||||
|
||||
use futures::{sink::SinkExt, stream::StreamExt};
|
||||
|
||||
use crate::{action::Action, AppState};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct NewState {
|
||||
client: String,
|
||||
doc: String,
|
||||
}
|
||||
|
||||
pub(crate) async fn route(
|
||||
ws: WebSocketUpgrade,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> impl IntoResponse {
|
||||
ws.on_upgrade(move |socket| handle_socket(socket, state))
|
||||
}
|
||||
|
||||
async fn handle_socket(stream: WebSocket, state: Arc<AppState>) {
|
||||
let (mut sender, mut receiver) = stream.split();
|
||||
|
||||
{
|
||||
let doc = state.doc.read().await;
|
||||
let doc: String = doc.clone();
|
||||
let new_state = NewState {
|
||||
client: String::from("0000"),
|
||||
doc: doc.to_string(),
|
||||
};
|
||||
let payload = serde_json::to_string(&new_state).unwrap();
|
||||
let _ = sender.send(Message::Text(payload)).await;
|
||||
}
|
||||
|
||||
let mut rx = state.tx.subscribe();
|
||||
|
||||
let mut send_task = tokio::spawn(async move {
|
||||
while let Ok(msg) = rx.recv().await {
|
||||
if sender.send(Message::Text(msg)).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let tx = state.tx.clone();
|
||||
let state = state.clone();
|
||||
let mut recv_task = tokio::spawn(async move {
|
||||
while let Some(Ok(Message::Text(text))) = receiver.next().await {
|
||||
if let Ok(action) = serde_json::from_str::<Action>(&text) {
|
||||
let client = action.client.clone();
|
||||
action.execute(state.clone()).await;
|
||||
|
||||
let doc = state.doc.read().await;
|
||||
|
||||
let new_state = NewState {
|
||||
client,
|
||||
doc: doc.to_string(),
|
||||
};
|
||||
let payload = serde_json::to_string(&new_state).unwrap();
|
||||
let _ = tx.send(payload);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tokio::select! {
|
||||
_ = (&mut send_task) => recv_task.abort(),
|
||||
_ = (&mut recv_task) => send_task.abort(),
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user