simplify the frontend

This commit is contained in:
Jovi Hsu 2021-11-08 20:29:24 +08:00
parent da0819c214
commit fb4c97f95b
6 changed files with 34 additions and 34 deletions

View File

@ -47,8 +47,8 @@ pub async fn target_validate(
} }
} }
#[post("/target/ssh")] #[post("/target/remote")]
pub async fn target_ssh( pub async fn target_remote(
req: HttpRequest, req: HttpRequest,
session: Session, session: Session,
params: web::Json<RemoteInfo>, params: web::Json<RemoteInfo>,

View File

@ -72,7 +72,7 @@ async fn main() -> std::io::Result<()> {
.service(index) .service(index)
.service(user::auth::auth) .service(user::auth::auth)
.service(agent::remote::target_validate) .service(agent::remote::target_validate)
.service(agent::remote::target_ssh) .service(agent::remote::target_remote)
.service(agent::ws::ws_index) .service(agent::ws::ws_index)
.service( .service(
fs::Files::new("/static", STATIC_DIR) fs::Files::new("/static", STATIC_DIR)

View File

@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use crate::components::auth; 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::html::IntoPropValue;
use yew::prelude::*; use yew::prelude::*;
use yew::services::ConsoleService; use yew::services::ConsoleService;
@ -13,8 +13,8 @@ use yew_router::{router::Router, Switch};
enum AppRoute { enum AppRoute {
// #[at("/ssh/:id")] // #[at("/ssh/:id")]
// Ssh(i32), // Ssh(i32),
#[to = "/ssh"] // #[to = "/ssh"]
Ssh, // Ssh,
#[to = "/!"] #[to = "/!"]
Home, Home,
#[to = ""] #[to = ""]
@ -24,7 +24,7 @@ enum AppRoute {
impl From<AppRoute> for &str { impl From<AppRoute> for &str {
fn from(route: AppRoute) -> Self { fn from(route: AppRoute) -> Self {
match route { match route {
AppRoute::Ssh => "/ssh", // AppRoute::Ssh => "/ssh",
_ => "/", _ => "/",
} }
} }
@ -113,9 +113,9 @@ impl App {
<RouterAnchor<AppRoute> classes="navbar-item" route=AppRoute::Home> <RouterAnchor<AppRoute> classes="navbar-item" route=AppRoute::Home>
{ "Home" } { "Home" }
</RouterAnchor<AppRoute>> </RouterAnchor<AppRoute>>
<RouterAnchor<AppRoute> classes="navbar-item" route=AppRoute::Ssh> // <RouterAnchor<AppRoute> classes="navbar-item" route=AppRoute::Ssh>
{ "Ssh" } // { "Ssh" }
</RouterAnchor<AppRoute>> // </RouterAnchor<AppRoute>>
</div> </div>
</nav> </nav>
} }
@ -127,9 +127,9 @@ impl App {
// Route::Ssh(ip) => { // Route::Ssh(ip) => {
// html! { <Ssh /> } // html! { <Ssh /> }
// } // }
AppRoute::Ssh => { // AppRoute::Ssh => {
html! {<PageSsh />} // html! {<PageSsh />}
} // }
AppRoute::Home => { AppRoute::Home => {
html! {<PageHome />} html! {<PageHome />}
} }

View File

@ -1,3 +1,3 @@
pub mod page_home; pub mod page_home;
pub mod page_not_found; pub mod page_not_found;
pub mod page_ssh; pub mod page_remote;

View File

@ -14,7 +14,7 @@ impl Component for PageHome {
fn view(&self) -> Html { fn view(&self) -> Html {
html! { html! {
"Hello world" <crate::pages::page_remote::PageRemote/>
} }
} }

View File

@ -10,7 +10,7 @@ use yew::{
use crate::components; use crate::components;
pub struct PageSsh { pub struct PageRemote {
link: ComponentLink<Self>, link: ComponentLink<Self>,
target: (String, u16), target: (String, u16),
error_msg: String, error_msg: String,
@ -18,19 +18,19 @@ pub struct PageSsh {
connected: bool, connected: bool,
} }
pub enum SshMsg { pub enum RemoteMsg {
SshConnect((String, u16)), Connect((String, u16)),
SshConnectResp(Result<Value, anyhow::Error>), ConnectResp(Result<Value, anyhow::Error>),
SshConnected, Connected,
SshRecv(Vec<u8>), Recv(Vec<u8>),
} }
impl Component for PageSsh { impl Component for PageRemote {
type Message = SshMsg; type Message = RemoteMsg;
type Properties = (); type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self { fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
PageSsh { PageRemote {
link, link,
target: (String::from(""), 0), target: (String::from(""), 0),
error_msg: String::from(""), error_msg: String::from(""),
@ -41,7 +41,7 @@ impl Component for PageSsh {
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg { match msg {
SshMsg::SshConnect(target) => { RemoteMsg::Connect(target) => {
self.target = target; self.target = target;
// ConsoleService::log(&self.target); // ConsoleService::log(&self.target);
let to_post = json!({ let to_post = json!({
@ -50,7 +50,7 @@ impl Component for PageSsh {
}); });
// 1. build the request // 1. build the request
let request = Request::post("/target/ssh") let request = Request::post("/target/remote")
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.body(Json(&to_post)) .body(Json(&to_post))
.expect("Could not build auth request."); .expect("Could not build auth request.");
@ -60,7 +60,7 @@ impl Component for PageSsh {
.callback(|response: Response<Json<Result<Value, anyhow::Error>>>| { .callback(|response: Response<Json<Result<Value, anyhow::Error>>>| {
// ConsoleService::error(&format!("{:?}", response)); // ConsoleService::error(&format!("{:?}", response));
let Json(data) = response.into_body(); let Json(data) = response.into_body();
SshMsg::SshConnectResp(data) RemoteMsg::ConnectResp(data)
}); });
// 3. pass the request and callback to the fetch service // 3. pass the request and callback to the fetch service
let task = FetchService::fetch(request, callback).expect("failed to start request"); let task = FetchService::fetch(request, callback).expect("failed to start request");
@ -68,12 +68,12 @@ impl Component for PageSsh {
self.fetch_task = Some(task); self.fetch_task = Some(task);
true true
} }
SshMsg::SshConnectResp(response) => { RemoteMsg::ConnectResp(response) => {
if let Ok(response) = response { if let Ok(response) = response {
self.error_msg = response["status"].to_string(); self.error_msg = response["status"].to_string();
if "\"success\"" == self.error_msg { if "\"success\"" == self.error_msg {
self.link.send_message(SshMsg::SshConnected); self.link.send_message(RemoteMsg::Connected);
} else { } else {
self.error_msg = response["message"].to_string(); self.error_msg = response["message"].to_string();
} }
@ -85,11 +85,11 @@ impl Component for PageSsh {
self.fetch_task = None; self.fetch_task = None;
true true
} }
SshMsg::SshConnected => { RemoteMsg::Connected => {
self.connected = true; self.connected = true;
true true
} }
SshMsg::SshRecv(v) => { RemoteMsg::Recv(v) => {
self.error_msg = String::from_utf8(v).unwrap(); self.error_msg = String::from_utf8(v).unwrap();
true true
} }
@ -102,15 +102,15 @@ impl Component for PageSsh {
fn view(&self) -> Html { fn view(&self) -> Html {
if !self.connected { if !self.connected {
let connect_ssh = self.link.callback(SshMsg::SshConnect); let connect_remote = self.link.callback(RemoteMsg::Connect);
html! { html! {
<> <>
<components::host::Host onsubmit=connect_ssh/> <components::host::Host onsubmit=connect_remote/>
{self.error_msg.clone()} {self.error_msg.clone()}
</> </>
} }
} else { } else {
let recv_msg = self.link.callback(|v| SshMsg::SshRecv(v)); let recv_msg = self.link.callback(|v| RemoteMsg::Recv(v));
html! { html! {
<> <>
<components::ws::WebsocketCtx <components::ws::WebsocketCtx