refactor: make ui endpoint configurable
* use askama template for UI endpoint * make UI endpoint configurable
This commit is contained in:
+22
@@ -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<F>(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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+2
-6
@@ -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<NamedFile> {
|
||||
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()
|
||||
|
||||
@@ -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<S>(websocket_path: S, static_path: S) -> Self
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
Self {
|
||||
websocket_path: websocket_path.into(),
|
||||
static_path: static_path.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user