zachery.lol/crates/server/bin/main.rs

34 lines
1,023 B
Rust
Raw Normal View History

2025-11-17 22:21:49 +00:00
use axum::{Router, response::Html, routing::get};
use std::{fs::OpenOptions, io::Read};
type Error = Box<dyn std::error::Error>;
type Result<T> = std::result::Result<T, Error>;
#[tokio::main]
async fn main() -> Result<()> {
// build our application with a single route
let app = Router::new()
.route("/", get(serve_path("./routes/root/index.html")?))
.route("/who", get(serve_path("./routes/who/index.html")?))
.route("/contact", get(serve_path("./routes/contact/index.html")?))
// .route("/api/pubkey", get(todo!()));
;
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
Ok(())
}
fn serve_path(path: impl AsRef<std::path::Path>) -> Result<Html<String>> {
let mut s = String::new();
OpenOptions::new()
.read(true)
.write(false)
.open(path)?
.read_to_string(&mut s)?;
Ok(Html(s))
}