From fb4c97f95b2e59afce0d9c023b8e28aad886f9c8 Mon Sep 17 00:00:00 2001 From: Jovi Hsu Date: Mon, 8 Nov 2021 20:29:24 +0800 Subject: [PATCH] simplify the frontend --- backend/src/agent/remote.rs | 4 +- backend/src/main.rs | 2 +- frontend/src/app.rs | 20 +++++----- frontend/src/pages/mod.rs | 2 +- frontend/src/pages/page_home.rs | 2 +- .../src/pages/{page_ssh.rs => page_remote.rs} | 38 +++++++++---------- 6 files changed, 34 insertions(+), 34 deletions(-) rename frontend/src/pages/{page_ssh.rs => page_remote.rs} (80%) diff --git a/backend/src/agent/remote.rs b/backend/src/agent/remote.rs index 0ffa059..6a40095 100644 --- a/backend/src/agent/remote.rs +++ b/backend/src/agent/remote.rs @@ -47,8 +47,8 @@ pub async fn target_validate( } } -#[post("/target/ssh")] -pub async fn target_ssh( +#[post("/target/remote")] +pub async fn target_remote( req: HttpRequest, session: Session, params: web::Json, diff --git a/backend/src/main.rs b/backend/src/main.rs index 20c1079..cd024da 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -72,7 +72,7 @@ async fn main() -> std::io::Result<()> { .service(index) .service(user::auth::auth) .service(agent::remote::target_validate) - .service(agent::remote::target_ssh) + .service(agent::remote::target_remote) .service(agent::ws::ws_index) .service( fs::Files::new("/static", STATIC_DIR) diff --git a/frontend/src/app.rs b/frontend/src/app.rs index ec27cb6..7172cc4 100644 --- a/frontend/src/app.rs +++ b/frontend/src/app.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use crate::components::auth; -use crate::pages::{page_home::PageHome, page_not_found::PageNotFound, page_ssh::PageSsh}; +use crate::pages::{page_home::PageHome, page_not_found::PageNotFound}; use yew::html::IntoPropValue; use yew::prelude::*; use yew::services::ConsoleService; @@ -13,8 +13,8 @@ use yew_router::{router::Router, Switch}; enum AppRoute { // #[at("/ssh/:id")] // Ssh(i32), - #[to = "/ssh"] - Ssh, + // #[to = "/ssh"] + // Ssh, #[to = "/!"] Home, #[to = ""] @@ -24,7 +24,7 @@ enum AppRoute { impl From for &str { fn from(route: AppRoute) -> Self { match route { - AppRoute::Ssh => "/ssh", + // AppRoute::Ssh => "/ssh", _ => "/", } } @@ -113,9 +113,9 @@ impl App { classes="navbar-item" route=AppRoute::Home> { "Home" } > - classes="navbar-item" route=AppRoute::Ssh> - { "Ssh" } - > + // classes="navbar-item" route=AppRoute::Ssh> + // { "Ssh" } + // > } @@ -127,9 +127,9 @@ impl App { // Route::Ssh(ip) => { // html! { } // } - AppRoute::Ssh => { - html! {} - } + // AppRoute::Ssh => { + // html! {} + // } AppRoute::Home => { html! {} } diff --git a/frontend/src/pages/mod.rs b/frontend/src/pages/mod.rs index 5fd035d..58fc0a1 100644 --- a/frontend/src/pages/mod.rs +++ b/frontend/src/pages/mod.rs @@ -1,3 +1,3 @@ pub mod page_home; pub mod page_not_found; -pub mod page_ssh; +pub mod page_remote; diff --git a/frontend/src/pages/page_home.rs b/frontend/src/pages/page_home.rs index d774dac..30c837a 100644 --- a/frontend/src/pages/page_home.rs +++ b/frontend/src/pages/page_home.rs @@ -14,7 +14,7 @@ impl Component for PageHome { fn view(&self) -> Html { html! { - "Hello world" + } } diff --git a/frontend/src/pages/page_ssh.rs b/frontend/src/pages/page_remote.rs similarity index 80% rename from frontend/src/pages/page_ssh.rs rename to frontend/src/pages/page_remote.rs index 0c6b44e..a72e62f 100644 --- a/frontend/src/pages/page_ssh.rs +++ b/frontend/src/pages/page_remote.rs @@ -10,7 +10,7 @@ use yew::{ use crate::components; -pub struct PageSsh { +pub struct PageRemote { link: ComponentLink, target: (String, u16), error_msg: String, @@ -18,19 +18,19 @@ pub struct PageSsh { connected: bool, } -pub enum SshMsg { - SshConnect((String, u16)), - SshConnectResp(Result), - SshConnected, - SshRecv(Vec), +pub enum RemoteMsg { + Connect((String, u16)), + ConnectResp(Result), + Connected, + Recv(Vec), } -impl Component for PageSsh { - type Message = SshMsg; +impl Component for PageRemote { + type Message = RemoteMsg; type Properties = (); fn create(_: Self::Properties, link: ComponentLink) -> Self { - PageSsh { + PageRemote { link, target: (String::from(""), 0), error_msg: String::from(""), @@ -41,7 +41,7 @@ impl Component for PageSsh { fn update(&mut self, msg: Self::Message) -> ShouldRender { match msg { - SshMsg::SshConnect(target) => { + RemoteMsg::Connect(target) => { self.target = target; // ConsoleService::log(&self.target); let to_post = json!({ @@ -50,7 +50,7 @@ impl Component for PageSsh { }); // 1. build the request - let request = Request::post("/target/ssh") + let request = Request::post("/target/remote") .header("Content-Type", "application/json") .body(Json(&to_post)) .expect("Could not build auth request."); @@ -60,7 +60,7 @@ impl Component for PageSsh { .callback(|response: Response>>| { // ConsoleService::error(&format!("{:?}", response)); let Json(data) = response.into_body(); - SshMsg::SshConnectResp(data) + RemoteMsg::ConnectResp(data) }); // 3. pass the request and callback to the fetch service let task = FetchService::fetch(request, callback).expect("failed to start request"); @@ -68,12 +68,12 @@ impl Component for PageSsh { self.fetch_task = Some(task); true } - SshMsg::SshConnectResp(response) => { + RemoteMsg::ConnectResp(response) => { if let Ok(response) = response { self.error_msg = response["status"].to_string(); if "\"success\"" == self.error_msg { - self.link.send_message(SshMsg::SshConnected); + self.link.send_message(RemoteMsg::Connected); } else { self.error_msg = response["message"].to_string(); } @@ -85,11 +85,11 @@ impl Component for PageSsh { self.fetch_task = None; true } - SshMsg::SshConnected => { + RemoteMsg::Connected => { self.connected = true; true } - SshMsg::SshRecv(v) => { + RemoteMsg::Recv(v) => { self.error_msg = String::from_utf8(v).unwrap(); true } @@ -102,15 +102,15 @@ impl Component for PageSsh { fn view(&self) -> Html { if !self.connected { - let connect_ssh = self.link.callback(SshMsg::SshConnect); + let connect_remote = self.link.callback(RemoteMsg::Connect); html! { <> - + {self.error_msg.clone()} } } else { - let recv_msg = self.link.callback(|v| SshMsg::SshRecv(v)); + let recv_msg = self.link.callback(|v| RemoteMsg::Recv(v)); html! { <>