Welcome to my blog 🤟🏽

Those are the most recent posts tagged with axum


Let the compiler check your URLs

Published on
🏷️ rust, axum

It's nice when some classes of bugs cannot be fabricated. For example, some languages like Kotlin and Rust bake the concept of "empty" especially in the language and standard library to remind and force you (the developer) to deal with the case when something is missing.

This post is about a library I wrote that brings a similar compile-verified assurance to URL construction when writing web servers in Rust. 🦀

The problem it solves is: in a web server, you usually declare some "handlers" (like web pages, assets or api endpoints) and hook them to a global path router. It works well, but what about when you want to link from one page to another? How to do that correctly and let the compiler help you keep all links correct as your app evolves?

To illustrate, consider this basic example using the library axum in Rust to create a website with a home page and a dynamic "posts" page, with the home linking to a post:

use axum::extract::Path;
use axum::response::Html;
use axum::routing::get;

#[tokio::main]
async fn main() {
    let app = axum::Router::new()
        .route("/", get(home))
        .route("/post/{id}", get(about));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();

    axum::serve(listener, app).await.unwrap();
}

async fn home() -> Html<String> {
    Html("Hello! <a href='/post/17'>See post</a>".to_string())
}

async fn about(Path(id): Path<u32>) -> Html<String> {
    Html(format!("Thanks for visiting post {}", id))
}

... continue reading ...


Web server auto-reload in Rust

Published on
🏷️ programming, rust, axum

This blog is written in Rust, and I wanted a way to reload the web pages automatically while I change the posts' contents, styles, etc. This is common-place with JavaScript frameworks, but not automatic in the Rust land. So I've embarked on a side quest to achieve just that: the "type and auto-reload" experience. In the end, I was surprised to learn a bit more about sockets and processes in Linux.

This post is a note to myself about these nuggets that I've learned and to share the solution. It may be helpful for future me and I hope for someonelse out there.

TL;DR

You can check the solution here: https://git.sitegui.dev/sitegui/axum-web-auto-reload-example/src/branch/main/src/main.rs. The README in that repo has some nice diagrams as well.

Shopping list

... continue reading ...


Maximum frequency:

I will not share your email with anyone. I will only use it to send you a newsletter when there are new posts. My newsletters have no tracking at all.