43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
|
use std::{net::SocketAddr, path::PathBuf};
|
||
|
use warp::Filter;
|
||
|
|
||
|
mod redirects;
|
||
|
|
||
|
#[tokio::main]
|
||
|
async fn main() {
|
||
|
// TODO: Can we do some perfect hashing with the static dir?
|
||
|
let static_dir = std::env::var("STATIC_DIR").unwrap_or_else(|_| String::from("./static"));
|
||
|
let static_files = warp::fs::dir(static_dir.clone());
|
||
|
|
||
|
let misc_files_dir = std::env::var("MISC_DIR").unwrap_or_else(|_| String::from("misc"));
|
||
|
let misc_files = warp::fs::dir(misc_files_dir);
|
||
|
|
||
|
let redirect_routes = redirects::routes(&static_dir).await;
|
||
|
|
||
|
let not_found_route = {
|
||
|
let not_found_page = {
|
||
|
let mut path = PathBuf::from(&static_dir);
|
||
|
path.push("404.html");
|
||
|
path
|
||
|
};
|
||
|
|
||
|
warp::fs::file(not_found_page)
|
||
|
.map(|reply| warp::reply::with_status(reply, warp::hyper::StatusCode::NOT_FOUND))
|
||
|
};
|
||
|
|
||
|
let routes = static_files
|
||
|
.or(misc_files)
|
||
|
.or(redirect_routes)
|
||
|
.or(not_found_route);
|
||
|
|
||
|
let host = std::env::var("BIND_ADDR")
|
||
|
.map(|addr| {
|
||
|
addr.parse::<SocketAddr>()
|
||
|
.unwrap_or_else(|_| panic!("{addr} is not a valid socket address"))
|
||
|
})
|
||
|
.unwrap_or_else(|_| ([127, 0, 0, 1], 8080).into());
|
||
|
println!("Listening on http://{host} ...");
|
||
|
|
||
|
warp::serve(routes).bind(host).await;
|
||
|
}
|