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 = ♥</h1>
</body>
</html>
"#;
let route = warp::any()
.map(move || {
warp::reply::html(body)
});
Examples found in repository?
More 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;
}