aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 0c3565b8c097ceeabcf97b10987859bfa6e793b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::convert::Infallible;
use std::{include_str};
use std::net::SocketAddr;
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use sailfish::TemplateOnce;


#[derive(TemplateOnce)]
#[template(path = "index.html")]
struct IndexTemplate {
    pico: &'static str,
}

const PICO_CSS: &str = include_str!("../assets/pico.min.css");

async fn hello_world(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
    let body = IndexTemplate { pico: PICO_CSS }
        .render_once()
        .unwrap();

    let resp: Response<Body> = Response::builder()
        .status(200)
        .header("content-type", "text/html")
        .body(body.into())
        .unwrap();

    Ok(resp)
}


#[tokio::main]
async fn main() {
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    let make_svc = make_service_fn(|_conn| async {
        Ok::<_, Infallible>(service_fn(hello_world))
    });

    let server = Server::bind(&addr).serve(make_svc);

    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }
}