server: Remove SpaRouter and implement similar

In order to serve several single page applications reimplement the
`SpaRouter` without a fallback.
This commit is contained in:
finga 2022-09-20 16:20:45 +02:00
parent b12b8765a9
commit 73d3d739e0
3 changed files with 34 additions and 31 deletions

28
Cargo.lock generated
View file

@ -127,25 +127,6 @@ dependencies = [
"tower-service", "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]] [[package]]
name = "base64" name = "base64"
version = "0.13.0" version = "0.13.0"
@ -596,9 +577,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.132" version = "0.2.133"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -616,7 +597,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"axum", "axum",
"axum-extra",
"clap", "clap",
"log", "log",
"tokio", "tokio",
@ -945,9 +925,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.99" version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" checksum = "52205623b1b0f064a4e71182c3b18ae902267282930c6d5462c91b859668426e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -13,7 +13,6 @@ categories = ["web-programming::http-server"]
anyhow = "1" anyhow = "1"
log = "0.4" log = "0.4"
axum = "0.5" axum = "0.5"
axum-extra = { version = "0.3", features = ["spa"] }
tokio = { version = "1.0", features = ["full"] } tokio = { version = "1.0", features = ["full"] }
tracing-subscriber = "0.3" tracing-subscriber = "0.3"
tower = "0.4" tower = "0.4"

View file

@ -1,14 +1,22 @@
use anyhow::Result; use anyhow::Result;
use axum::Router; use axum::{
use axum_extra::routing::SpaRouter; http::StatusCode,
response::IntoResponse,
routing::get_service,
Router,
};
use clap::{ArgAction, Parser}; use clap::{ArgAction, Parser};
use log::info; use log::{error, info};
use std::{ use std::{
env, env, io,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
path::PathBuf,
}; };
use tower::ServiceBuilder; use tower::ServiceBuilder;
use tower_http::trace::TraceLayer; use tower_http::{
services::{ServeDir, ServeFile},
trace::TraceLayer,
};
#[derive(Parser)] #[derive(Parser)]
#[clap(about, author, version)] #[clap(about, author, version)]
@ -27,6 +35,12 @@ struct Cli {
verbosity: u8, 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] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
@ -38,7 +52,17 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let app = Router::new() 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())); .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()));
let addr = SocketAddr::from((cli.address, cli.port)); let addr = SocketAddr::from((cli.address, cli.port));