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")]
pub async fn target_ssh(
#[post("/target/remote")]
pub async fn target_remote(
req: HttpRequest,
session: Session,
params: web::Json<RemoteInfo>,

View File

@ -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)

View File

@ -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<AppRoute> for &str {
fn from(route: AppRoute) -> Self {
match route {
AppRoute::Ssh => "/ssh",
// AppRoute::Ssh => "/ssh",
_ => "/",
}
}
@ -113,9 +113,9 @@ impl App {
<RouterAnchor<AppRoute> classes="navbar-item" route=AppRoute::Home>
{ "Home" }
</RouterAnchor<AppRoute>>
<RouterAnchor<AppRoute> classes="navbar-item" route=AppRoute::Ssh>
{ "Ssh" }
</RouterAnchor<AppRoute>>
// <RouterAnchor<AppRoute> classes="navbar-item" route=AppRoute::Ssh>
// { "Ssh" }
// </RouterAnchor<AppRoute>>
</div>
</nav>
}
@ -127,9 +127,9 @@ impl App {
// Route::Ssh(ip) => {
// html! { <Ssh /> }
// }
AppRoute::Ssh => {
html! {<PageSsh />}
}
// AppRoute::Ssh => {
// html! {<PageSsh />}
// }
AppRoute::Home => {
html! {<PageHome />}
}

View File

@ -1,3 +1,3 @@
pub mod page_home;
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 {
html! {
"Hello world"
<crate::pages::page_remote::PageRemote/>
}
}

View File

@ -10,7 +10,7 @@ use yew::{
use crate::components;
pub struct PageSsh {
pub struct PageRemote {
link: ComponentLink<Self>,
target: (String, u16),
error_msg: String,
@ -18,19 +18,19 @@ pub struct PageSsh {
connected: bool,
}
pub enum SshMsg {
SshConnect((String, u16)),
SshConnectResp(Result<Value, anyhow::Error>),
SshConnected,
SshRecv(Vec<u8>),
pub enum RemoteMsg {
Connect((String, u16)),
ConnectResp(Result<Value, anyhow::Error>),
Connected,
Recv(Vec<u8>),
}
impl Component for PageSsh {
type Message = SshMsg;
impl Component for PageRemote {
type Message = RemoteMsg;
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> 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<Json<Result<Value, anyhow::Error>>>| {
// 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! {
<>
<components::host::Host onsubmit=connect_ssh/>
<components::host::Host onsubmit=connect_remote/>
{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! {
<>
<components::ws::WebsocketCtx