From d897bfe58aa32d0d93bb695a19fb05d274a0961a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 15 Nov 2019 14:03:54 -0500 Subject: serve the teleterm-web content --- Cargo.lock | 18 ++++++++++++++++++ static/index.html | 12 ++++++++++++ teleterm/Cargo.toml | 1 + teleterm/src/web.rs | 37 +++++++++++++++++++++++++++++++++---- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 static/index.html diff --git a/Cargo.lock b/Cargo.lock index b74188c..9a84eae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -981,6 +981,17 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "lazy-static-include" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd80064440e1d2d057f602460cbf24313e8939e3755aa4ed888bae9a2f582201" +dependencies = [ + "lazy_static", + "starts-ends-with-caseless", + "syn 1.0.7", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -1929,6 +1940,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "starts-ends-with-caseless" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "423e8765e5ec86a05cf18aa54cf7fcb53fbf460f5e586c99386e4ae2647ea5b1" + [[package]] name = "string" version = "0.2.1" @@ -1999,6 +2016,7 @@ dependencies = [ "futures", "gotham", "hyper", + "lazy-static-include", "lazy_static", "log", "mio", diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..58a7c36 --- /dev/null +++ b/static/index.html @@ -0,0 +1,12 @@ + + + + + + +

it's a page

+ + diff --git a/teleterm/Cargo.toml b/teleterm/Cargo.toml index f1e0ed0..0d28cb5 100644 --- a/teleterm/Cargo.toml +++ b/teleterm/Cargo.toml @@ -25,6 +25,7 @@ futures = "0.1.29" gotham = { git = "https://github.com/gotham-rs/gotham", rev = "d2395926b93710832f8d72b49c9bd3e77516e386" } hyper = "0.12" lazy_static = "1" +lazy-static-include = "2" log = { version = "0.4", features = ["release_max_level_info"] } mio = "0.6.19" native-tls = "0.2" diff --git a/teleterm/src/web.rs b/teleterm/src/web.rs index afc5128..5159233 100644 --- a/teleterm/src/web.rs +++ b/teleterm/src/web.rs @@ -3,17 +3,46 @@ mod ws; use futures::{Future as _, Sink as _, Stream as _}; use gotham::router::builder::{DefineSingleRoute as _, DrawRoutes as _}; use gotham::state::FromState as _; +use lazy_static::lazy_static; +use lazy_static_include::*; + +lazy_static_include::lazy_static_include_bytes!( + INDEX_HTML, + "../static/index.html" +); +lazy_static_include::lazy_static_include_bytes!( + TELETERM_WEB_JS, + "../target/wasm/teleterm_web.js" +); +lazy_static_include::lazy_static_include_bytes!( + TELETERM_WEB_WASM, + "../target/wasm/teleterm_web_bg.wasm" +); pub fn router() -> impl gotham::handler::NewHandler { gotham::router::builder::build_simple_router(|route| { - route.get("/").to(root); + route.get("/").to(serve_static("text/html", &INDEX_HTML)); + route + .get("/teleterm_web.js") + .to(serve_static("application/javascript", &TELETERM_WEB_JS)); + route + .get("/teleterm_web_bg.wasm") + .to(serve_static("application/wasm", &TELETERM_WEB_WASM)); route.get("/ws").to(handle_websocket_connection); }) } -fn root(state: gotham::state::State) -> (gotham::state::State, String) { - log::info!("request for /"); - (state, "hello world".to_string()) +fn serve_static( + content_type: &'static str, + s: &'static [u8], +) -> impl gotham::handler::Handler + Copy { + move |state| { + let response = hyper::Response::builder() + .header("Content-Type", content_type) + .body(hyper::Body::from(s)) + .unwrap(); + (state, response) + } } fn handle_websocket_connection( -- cgit v1.2.3-54-g00ecf