Crate axum_controller

Source
Expand description

Crates.io Documentation

§Axum-Controller

Helper macro for axum-typed-routing.

Adds a #[controller(...)] macro for easy wrapping of multiple routes inside of a “controller” Struct.

See example here.

See the docs for more information.

§Licensing

This repository, like all my personal projects, is licensed under the GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later). This ensures that modifications to the code remain open source when used in network services.

If the AGPL license doesn’t suit your needs, a version under more permissive terms (like MIT, Apache, or BSD license) is available for a small fee. Please contact me directly via the email in the crate metadata for licensing inquiries.

§Basic route macro usage

See the docs of axum_typed_routing for details on the route macro. For convenience we re-export the route macro & TypedRouter for you so that all you need to use on your side is use axum_controller::*

§Controller macro usage

This crate also offers a controller() attribute macro. use it like this:

use axum::extract::State;
use axum_controller::*;

struct ExampleController;

async fn my_middleware(
    request: axum::extract::Request,
    next: axum::middleware::Next,
) -> axum::response::Response {
    next.run(request).await
}

async fn my_other_middleware(
    request: axum::extract::Request,
    next: axum::middleware::Next,
) -> axum::response::Response {
    next.run(request).await
}

#[derive(Clone, Debug)]
struct AppState();

#[controller(
    path = "/asd",
    state = AppState,
    middleware=axum::middleware::from_fn(my_middleware),
    middleware=axum::middleware::from_fn(my_other_middleware),
)]
impl ExampleController {
    #[route(GET "/test")]
    async fn test_handler_fn(_: State<AppState>) -> String {
        todo!("handle request")
    }

    #[route(GET "/test2")]
    async fn test_handler_fn2(State(_): State<AppState>) -> String {
        todo!("handle request")
    }
}

fn main() {
    let _router: axum::Router<AppState>= ExampleController.into_router(AppState());
}

Traits§

TypedRouter
A trait that allows typed routes, created with the route macro to be added to an axum router.

Attribute Macros§

controller
A macro that generates a into_router(_: State<_>) impl which automatically wires up all route’s and the given middlewares, path-prefix etc
route
A macro that generates statically-typed routes for axum handlers.