#web #request

web_server

Small and dependency-less crate for creating HTTP servers

17 unstable releases (3 breaking)

0.4.2 Jun 1, 2020
0.4.1 Jun 1, 2020
0.4.0 May 31, 2020
0.3.41 May 25, 2020
0.1.20 Sep 25, 2019

#545 in HTTP server

Download history 238/week @ 2024-07-26 86/week @ 2024-08-02 69/week @ 2024-08-09 57/week @ 2024-08-16 34/week @ 2024-08-23 32/week @ 2024-08-30 70/week @ 2024-09-06 67/week @ 2024-09-13 73/week @ 2024-09-20 128/week @ 2024-09-27 58/week @ 2024-10-04 55/week @ 2024-10-11 56/week @ 2024-10-18 108/week @ 2024-10-25 8/week @ 2024-11-01

237 downloads per month

MIT license

225KB
625 lines

Http Server lib for Rust

Build Status

web_server is a small, dependency-less crate for creating HTTP servers.

When you coding the backend using Rust, the most annoying thing could be the size of a freamwork and the time needed to compile the application

The web_server package fixes these problems. web_server has no dependencies, but allows you to create full-fledged servers

First server using web_server

extern crate web_server;
web_server::new()
   .get("/", |request: web_server::Request, mut response: web_server::Response|
        "Hello World!".into())
   .launch(80)
   .unwrap();

It's easy!

First you must create instance of HttpServer

web_server::new()

then you can declare your endpoints. E.g.

.get("/your/path", |request, default_response| {
    // There place your logic
    // This function returns Response
    "response text".into()
})
.post("/your/path", |_, _| "Handler for POST method")
.route(web_server::HttpMethod::DELETE, "/your/path", |_, _| "Handler for DELETE method")
.any("/your/path", |_, _| "Handler for any method")

Now you must run server by launch method

.launch(PORT).unwrap()

Receiving post data

e.g.

    use web_server::decoders::x_www_form_urlencoded;

    web_server::new()
        .post("/add-user", |req, _| {
            println!("{}", req.get_body());
            let body: HashMap<String, String> = x_www_form_urlencoded(req.get_body().as_str());
            format!(
                "Add new user: {}",
                body.get("user").unwrap_or(&String::from("Error!"))
            )
            .into()
        })
        .launch(8080)
        .unwrap();

Read examples/ to know more!

No runtime deps