Crate servt

Source
Expand description

§A simple, easy to use, and fast web server library for Rust.

Designed to be easy, simple and fast, though powerful enough to handle most use cases.

§Features

  • Simple and easy to use
  • Multi-threaded (or async if you enable the async feature
  • Redirects
  • Custom error pages
  • Custom routes (with query string and form parsing)
  • 100-continue support
  • Custom status codes

§A few notes

  • The server by default uses multithreading, but you can enable the async feature to use async instead.
  • The Date header is included by default, but you can disable the time feature to exclude it (and save a couple of dependencies). If you don’t enable it, bear in mind that is against the HTTP/1.1 spec, but most clients don’t care, and thus it is included as an option in order to keep the library as lightweight as possible if configured that way.
  • At the moment, the server does not support keep-alive connections, but it is planned for the future (along with possible HTTP/2 support).
  • Deliberately does not support Last-Modified, If-Modified-Since, etc., because it is not designed to serve static files, but rather functions which are not trackable in the same way.

§Ensure your callbacks don’t panic

  • If a callback panics, the mutex is poisoned and thus the server will error out with a 500 Internal Server Error on any subsequent requests to the route - therefore, ensure your callbacks don’t panic (or handle any errors and return a 500 error).

§Supported path formats

  • /path

  • /path/

  • /path?query=string

  • /path/?query=string

  • site.com/path

  • site.com/path/

  • site.com/path?query=string

  • site.com/path/?query=string

  • Note that the Host header is mandated by the HTTP/1.1 spec, but if a request is sent with HTTP 1.0, the server will not enforce it (else it will error out with a 400 Bad Request).

§Examples

§Basic ‘Hello, World!’ server

use servt::{Server, ParsedRequest};

let mut server = Server::new(8080, "localhost".to_string());
server.route("/", |req| ("Hello, World!".to_string(), 200));

server.run();

§Example good practice form handling

use servt::{Server, ParsedRequest};

let mut server = Server::new(8080, "localhost".to_string());
server.route("/", |req| match req.form {
   Some(form) => (format!("Hello, {}!", form.get("name").unwrap_or(&"World".to_string())), 200),
   None => ("Hello, World!".to_string(), 200),
});

server.run();

§Custom error pages

use servt::{Server, ParsedRequest};
 
let mut server = Server::new(8080, "localhost".to_string());
server.error(404, |_| ("Custom 404 page".to_string(), 404));
 
server.run();

Modules§

status_code
The status code module, containing a lookup table for status codes.

Structs§

ParsedRequest
The ParsedRequest struct, containing the parsed request data. Method, body and args are always present, while form is only present if the request is a POST request (and the body is form-urlencoded). Can be cloned and compared, and is the exposed client-facing struct.
Server
The Server struct, containing the server data. The server is the main struct of the library, and is used to create and run the server. The route_callbacks and error_callbacks are both HashMaps containing the path and the callback to be called when the path is accessed. The redirects HashMap contains the paths to be redirected and the path to redirect to.