Expand description
The rouille library is very easy to get started with.
Listening to a port is done by calling the start_server
function:
use rouille::Request;
use rouille::Response;
rouille::start_server("0.0.0.0:80", move |request| {
Response::text("hello world")
});
Whenever an HTTP request is received on the address passed as first parameter, the closure
passed as second parameter is called. This closure must then return a
Response
that will be sent back to the client.
See the documentation of start_server
for more details.
Analyzing the request
The parameter that the closure receives is a Request
object that
represents the request made by the client.
The Request
object itself provides some getters, but most advanced functionalities are
provided by other modules of this crate.
- In order to dispatch between various code depending on the URL, you can use the
router!
macro. - In order to analyze the body of the request, like handling JSON input, form input, etc. you
can take a look at the
input
module.
Returning a response
Once you analyzed the request, it is time to return a response by returning a
Response
object.
All the members of Response
are public, so you can customize it as you want. There are also
several constructors that you build a basic Response
which can then modify.
In order to serve static files, take a look at
the match_assets
function.
In order to apply content encodings (including compression such as gzip or deflate), see the content_encoding module, and specifically the content_encoding::apply function.
Re-exports
pub extern crate percent_encoding;
pub extern crate url;
Modules
Macros
Accept
header.Response
. If the condition you pass
to the macro is false, then a 400 response is returned.post
module for general
documentation.match
expression but for routes.Response
and takes a Result
.
If the expression you pass to the macro is an error, then a 400 response is returned.Response
and takes a Result
.
If the expression you pass to the macro is an error, then a 404 response is returned.Structs
Constants
Traits
Read
and Write
traits.Functions
application/octet-stream
if the
extension is unknown.path
for a file that matches the given request. If a file is found,
returns a Response
that would serve this file if returned. If no file is found, a 404
response is returned instead.start_server
but uses a ThreadPool
of the given size.