Crate web_server

Source
Expand description

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

use web_server::route;

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

Now you must run server by launch method

.launch(PORT).unwrap()

You can send files to client e.g.

.get("/image.png", Box::new(|_, _| {
    std::path::Path::new("path to your file").into();
}))

Modules§

decoders
Decoders for http request body

Structs§

HttpRoute
Storing http methods and path
HttpServer
Storing basics informations about server and handlers Represents http server
Request
Represents http request
Response
Represents http request

Enums§

Body
HttpMethod
Represents http’s methods

Functions§

create_server
Create new instance of HttpServer with predefined body
new
Create new instance of HttpServer

Type Aliases§

RequestHandler