Function warp::reply::html

source · []
pub fn html<T>(body: T) -> Html<T> where
    Body: From<T>,
    T: Send
Expand description

Reply with a body and content-type set to text/html; charset=utf-8.

Example

use warp::Filter;

let body = r#"
<html>
    <head>
        <title>HTML with warp!</title>
    </head>
    <body>
        <h1>warp + HTML = &hearts;</h1>
    </body>
</html>
"#;

let route = warp::any()
    .map(move || {
        warp::reply::html(body)
    });
Examples found in repository?
examples/handlebars_template.rs (line 21)
14
15
16
17
18
19
20
21
22
fn render<T>(template: WithTemplate<T>, hbs: Arc<Handlebars>) -> impl warp::Reply
where
    T: Serialize,
{
    let render = hbs
        .render(template.name, &template.value)
        .unwrap_or_else(|err| err.to_string());
    warp::reply::html(render)
}
More examples
Hide additional examples
examples/websockets_chat.rs (line 44)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
async fn main() {
    pretty_env_logger::init();

    // Keep track of all connected users, key is usize, value
    // is a websocket sender.
    let users = Users::default();
    // Turn our "state" into a new Filter...
    let users = warp::any().map(move || users.clone());

    // GET /chat -> websocket upgrade
    let chat = warp::path("chat")
        // The `ws()` filter will prepare Websocket handshake...
        .and(warp::ws())
        .and(users)
        .map(|ws: warp::ws::Ws, users| {
            // This will call our function if the handshake succeeds.
            ws.on_upgrade(move |socket| user_connected(socket, users))
        });

    // GET / -> index html
    let index = warp::path::end().map(|| warp::reply::html(INDEX_HTML));

    let routes = index.or(chat);

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}