main/
main.rs

1use axum::Router;
2use coaxial::{
3    context::Context,
4    html::{body, button, div, head, html, p},
5    live::live,
6    CoaxialResponse, Config,
7};
8
9#[tokio::main]
10async fn main() {
11    let app = Router::new()
12        .route("/", live(counter))
13        // this following line is optional since this is the default, i'm adding it for documentation purposes
14        .layer(Config::with_layout(|content| html(head(()) + body(content))).layer());
15
16    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
17    axum::serve(listener, app).await.unwrap();
18}
19
20async fn counter(mut ctx: Context) -> CoaxialResponse {
21    let counter = ctx.use_state(0u32);
22
23    let add = ctx.use_closure(move || async move {
24        counter.set(counter.get() + 1);
25    });
26    let sub = ctx.use_closure(move || async move {
27        counter.set(counter.get() - 1);
28    });
29
30    ctx.with(div(p(counter)
31        + button(("+", ("onclick", add)))
32        + button(("-", ("onclick", sub)))))
33}