mirror of
https://github.com/skerkour/black-hat-rust
synced 2026-06-08 17:29:34 +00:00
ch_09/phishing: improve routing
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use clap::Arg;
|
||||
use log::info;
|
||||
use std::sync::Arc;
|
||||
use std::{path::Path, sync::Arc};
|
||||
use warp::Filter;
|
||||
|
||||
mod api;
|
||||
@@ -48,7 +48,13 @@ async fn run_server(pool: SqlitePool, port: u16, public_dir: String) -> Result<(
|
||||
info!("Starting server. port={}, directory={}", port, &public_dir);
|
||||
|
||||
let pool = Arc::new(pool);
|
||||
let index_path = Path::new(&public_dir)
|
||||
.join("index.html")
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.unwrap();
|
||||
|
||||
let index = warp::any().and(warp::fs::file(index_path));
|
||||
let files = warp::any().and(warp::fs::dir(public_dir));
|
||||
let login = warp::path("api")
|
||||
.and(warp::path("login"))
|
||||
@@ -57,7 +63,7 @@ async fn run_server(pool: SqlitePool, port: u16, public_dir: String) -> Result<(
|
||||
.and(api::with_db(pool))
|
||||
.and_then(api::login);
|
||||
|
||||
let routes = files.or(login).with(warp::log("server"));
|
||||
let routes = files.or(login).or(index).with(warp::log("server"));
|
||||
warp::serve(routes).run(([0, 0, 0, 0], port)).await;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -57,7 +57,7 @@ impl Component for LoginForm {
|
||||
self.api_task = None;
|
||||
let window: Window = web_sys::window().expect("window not available");
|
||||
let location = window.location();
|
||||
let _ = location.set_href("https://academy.kerkour.com/black-hat-rust");
|
||||
let _ = location.set_href("/error");
|
||||
}
|
||||
Msg::ApiResponse(Err(err)) => {
|
||||
self.error = Some(err);
|
||||
|
||||
@@ -12,6 +12,10 @@ pub use error::Error;
|
||||
#[derive(Switch, Debug, Clone)]
|
||||
pub enum Route {
|
||||
#[to = "*"]
|
||||
Fallback,
|
||||
#[to = "/error"]
|
||||
Error,
|
||||
#[to = "/"]
|
||||
Login,
|
||||
}
|
||||
|
||||
@@ -35,7 +39,8 @@ impl Component for App {
|
||||
|
||||
fn view(&self) -> Html {
|
||||
let render = Router::render(|switch: Route| match switch {
|
||||
Route::Login => html! {<pages::Login/>},
|
||||
Route::Login | Route::Fallback => html! {<pages::Login/>},
|
||||
Route::Error => html! {<pages::Error/>},
|
||||
});
|
||||
|
||||
html! {
|
||||
@@ -46,5 +51,5 @@ impl Component for App {
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn run_app() {
|
||||
yew::App::<pages::Login>::new().mount_to_body();
|
||||
yew::App::<App>::new().mount_to_body();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
use yew::prelude::*;
|
||||
|
||||
pub struct Error {}
|
||||
|
||||
impl Component for Error {
|
||||
type Message = ();
|
||||
type Properties = ();
|
||||
|
||||
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
|
||||
Error {}
|
||||
}
|
||||
|
||||
fn update(&mut self, _: Self::Message) -> ShouldRender {
|
||||
true
|
||||
}
|
||||
|
||||
fn change(&mut self, _props: Self::Properties) -> ShouldRender {
|
||||
false
|
||||
}
|
||||
|
||||
fn view(&self) -> Html {
|
||||
html! {
|
||||
<div>
|
||||
<h1>{ "Error. Please try again later" }</h1>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ pub struct Login {}
|
||||
impl Component for Login {
|
||||
type Message = ();
|
||||
type Properties = ();
|
||||
|
||||
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
|
||||
Login {}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
mod login;
|
||||
pub use login::Login;
|
||||
|
||||
mod error;
|
||||
pub use error::Error;
|
||||
|
||||
Reference in New Issue
Block a user