fix : path to index.html

This commit is contained in:
GreyRav
2026-03-30 12:40:05 +02:00
committed by M1n-0
parent ee415c3d8b
commit 46660bd921
2 changed files with 19 additions and 16 deletions

View File

@@ -1,9 +1,12 @@
use axum::{routing::get, Router};
use axum::{Router, response::Html, routing::get};
use std::net::SocketAddr;
use std::path::Path;
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));
let app = Router::new().route("/", get(handler));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
@@ -12,3 +15,17 @@ async fn main() {
.await
.unwrap();
}
async fn handler() -> Html<String> {
let html_content = read_html_from_file("../web/templates/view/index.html")
.await
.unwrap_or_else(|_| "<h1>Error loading HTML file</h1>".to_string());
Html(html_content)
}
async fn read_html_from_file<P: AsRef<Path>>(path: P) -> io::Result<String> {
let mut file = File::open(path).await?;
let mut contents = String::new();
file.read_to_string(&mut contents).await?;
Ok(contents)
}