ryde_router

Crates.ioryde_router
lib.rsryde_router
version0.1.1
sourcesrc
created_at2024-02-29 16:52:51.742407
updated_at2024-03-18 21:10:39.058943
descriptionrouter crate for ryde
homepagehttps://github.com/swlkr/ryde
repositoryhttps://github.com/swlkr/ryde
max_upload_size
id1157971
size6,223
swlkr (swlkr)

documentation

README

router

router is a rust enum router for axum that doesn't support nesting.

Declare your routes

use router::router;

#[router]
pub enum Route {
  #[get("/")]
  Root,
  #[get("/todos/:id/edit")]
  EditTodo(i32)
  #[put("/todos/:id")]
  UpdateTodo(i32)
}

It will complain about missing functions which you still have to write:

async fn root() -> String {
  Route::Root.to_string() // "/"
}

async fn edit_todo(Path(id): Path<i32>) -> String {
  Route::EditTodo(id).to_string() // "/todos/:id/edit"
}

async fn update_todo(Path(id): Path<i32>) -> String {
  Route::UpdateTodo(id).to_string() // "/todos/:id"
}

Use it like this

#[tokio::main]
async fn main() {
  let ip = "127.0.0.1:9001";
  let listener = tokio::net::TcpListener::bind(ip).await.unwrap();
  let router = Route::router();
  axum::serve(listener, router).await.unwrap();
}

Got state?

use std::sync::Arc;
use axum::extract::State;

struct AppState {
  count: u64
}

#[router(Arc<AppState>)]
enum Routes {
  #[get("/")]
  Index
}

async fn index(State(_st): State<Arc<AppState>>) -> String {
  Routes::Index.to_string()
}

#[tokio::main]
async fn main() {
  let ip = "127.0.0.1:9001";
  let listener = tokio::net::TcpListener::bind(ip).await.unwrap();
  let router = Routes::router().with_state(Arc::new(AppState { count: 0 }));
  axum::serve(listener, router).await.unwrap();
}
Commit count: 115

cargo fmt