Files
Projet_48h/backend/src/main.rs
2026-03-31 14:11:46 +02:00

35 lines
1011 B
Rust

use axum::{Router, response::Html, routing::get};
use std::net::SocketAddr;
use std::path::Path;
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
use tower_http::services::ServeDir;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(handler))
.nest_service("/assets", ServeDir::new("../web/assets"));
let addr = SocketAddr::from(([0, 0, 0, 0], 3500));
println!("listening on {}", addr);
axum_server::bind(addr)
.serve(app.into_make_service())
.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)
}