Crates.io | route_controller |
lib.rs | route_controller |
version | |
source | src |
created_at | 2025-01-08 18:21:00.185095 |
updated_at | 2025-01-08 18:35:05.548114 |
description | An attribute macro enabling a structured approach to defining routes and attaching middleware for axum servers |
homepage | |
repository | |
max_upload_size | |
id | 1508902 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A Rust procedural macro crate that provides a structured approach for defining routes and attaching middleware for Axum-based web servers. This crate helps generate Axum routers by combining annotated methods into routes with specified HTTP methods and paths, while also allowing middleware functions to be applied to these routes.
cargo add route_controller
use axum::{
body::Body,
extract::{Request, State},
http::StatusCode,
middleware::Next,
response::{IntoResponse, Response},
Json, Router,
};
use route_controller::{controller, route};
#[derive(Clone)]
pub struct AppState {}
// Auth Middleware using AppState
async fn auth_middleware(
State(\_app_state): State<AppState>,
req: Request,
next: Next,
) -> Result<Response<Body>, StatusCode> {
Ok(next.run(req).await)
}
// Context Middleware without AppState
async fn context_middleware(
mut req: Request,
next: Next
) -> Result<Response<Body>, StatusCode> {
let trace_id = "123"; // Replace with a uuid
req.headers_mut().append("trace_id", trace_id.parse().unwrap());
Ok(next.run(req).await)
}
pub struct ApiController;
// Controller with base_path and middlewares
#[controller(
path = "/api",
middleware = "auth_middleware",
middleware = "context_middleware"
)]
impl ApiController {
// route with http methods and endpoint
#[route("GET", "/users")]
pub async fn get_users(\_request: Request) -> impl IntoResponse {
Json(vec!["user1", "user2"])
}
// route using AppState
#[route("POST", "/users")]
pub async fn create_user(
State(_app_state): State<AppState>,
_request: Request
) -> impl IntoResponse {
Json("User Created")
}
}
#[tokio::main]
async fn main() {
let app_state = AppState {};
let router = Router::new()
.merge(ApiController::router(app_state.clone()))
.with_state(app_state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await.unwrap();
axum::serve(listener, router).await.unwrap();
}
The #[controller] macro generates a router function for the controller, which constructs an Axum::Router by combining the specified routes and applying the middleware.