From 94709feb3d7ad14fcfdb602bd3f1f3bb86c5462d Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Mon, 25 Mar 2019 15:46:07 -0400 Subject: [PATCH] refactor: move some logic into WebTermExt trait --- src/main.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 266bb01..fb77c99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,8 @@ const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); mod event; mod terminado; -struct Websocket { +/// Actix WebSocket actor +pub struct Websocket { cons: Option>, hb: Instant, } @@ -172,7 +173,8 @@ impl StreamHandler for Websocket { } } -struct Terminal { +/// Represents a PTY backenActix WebSocket actor.d with attached child +pub struct Terminal { pty_write: Option, child: Option, ws: Addr, @@ -313,6 +315,20 @@ impl Handler for Terminal { } } +/// Trait to extend an [actix_web::App] by serving a web terminal. +pub trait WebTermExt { + /// Serve the websocket for the webterm + fn webterm_socket(self: Self) -> Self; +} + +impl WebTermExt for App<()> { + fn webterm_socket(self: Self) -> Self {; + self.resource("/websocket", |r| { + r.f(|req| ws::start(req, Websocket::new())) + }) + } +} + fn main() { pretty_env_logger::init(); @@ -324,9 +340,7 @@ fn main() { .unwrap() .show_files_listing(), ) - .resource("/websocket", |r| { - r.f(|req| ws::start(req, Websocket::new())) - }) + .webterm_socket() .resource("/", |r| r.f(index)) }) .bind("127.0.0.1:8080")