worker-route

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

Examples

```rust 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, : RouteContext<()>) -> Result { Response::fromjson(&req.into_inner()) }

[derive(Debug, Serialize, Deserialize)]

struct Foo { foo: String, }

[get("/foo")]

async fn foo(req: Query, : RouteContext<()>) -> Result { Response::fromjson(&req.into_inner()) }

[derive(Debug, Serialize, Deserialize)]

struct FooBar { foo: String, bar: String, }

// your function can consists of (Query, Request, RouteContext<()>) too

[get("/foo-bar")]

async fn foobar(req: Query, _req: Request, _: RouteContext<()>) -> Result { Response::fromjson(&req.into_inner()) }

[derive(Debug, Deserialize, Serialize)]

struct Person { name: String, age: usize, }

[get("/person/:name/:age")]

async fn person(req: Query, : RouteContext<()>) -> Result { Response::fromjson(&req.into_inner()) }

fn initroutes(router: Router<'static, ()>) -> Router<'static, ()> { router .configure(bar) .configure(foo) .configure(person) .configure(foobar) }

[event(fetch)]

pub async fn main(req: Request, env: Env, ctx: worker::Context) -> Result { let router = Router::new(); router.service(initroutes).run(req, env).await } ```

Features

Limitations

Currently only async methods are supported. If you have a synchronous get method, it will be set to .get_async() instead of .get().

License: Apache-2.0