Boron is a small and expressive web framework for Rust which aims to give a robust foundation for web applications and APIs.

Installation

Boron is available via Cargo. You can use it by adding it to the dependencies section of Cargo.toml.

        [dependencies]
boron = "*"
        

To include the latest (maybe unreleased) version, add the git repository.

        [dependencies]
boron = { git = "https://github.com/troposphere/boron.git" }
        

Create your first app.

        extern crate boron;

use boron::server::Boron;
use boron::request::Request;
use boron::response::Response;
use boron::router::HttpMethods;

fn main() {
    let mut app = Boron::new();
    app.get("/", |req: &Request, res: Response| {
        res.send(b"Hello World! I am Boron.")
    });
    app.listen("localhost:3000");
}
        

Adding routes

Boron provides support for adding routes with parameters and captures.

        extern crate boron;
use boron::server::Boron;
use boron::request::Request;
use boron::response::Response;
use boron::router::HttpMethods;

fn main() {
    let mut app = Boron::new();

    // adding a simple route
    // this enables boron to match all request to the URL
    // /hello and respond with the mentioned text
    app.get("/hello", |req: &Request, res: Response| {
        res.send(b"Hello World! I am Boron.")
    });

    // adding a route with parameters
    // this enables boron to capture the string <username>
    // in URLs of the form /user/<username>
    app.get(r"/user/(?P<username>\w+)", |req: &Request, res: Response| {
        let value = req.url_param("username").unwrap();
        res.send(format!("Hello {}!", value).as_bytes())
    });
    app.listen("0.0.0.0:3000");
}