Expand description
NOTE
This is a maintenace release of rouille, not by its original author, and not endorsed or supported by him. The aim of the release is to minimally update the code to the 2018 edition of Rust, and upgrade several dependencies to avoid crate duplication. This crate should be a drop-in replacement for
rouille-3.0.0. To use it, updateCargo.tomlto say:
rouille = { version = "3", package = "rouille-maint-in" }
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 functionnalities 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
inputmodule.
§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.
Re-exports§
pub extern crate percent_encoding;pub extern crate url;
Modules§
- cgi
- Allows you to let an external process handle the request through CGI.
- content_
encoding - input
- Analyze the request’s headers and body.
- proxy
- Dispatch a request to another HTTP server.
- session
- Sessions handling.
- websocket
- Support for websockets.
Macros§
- accept
- Dispatches between blocks depending on the value of the
Acceptheader. - assert_
or_ 400 - This macro assumes that the current function returns a
Response. If the condition you pass to the macro is false, then a 400 response is returned. - find_
route - Evaluates each parameter until one of them evaluates to something else than a 404 error code.
- post_
input - Parse input from HTML forms. See the
postmodule for general documentation. - response_
panic - Calls the panic! macro from the standard library wrapped in a
Response-typed expression, to avoid the compiler complaining about
unreachable paths in certain styles of
router!blocks. The macro supports exactly the same types of arguments as the one instd. - router
- Equivalent to a
matchexpression but for routes. - try_
or_ 400 - This macro assumes that the current function returns a
Responseand takes aResult. If the expression you pass to the macro is an error, then a 400 response is returned. - try_
or_ 404 - This macro assumes that the current function returns a
Responseand takes aResult. If the expression you pass to the macro is an error, then a 404 response is returned.
Structs§
- Headers
Iter - Iterator to the list of headers in a request.
- Request
- Represents a request that your handler must answer to.
- Request
Body - Gives access to the body of a request.
- Response
- Contains a prototype of a response.
- Response
Body - An opaque type that represents the body of a response.
- Server
- A listening server.
Constants§
Traits§
- Read
Write - Dummy trait that regroups the
ReadandWritetraits. - Upgrade
- Trait for objects that can take ownership of a raw connection to the client data.
Functions§
- extension_
to_ mime - Returns the mime type of a file based on its extension, or
application/octet-streamif the extension is unknown. - log
- Adds a log entry to the given writer for each request.
- log_
custom - Calls custom logging functions after processing a request.
- match_
assets - Searches inside
pathfor a file that matches the given request. If a file is found, returns aResponsethat would serve this file if returned. If no file is found, a 404 response is returned instead. - start_
server - Starts a server and uses the given requests handler.
- start_
server_ with_ pool - Identical to
start_serverbut uses aThreadPoolof the given size.