7 个版本

0.1.10 2024 年 8 月 16 日
0.1.9 2024 年 5 月 25 日
0.1.4 2024 年 4 月 27 日

#938 in Web 编程

Download history 3/week @ 2024-05-03 234/week @ 2024-05-17 397/week @ 2024-05-24 23/week @ 2024-05-31 8/week @ 2024-06-07 1/week @ 2024-06-14 4/week @ 2024-07-05 75/week @ 2024-07-26 7/week @ 2024-08-02 161/week @ 2024-08-16

每月 243 次下载

MIT 许可证

27KB
631

命名路由 Axum

Name Routes Axums 是一个 crate,允许您为 Axum 路由命名

 use named_routes_axum::{RouterWrapper, NamedRoutesService};

let app = axum::Router::<()>::new();
let my_named_routes = RouterWrapper::new().get("/hello", || async { "Hello world" }, "index-page");

app.merge(my_named_routes.into_router()); // then get the actual axum router built
// using  an instance of the `NamedRoutesService` you can redirect or get the route path

let path = NamedRoutesService::new().get("index-page").unwrap().redirector().path();
完整示例
use axum::{
  extract::{Path, State},
  response::{Html, IntoResponse},
};
use named_routes_axum::{NamedRoutesService, RouterWrapper};
use rand::Rng;

#[tokio::main]
async fn main() {
  // 1. Application state
  let state = AppState::default();

  // 2.  build our application with a route
  let app = RouterWrapper::new()
      .get("/", handler, "home")
      .get("/day/:index", handle_day, "day");

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

  println!("listening on {}", listener.local_addr().unwrap());
  axum::serve(listener, app.into_router().with_state(state))
      .await
      .unwrap();
}

async fn handler(State(app): State<AppState>) -> impl IntoResponse {
  // 3. Get the route with name "day" and redirect to it
  if let Some(route) = app.route_service().get("day") {
      // 4. The route named "day" requires a value
      let part = rand::thread_rng().gen_range(0..6);

      return route.with(part.to_string()).redirect(Html("")); // we are creating a response with an empty HTML body

      // or
      // return route.with((part,)).redirect(Html("")); 
      // return route.with(vec![part]).redirect(Html("")); 
      //
      // let mut map = std::collections::HashMap::new();
      //  map.insert("index", part)
      // return route.with(map).redirect(Html("")); 
  } else {
      Html("<h1>We could not get the rout named <b>add_numbers</b></h1>").into_response()
  }
}

async fn handle_day(Path(index): Path<i32>) -> Html<&'static str> {
  let days = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
  ];
  let unknown = "Unknown";

  Html(days.get(index as usize).unwrap_or(&unknown))
}

#[derive(Debug, Default, Clone)]
struct AppState {
  route_service: NamedRoutesService,
}

impl AppState {
  fn route_service(&self) -> &NamedRoutesService {
      &self.route_service
  }
}

更多示例

示例文件夹 examples 包含简单和完整示例。如果这些示例都没有帮助,请告诉我您的使用案例,我会尽量提供一个。

反馈

如果您觉得这个 crate 有用,请星标该仓库。同时提交您的问题和建议。

许可证

MIT 许可证 (MIT)

以下对本软件及其相关文档文件的副本(“软件”)的使用,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,以及允许向软件提供者提供软件的人士使用软件的权利,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该索赔、损害或其他责任是基于合同、侵权或其他原因,以及与软件或其使用或其他方式相关。

依赖关系

~5–7MB
~123K SLoC