refactor: move some logic into WebTermExt trait

This commit is contained in:
Fabian Freyer 2019-03-25 15:46:07 -04:00
parent d6f1eb671c
commit 94709feb3d

View File

@ -59,7 +59,8 @@ const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
mod event; mod event;
mod terminado; mod terminado;
struct Websocket { /// Actix WebSocket actor
pub struct Websocket {
cons: Option<Addr<Terminal>>, cons: Option<Addr<Terminal>>,
hb: Instant, hb: Instant,
} }
@ -172,7 +173,8 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for Websocket {
} }
} }
struct Terminal { /// Represents a PTY backenActix WebSocket actor.d with attached child
pub struct Terminal {
pty_write: Option<AsyncPtyMasterWriteHalf>, pty_write: Option<AsyncPtyMasterWriteHalf>,
child: Option<Child>, child: Option<Child>,
ws: Addr<Websocket>, ws: Addr<Websocket>,
@ -313,6 +315,20 @@ impl Handler<event::TerminadoMessage> 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() { fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
@ -324,9 +340,7 @@ fn main() {
.unwrap() .unwrap()
.show_files_listing(), .show_files_listing(),
) )
.resource("/websocket", |r| { .webterm_socket()
r.f(|req| ws::start(req, Websocket::new()))
})
.resource("/", |r| r.f(index)) .resource("/", |r| r.f(index))
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")