Crates.io | axum-routes |
lib.rs | axum-routes |
version | |
source | src |
created_at | 2024-10-13 20:58:34.211449 |
updated_at | 2025-02-08 18:09:31.155159 |
description | Create an axum Router from an enum and resolve routes |
homepage | https://github.com/zllak/axum-routes |
repository | https://github.com/zllak/axum-routes |
max_upload_size | |
id | 1407679 |
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 |
axum-routes
is a crate on top of axum to
declare routers through enums, and resolve easily routes, so we don't have to
hardcode routes when linking in web apps.
axum::Router
using enumsuse axum::{extract::Path, Json};
use axum_routes::routes;
use http::StatusCode;
use serde_json::{json, Value};
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
#[routes]
enum MyApp {
#[nest("/api", customize = custom_api)]
Api(APIRoutes),
#[get("/", handler = home)]
Home,
#[get("/about", handler = about)]
About,
}
#[routes]
enum APIRoutes {
#[get("/users/:id", handler = get_users_by_id)]
GetUsersByID,
#[post("/users", handler = create_user)]
CreateUser,
}
// ----------------------------------------------------------------------------
// Handlers
async fn home() -> &'static str {
"Hello world!"
}
async fn about() {}
async fn get_users_by_id(Path(_user_id): Path<u32>) {}
async fn create_user(Json(_payload): Json<Value>) -> (StatusCode, Json<Value>) {
(StatusCode::CREATED, Json(json!({"status": "ok"})))
}
// ----------------------------------------------------------------------------
#[tokio::main]
async fn main() {
let trace_layer = ServiceBuilder::new().layer(TraceLayer::new_for_http());
let app = axum_routes::router!(MyApp,
custom_api = #move |route| {
route.layer(trace_layer.clone())
},
);
// run the app
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
See a bug ? An improvement ? A new feature you want ? Feel free to open an issue, or even a PR.
This project is not affiliated with axum
at all.