7 releases

0.1.6 Nov 3, 2023
0.1.5 Nov 1, 2023
0.1.4 Oct 30, 2023

#976 in Web programming

47 downloads per month

GPL-3.0-only

18KB
213 lines

crane

Rust

A simple and fast webserver :)

Getting started

In order to build a webserver, you need to add crane-webserver as a dependency in your rust project by:

cargo add crane-webserver

Examples

Create an HTTP server that responds with a message.

use crane_webserver::*;

fn main() {
    let server = WebServer::bind("127.0.0.1:8888", |path, query| {
        match path.as_str() {
            "/" => default_route_fn(query),
            _ => ResponseBuilder::new().build(),
        }
    }).unwrap()

    server.start();
}

fn root() -> Response {
    ResponseBuilder::new()
        .status(HttpStatus::OK)
        .header("Content-Type", "text/plain")
        .body("Hello, World!")
        .build()
}
$ curl localhost:8888/
Hello, World!

lib.rs:

A simple and fast webserver.

crane-webserver proves the tools you need to quickly build a webserver.

How it works?

At its core, crane-webserver contains a WebServer. The WebServer is a "builder", which takes a closure which is responsible for mapping different functions for different paths, then call the start function to start the web server.

Examples

A basic web server that serves "Hello, World!"

use crane_webserver::*;

fn main() {
    let server = WebServer::bind("127.0.0.1:8888", |path, _query| {
        match path.as_str() {
            "/" => root(),
            _ => ResponseBuilder::new().build()
        }
    }).unwrap();

    server.start();
}

fn root() -> Response {
    ResponseBuilder::new()
        .status(HttpStatus::OK)
        .header("Content-Type", "text/plain")
        .body("Hello, World!")
        .build()
}

Run the program and then open your web browser goto http://localhost:8888/ and see the server in action!

Dependencies

~1.5MB
~37K SLoC