From 09f8b4ffd7f8dc452e98cf53d380b3805480ed96 Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Fri, 29 Mar 2019 12:51:42 -0400 Subject: [PATCH] refactor: make ui endpoint configurable * use askama template for UI endpoint * make UI endpoint configurable --- Cargo.lock | 73 +++++++++++++++++++++++++++++++++ Cargo.toml | 11 ++--- src/lib.rs | 22 ++++++++++ src/server.rs | 8 +--- src/templates.rs | 20 +++++++++ {static => templates}/term.html | 14 +++---- 6 files changed, 130 insertions(+), 18 deletions(-) create mode 100644 src/templates.rs rename {static => templates}/term.html (64%) diff --git a/Cargo.lock b/Cargo.lock index f0bb7d8..cf79a62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,6 +141,48 @@ dependencies = [ "nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "askama" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "actix-web 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)", + "askama_derive 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "askama_escape 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "askama_shared 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "mime_guess 2.0.0-alpha.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "askama_derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "askama_shared 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "askama_escape" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "askama_shared" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "askama_escape 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "atty" version = "0.2.11" @@ -532,6 +574,11 @@ name = "httparse" version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "humansize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "humantime" version = "1.2.0" @@ -1101,6 +1148,16 @@ name = "serde" version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde_derive" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_json" version = "1.0.39" @@ -1436,6 +1493,14 @@ dependencies = [ "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "toml" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tower-service" version = "0.1.0" @@ -1619,6 +1684,7 @@ version = "0.1.0" dependencies = [ "actix 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)", + "askama 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1716,6 +1782,10 @@ dependencies = [ "checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" "checksum arc-swap 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c79e383ce3e5b88b123589fe774221be2240a9936866f4f2286cbfe555ef36e8" "checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" +"checksum askama 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3dc2a4b6d7f812d2b13d251ae792caecebd635d6401761162d4b71d5ebe1a010" +"checksum askama_derive 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ee2fff0f22ad5d215cace1227cd036c28e81e26206763bb837b6d0e766c87d" +"checksum askama_escape 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0de942230b5beedaa9e1d64df5b76fa1c97002e4c7982897be899cccf40621d" +"checksum askama_shared 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a6dfa6b6d254fd066a8bbed9a8f913123e3f701db89216ad4f0aff04ad87718c" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" "checksum backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "cd5a90e2b463010cd0e0ce9a11d4a9d5d58d9f41d4a6ba3dcaf9e68b466e88b4" @@ -1763,6 +1833,7 @@ dependencies = [ "checksum hostname 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "21ceb46a83a85e824ef93669c8b390009623863b5c195d1ba747292c0c72f94e" "checksum http 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "fe67e3678f2827030e89cc4b9e7ecd16d52f132c0b940ab5005f88e821500f6a" "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83" +"checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" @@ -1833,6 +1904,7 @@ dependencies = [ "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "92514fb95f900c9b5126e32d020f5c6d40564c27a5ea6d1d7d9f157a96623560" +"checksum serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6eabf4b5914e88e24eea240bb7c9f9a2cbc1bbbe8d961d381975ec3c6b806c" "checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d" "checksum serde_urlencoded 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d48f9f99cd749a2de71d29da5f948de7f2764cc5a9d7f3c97e3514d4ee6eabf2" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" @@ -1864,6 +1936,7 @@ dependencies = [ "checksum tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "350c9edade9830dc185ae48ba45667a445ab59f6167ef6d0254ec9d2430d9dd3" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" +"checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum tower-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b32f72af77f1bfe3d3d4da8516a238ebe7039b51dd8637a09841ac7f16d2c987" "checksum trust-dns-proto 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0838272e89f1c693b4df38dc353412e389cf548ceed6f9fd1af5a8d6e0e7cf74" "checksum trust-dns-proto 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "09144f0992b0870fa8d2972cc069cbf1e3c0fda64d1f3d45c4d68d0e0b52ad4e" diff --git a/Cargo.toml b/Cargo.toml index 9b23db7..083269c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,18 +6,19 @@ edition = "2018" license = "BSD-3-Clause" [dependencies] -actix= "0.7.9" actix-web= "0.7.19" +actix= "0.7.9" +askama = { version = "0.8.0", features= ["with-actix-web"] } +futures = "0.1.25" libc = "0.2.51" log = "0.4.6" pretty_env_logger = "0.3.0" serde = "1.0.89" serde_json = "1.0.39" tokio = "0.1.18" -tokio-pty-process = "*" -tokio-io= "0.1.12" tokio-codec= "0.1.1" -futures = "0.1.25" +tokio-io= "0.1.12" +tokio-pty-process = "*" [patch.crates-io] tokio-pty-process = {path = "vendor/stund/tokio-pty-process"} @@ -28,4 +29,4 @@ path = "src/lib.rs" [[bin]] name = "webterm-server" -path = "src/server.rs" \ No newline at end of file +path = "src/server.rs" diff --git a/src/lib.rs b/src/lib.rs index 4ba0eb7..7c41af7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ extern crate actix; extern crate actix_web; +extern crate askama; extern crate futures; extern crate libc; extern crate serde; @@ -40,6 +41,7 @@ extern crate tokio_pty_process; #[macro_use] extern crate log; extern crate pretty_env_logger; +use askama::actix_web::TemplateIntoResponse; use actix::*; use actix_web::{ws, App}; @@ -57,6 +59,7 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); mod event; +pub mod templates; mod terminado; /// Actix WebSocket actor @@ -339,6 +342,13 @@ pub trait WebTermExt { fn webterm_socket(self: Self, endpoint: &str, handler: F) -> Self where F: Fn(&actix_web::Request) -> Command + 'static; + + fn webterm_ui( + self: Self, + endpoint: &str, + webterm_socket_endpoint: &str, + static_path: &str, + ) -> Self; } impl WebTermExt for App<()> { @@ -350,4 +360,16 @@ impl WebTermExt for App<()> { r.f(move |req| ws::start(req, Websocket::new(handler(req)))) }) } + + fn webterm_ui( + self: Self, + endpoint: &str, + webterm_socket_endpoint: &str, + static_path: &str, + ) -> Self { + let template = templates::WebTerm::new(webterm_socket_endpoint, static_path); + self.resource(endpoint, move |r| { + r.get().f(move |_| template.into_response()) + }) + } } diff --git a/src/server.rs b/src/server.rs index 31bd970..fb0d5d0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -2,15 +2,11 @@ extern crate actix; extern crate actix_web; extern crate webterm; -use actix_web::{fs::NamedFile, fs::StaticFiles, server, App, HttpRequest, Result}; +use actix_web::{fs::StaticFiles, server, App}; use webterm::WebTermExt; use std::process::Command; -fn index(_req: &HttpRequest) -> Result { - Ok(NamedFile::open("static/term.html")?) -} - fn main() { pretty_env_logger::init(); @@ -27,7 +23,7 @@ fn main() { cmd.env("TERM", "xterm"); cmd }) - .resource("/", |r| r.f(index)) + .webterm_ui("/", "/websocket", "/static") }) .bind("127.0.0.1:8080") .unwrap() diff --git a/src/templates.rs b/src/templates.rs new file mode 100644 index 0000000..089f807 --- /dev/null +++ b/src/templates.rs @@ -0,0 +1,20 @@ +use askama::Template; + +#[derive(Template)] +#[template(path = "term.html")] +pub struct WebTerm { + websocket_path: String, + static_path: String, +} + +impl WebTerm { + pub fn new(websocket_path: S, static_path: S) -> Self + where + S: Into, + { + Self { + websocket_path: websocket_path.into(), + static_path: static_path.into(), + } + } +} diff --git a/static/term.html b/templates/term.html similarity index 64% rename from static/term.html rename to templates/term.html index 84d3c8c..8267f28 100644 --- a/static/term.html +++ b/templates/term.html @@ -7,12 +7,12 @@ SPDX-License-Identifier: BSD-3-Clause - - - - - - + + + + + +