Skip to content

Commit

Permalink
Embed the WASM files into the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Nov 27, 2023
1 parent 3dd6009 commit 1991186
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 7 deletions.
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ futures-util = "0.3"
pcap = "1.0.0"
etherparse = "0.13.0"
clap = { version = "4", features = ["derive"] }
rust-embed = "8.0.0"
mime_guess = "2.0.4"

# not using workspaces, as the crates use different targets
14 changes: 10 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::env;
use std::process::Command;

fn main() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();

println!("MANIFEST_DIR: {:?}", manifest_dir);
println!("OUT_DIR: {:?}", out_dir);
// TODO: in theory we shouldn't modify files outside of OUT_DIR
// but oh well
Command::new("trunk")
.args(["build", "--release", "--dist"])
.arg(&format!("{}/client/dist", manifest_dir))
.arg(&format!("{}/client/index.html", manifest_dir))
.status()
.unwrap();

panic!();
println!("cargo:rerun-if-changed=client");
}
34 changes: 31 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use log::{error, info, warn};
use rtpeeker_common::packet::SessionProtocol;
use rtpeeker_common::Source;
use rtpeeker_common::{Request, Response};
use rust_embed::RustEmbed;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{
Expand All @@ -13,12 +14,16 @@ use std::sync::{
use tokio::sync::{mpsc, mpsc::UnboundedSender, RwLock};
use tokio_stream::wrappers::UnboundedReceiverStream;
use warp::ws::{Message, WebSocket};
use warp::Filter;
use warp::{http::header::HeaderValue, path::Tail, reply};
use warp::{Filter, Rejection, Reply};

const DIST_DIR: &str = "client/dist";
const WS_PATH: &str = "ws";
static NEXT_CLIENT_ID: AtomicUsize = AtomicUsize::new(1);

#[derive(RustEmbed)]
#[folder = "client/dist"]
struct Asset;

struct Client {
pub sender: mpsc::UnboundedSender<Message>,
pub source: Option<Source>,
Expand Down Expand Up @@ -80,12 +85,35 @@ pub async fn run(
ws.on_upgrade(move |socket| client_connected(socket, clients_cl, source_to_packets_cl))
});

let routes = ws.or(warp::fs::dir(DIST_DIR));
let index_html = warp::path::end().and_then(serve_index);
let other = warp::path::tail().and_then(serve);

let routes = ws.or(index_html).or(other);
println!("RTPeeker running on http://{}/", addr);

warp::serve(routes).run(addr).await;
}

async fn serve_index() -> Result<impl Reply, Rejection> {
serve_impl("index.html").await
}

async fn serve(path: Tail) -> Result<impl Reply, Rejection> {
serve_impl(path.as_str()).await
}

async fn serve_impl(path: &str) -> Result<impl Reply, Rejection> {
let asset = Asset::get(path).ok_or_else(warp::reject::not_found)?;
let mime = mime_guess::from_path(path).first_or_octet_stream();

let mut res = reply::Response::new(asset.data.into());
res.headers_mut().insert(
"content-type",
HeaderValue::from_str(mime.as_ref()).unwrap(),
);
Ok(res)
}

async fn client_connected(ws: WebSocket, clients: Clients, source_to_packets: PacketsMap) {
let client_id = NEXT_CLIENT_ID.fetch_add(1, Ordering::Relaxed);

Expand Down

0 comments on commit 1991186

Please sign in to comment.