worker-route

Crates.ioworker-route
lib.rsworker-route
version0.0.3
sourcesrc
created_at2023-08-27 01:31:24.630029
updated_at2023-11-09 13:51:27.702662
descriptionRoute handlers an data extractor for Cloudflare Workers.
homepage
repositoryhttps://github.com/sengho66/worker-route
max_upload_size
id955827
size91,259
Ho (sengho66)

documentation

https://docs.rs/worker-route

README

worker-route

Worker Route is a crate designed for usage in Cloudflare Workers.

Examples

use serde::{Deserialize, Serialize};
use worker::{event, Env, Request, Response, Result, RouteContext, Router};
use worker_route::{get, Configure, Query, Service};

#[derive(Debug, Serialize, Deserialize)]
struct Bar {
    bar: String,
}

#[get("/bar")]
async fn bar(req: Query<Bar>, _: RouteContext<()>) -> Result<Response> {
    Response::from_json(&req.into_inner())
}

#[derive(Debug, Serialize, Deserialize)]
struct Foo {
    foo: String,
}

#[get("/foo")]
async fn foo(req: Query<Foo>, _: RouteContext<()>) -> Result<Response> {
    Response::from_json(&req.into_inner())
}


#[derive(Debug, Serialize, Deserialize)]
struct FooBar {
    foo: String,
    bar: String,
}

// your function can consist of (Query<T>, Request, RouteContext<()>) too
#[get("/foo-bar")]
async fn foo_bar(req: Query<FooBar>, _req: Request, _: RouteContext<()>) -> Result<Response> {
    Response::from_json(&req.into_inner())
}

#[derive(Debug, Deserialize, Serialize)]
struct Person {
    name: String,
    age: usize,
}

#[get("/person/:name/:age")]
async fn person(req: Query<Person>, _: RouteContext<()>) -> Result<Response> {
    Response::from_json(&req.into_inner())
}

fn init_routes(router: Router<'_, ()>) -> Router<'_, ()> {
    router
        .configure(bar)
        .configure(foo)
        .configure(person)
        .configure(foo_bar)
}

#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
    let router = Router::new();
    router.service(init_routes).run(req, env).await
}

Features

  • Add routes to handler with macro attribute

  • Extract query parameters or path from URL

License: Apache-2.0

Commit count: 51

cargo fmt