Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Serve Embedded HTML With axum

  • Remove merge from this example since you don't really need to do that

Code

[package]
name = "axum_cookies"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.6" 
tokio = { version = "1", features = ["full"] }
tower-cookies = "0.9"

Code

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

#[tokio::main]
async fn main() {
    let routes_all = Router::new().merge(hello_route());
    let addr = SocketAddr::from(([127, 0, 0, 1], 8484));
    axum::Server::bind(&addr)
        .serve(routes_all.into_make_service())
        .await
        .unwrap();
}

fn hello_route() -> Router {
    Router::new().route("/", get(hello_handler))
}

async fn hello_handler() -> impl IntoResponse {
    Html("Hello, World".to_string())
}