Skip to content

A wrapper for the axum router that lets routes have names

License

Notifications You must be signed in to change notification settings

z0ne-dev/axum-named-routes

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Axum Named Routes

axum-named-routes is a library which allows users to easily define routes with names in axum, and get the route path from the route name at runtime.

Crates.io Documentation

Safety

  • Uses 100% safe rust with #![forbid(unsafe_code)]

Usage Example

use std::{net::SocketAddr, path::PathBuf};
use axum::routing::get;
use axum_named_routes::{NamedRouter, Routes};

async fn index() -> &'static str {
    "Hello, World!"
}

// gets the routes from axum extensions
async fn nested_other(routes: Routes) {
    // this could panic if the name is not in the Routes map
    // but we know that it is because we got here
    let this_route = routes.has("ui.other");
    assert_eq!(this_route, &PathBuf::from("/ui/other"));
}

async fn other(routes: Routes) {
    // the get function does not panic rather it returns an Option
    let route = routes.get("ui.other");
    let this_route = routes.get("other");
    assert_ne!(route, this_route);
}

#[tokio::main]
async fn main() {
    let ui = NamedRouter::new()
        .route("index", "/", get(index))
        .route("other", "/other", get(nested_other));
    let app = NamedRouter::new()
        .nest("ui", "/ui/", ui)
        .route("other", "/other", get(other));

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

That example can be found in examples/simple.rs.

Performance

The router uses a HashMap internally while creating the map, and wraps it in an Arc when it is finished to add it as an axum extension. So overall the performance cost should be very low.

License

This project is licensed under the MIT license

About

A wrapper for the axum router that lets routes have names

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%