into_response/
into_response.rs

1use vegemite::{run, sys, systems::Endpoint, Get, IntoResponse, Response, Route};
2
3// This is a reimplementation of the provided `Html` type.
4struct Html {
5    value: String,
6}
7
8impl IntoResponse for Html {
9    fn response(self) -> Response<Vec<u8>> {
10        let bytes = self.value.into_bytes();
11
12        Response::builder()
13            .status(200)
14            .header("Content-Type", "text/html; charset=utf-8")
15            .header("Content-Length", format!("{}", bytes.len()))
16            .body(bytes)
17            .unwrap()
18    }
19}
20
21fn page(_get: Get, _e: Endpoint) -> Html {
22    Html {
23        value: "<h1> Hey Friend </h1>".to_string(),
24    }
25}
26
27fn favicon(_get: Get, _e: Endpoint) -> u16 {
28    println!("No favicon yet :C");
29    404
30}
31
32fn main() {
33    let router = Route::empty()
34        .route("favicon.ico", sys![favicon])
35        .route("page", sys![page]);
36
37    println!("Try connecting from a browser at 'http://localhost:8080/page'");
38
39    run("127.0.0.1:5000", router);
40}