use std::net::SocketAddr;
use axum::{response::IntoResponse, routing::get, Router};
use rstml_component::{html, write_html, For, HtmlComponent, HtmlContent};
use rstml_component_axum::HtmlContentAxiosExt;
use tokio::net::TcpListener;
#[derive(HtmlComponent)]
struct Book {
title: &'static str,
author: &'static str,
}
impl Book {
fn new(title: &'static str, author: &'static str) -> Self {
Self { title, author }
}
}
impl HtmlContent for Book {
fn fmt(self, formatter: &mut rstml_component::HtmlFormatter) -> std::fmt::Result {
write_html!(formatter,
{self.title}
"("{self.author}")"
)
}
}
// Your Axum handler
async fn index() -> impl IntoResponse {
let books = [
("Moby Dick", "Herman Melville"),
("Lord of the Rings", "John Ronald Reuel Tolkien"),
];
html!(
{ |f, book| Book::new(book.0, book.1).fmt(f) }
)
.into_html()
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(index));
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let listener = TcpListener::bind(addr)
.await
.expect(&format!("Failed to bind a TcpListener to {}", addr));
println!("listening on {}", addr);
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}