Use ServeDir

This commit is contained in:
Mo 2023-02-23 00:42:08 +01:00
parent 99372acf7d
commit 5acfa4556f
2 changed files with 19 additions and 11 deletions

View file

@ -11,12 +11,12 @@ anyhow = "1.0"
askama = { git = "https://github.com/djc/askama.git" } askama = { git = "https://github.com/djc/askama.git" }
askama_axum = { git = "https://github.com/djc/askama.git", package = "askama_axum" } askama_axum = { git = "https://github.com/djc/askama.git", package = "askama_axum" }
axum = { version = "0.6", default-features = false, features = ["http1", "form", "tokio", "macros"] } axum = { version = "0.6", default-features = false, features = ["http1", "form", "tokio", "macros"] }
axum-extra = { version = "0.4", features = ["spa"] }
captcha = { version = "0.0.9", default-features = false } captcha = { version = "0.0.9", default-features = false }
lettre = { version = "0.10", default-features = false, features = ["smtp-transport", "hostname", "rustls-tls", "pool", "builder"] } lettre = { version = "0.10", default-features = false, features = ["smtp-transport", "hostname", "rustls-tls", "pool", "builder"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9" serde_yaml = "0.9"
tokio = { version = "1.25", default-features = false, features = ["macros", "rt"] } tokio = { version = "1.25", default-features = false, features = ["macros", "rt"] }
tower-http = { version = "0.3", features = ["fs"] }
tracing = "0.1" tracing = "0.1"
tracing-appender = "0.2" tracing-appender = "0.2"
tracing-subscriber = "0.3" tracing-subscriber = "0.3"

View file

@ -9,12 +9,18 @@ mod states;
mod templates; mod templates;
use anyhow::Result; use anyhow::Result;
use axum::routing::get; use axum::{
use axum::{Router, Server}; http::StatusCode,
use axum_extra::routing::SpaRouter; routing::{get, get_service, Router},
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; Server,
use std::process; };
use std::sync::Arc; use std::{
future::ready,
net::{IpAddr, Ipv4Addr, SocketAddr},
process,
sync::Arc,
};
use tower_http::services::ServeDir;
use tracing::info; use tracing::info;
async fn init() -> Result<()> { async fn init() -> Result<()> {
@ -41,13 +47,15 @@ async fn init() -> Result<()> {
captcha_solutions, captcha_solutions,
}; };
let static_service =
get_service(ServeDir::new("static")).handle_error(|_| ready(StatusCode::NOT_FOUND));
let routes = Router::new() let routes = Router::new()
.route("/", get(routes::index).post(routes::submit)) .route("/", get(routes::index).post(routes::submit))
.with_state(app_state); .with_state(app_state)
.nest_service("/static", static_service);
let spa = SpaRouter::new(&format!("{}/static", &path_prefix), "static"); let app = Router::new().nest(&path_prefix, routes);
let app = Router::new().nest(&path_prefix, routes).merge(spa);
info!("Starting server"); info!("Starting server");
Server::bind(&socket_address) Server::bind(&socket_address)