Crate hyper_router [] [src]

Hyper Router

This cargo is a small extension to the great Hyper HTTP library. It basically is adds the ability to define routes to request handlers and then query for the handlers by request path.

Usage

To use the library just add:

hyper-router = "*"

to your dependencies.

extern crate hyper;
extern crate hyper_router;

use hyper::server::{Http, Request, Response};
use hyper::header::{ContentLength, ContentType};
use hyper_router::{Route, RouterBuilder, RouterService};
 
fn basic_handler(_: Request) -> Response {
    let body = "Hello World";
    Response::new()
        .with_header(ContentLength(body.len() as u64))
        .with_header(ContentType::plaintext())
        .with_body(body)
}

fn router_service() -> Result<RouterService, std::io::Error> {
    let router = RouterBuilder::new()
        .add(Route::get("/greet").using(basic_handler))
        .build();
    Ok(RouterService::new(router))
}

fn main() {
    let addr = "0.0.0.0:8080".parse().unwrap();
    let server = Http::new().bind(&addr, router_service).unwrap();
    server.run().unwrap();
}

This code will start Hyper server and add use router to find handlers for request. We create the Route so that when we visit path /greet the basic_handler handler will be called.

Things to note

Waiting for your feedback

I've created this little tool to help myself learn Rust and to avoid using big frameworks like Iron or rustful. I just want to keep things simple.

Obviously I could make some errors or bad design choices so I'm waiting for your feedback! You may create an issue at project's bug tracker.

Modules

handlers
route

Structs

Path

Represents a path in HTTP sense (starting from /)

Route

Holds route information

RouteBuilder
Router

This is the one. The router.

RouterBuilder

Builder for a router

RouterService

The default simple router service.

Type Definitions

Handler
HttpResult