Crate rustful [] [src]

A light HTTP framework with REST-like features. The main purpose of Rustful is to create a simple, modular and non-intrusive foundation for HTTP applications. It has a mainly stateless structure, which naturally allows it to run both as one single server and as multiple instances in a cluster.

A new server is created using the Server type, which contains all the necessary settings as fields:

extern crate rustful;
use rustful::{Server, Handler, Context, Response, DefaultRouter};

struct Phrase(&'static str);

impl Handler for Phrase {
    fn handle(&self, context: Context, response: Response) {
        //Check if the client accessed /hello/:name or /good_bye/:name
        if let Some(name) = context.variables.get("name") {
            //Use the value of :name
            response.send(format!("{}, {}", self.0, name));
        } else {
            response.send(self.0)
        }
    }
}

let mut my_router = DefaultRouter::<Phrase>::new();
my_router.build().many(|mut node| {
    //Receive GET requests to /hello and /hello/:name
    node.path("hello").many(|mut node| {
        node.then().on_get(Phrase("hello"));
        node.path(":name").then().on_get(Phrase("hello"));
    });

    //Receive GET requests to /good_bye and /good_bye/:name
    node.path("good_bye").many(|mut node| {
        node.then().on_get(Phrase("good bye"));
        node.path(":name").then().on_get(Phrase("good bye"));
    });
});

Server {
    //Use a closure to handle requests.
    handlers: my_router,
    //Set the listening port to `8080`.
    host: 8080.into(),
    //Fill out everything else with default values.
    ..Server::default()
}.run();

Re-exports

pub use self::server::Server;
pub use self::context::Context;
pub use self::response::Response;
pub use self::response::Error;
pub use self::response::SendResponse;
pub use self::handler::Handler;
pub use self::handler::DefaultRouter;
pub use self::handler::ContentFactory;
pub use self::handler::CreateContent;
pub use self::handler::OrElse;
pub use self::handler::StatusRouter;

Modules

context

Handler context and request body reading extensions.

file

File related utilities.

filter

Request and context filters.

handler

Request handlers and routers.

header

Headers container, and common header fields.

mime

Re-exporting the mime crate, for convenience.

net

Network related types and traits, reexported from Hyper.

response

Response writers.

server

Server configuration and instance.

Macros

content_type

A macro for making content types.

Enums

HttpError

A set of errors that can occur parsing HTTP streams.

HttpVersion

Represents a version of the HTTP spec.

Method

The Request Method (VERB)

StatusCode

An HTTP status code (status-code in RFC 7230 et al.).

Type Definitions

HttpResult

Result type often returned from methods that can have hyper Errors.