Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 861 Bytes

05-include-html.md

File metadata and controls

42 lines (31 loc) · 861 Bytes

Include Html

Dependencies

[dependencies]
axum = "0.5"
tokio = { version = "1", features = ["full"] }

Code

use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    println!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn handler() -> Html<&'static str> {
    // `std::include_str` macro can be used to include an utf-8 file as `&'static str` in compile
    // time. This method is relative to current `main.rs` file.
    Html(include_str!("../index.html"))
}

Run

Run the program then enter http://localhost:3000 on your browser.

Next

To be continued.