From 73d3d739e0e3258223a262af2d829fb67c1e0f51 Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 20 Sep 2022 16:20:45 +0200 Subject: [PATCH] server: Remove `SpaRouter` and implement similar In order to serve several single page applications reimplement the `SpaRouter` without a fallback. --- Cargo.lock | 28 ++++------------------------ lockwatch/Cargo.toml | 1 - lockwatch/src/main.rs | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8518a73..011c3e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,25 +127,6 @@ dependencies = [ "tower-service", ] -[[package]] -name = "axum-extra" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69034b3b0fd97923eee2ce8a47540edb21e07f48f87f67d44bb4271cec622bdb" -dependencies = [ - "axum", - "bytes", - "futures-util", - "http", - "mime", - "pin-project-lite", - "tokio", - "tower", - "tower-http", - "tower-layer", - "tower-service", -] - [[package]] name = "base64" version = "0.13.0" @@ -596,9 +577,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.132" +version = "0.2.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" +checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966" [[package]] name = "lock_api" @@ -616,7 +597,6 @@ version = "0.1.0" dependencies = [ "anyhow", "axum", - "axum-extra", "clap", "log", "tokio", @@ -945,9 +925,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.99" +version = "1.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +checksum = "52205623b1b0f064a4e71182c3b18ae902267282930c6d5462c91b859668426e" dependencies = [ "proc-macro2", "quote", diff --git a/lockwatch/Cargo.toml b/lockwatch/Cargo.toml index 2d470b4..9fc2534 100644 --- a/lockwatch/Cargo.toml +++ b/lockwatch/Cargo.toml @@ -13,7 +13,6 @@ categories = ["web-programming::http-server"] anyhow = "1" log = "0.4" axum = "0.5" -axum-extra = { version = "0.3", features = ["spa"] } tokio = { version = "1.0", features = ["full"] } tracing-subscriber = "0.3" tower = "0.4" diff --git a/lockwatch/src/main.rs b/lockwatch/src/main.rs index e3bd0f0..6b2538a 100644 --- a/lockwatch/src/main.rs +++ b/lockwatch/src/main.rs @@ -1,14 +1,22 @@ use anyhow::Result; -use axum::Router; -use axum_extra::routing::SpaRouter; +use axum::{ + http::StatusCode, + response::IntoResponse, + routing::get_service, + Router, +}; use clap::{ArgAction, Parser}; -use log::info; +use log::{error, info}; use std::{ - env, + env, io, net::{IpAddr, SocketAddr}, + path::PathBuf, }; use tower::ServiceBuilder; -use tower_http::trace::TraceLayer; +use tower_http::{ + services::{ServeDir, ServeFile}, + trace::TraceLayer, +}; #[derive(Parser)] #[clap(about, author, version)] @@ -27,6 +35,12 @@ struct Cli { verbosity: u8, } +#[allow(clippy::unused_async)] +async fn file_error(err: io::Error) -> impl IntoResponse { + error!("could not read from file: {}", err); + (StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong...") +} + #[tokio::main] async fn main() -> Result<()> { let cli = Cli::parse(); @@ -38,7 +52,17 @@ async fn main() -> Result<()> { tracing_subscriber::fmt::init(); let app = Router::new() - .merge(SpaRouter::new("/client", cli.static_dir)) + .nest( + "/client", + get_service(ServeDir::new(&cli.static_dir)).handle_error(file_error), + ) + .route( + "/", + get_service(ServeFile::new( + &PathBuf::from(&cli.static_dir).join("index.html"), + )) + .handle_error(file_error), + ) .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http())); let addr = SocketAddr::from((cli.address, cli.port));