macroblog.rs @ e7c5dc01cf3bf382c82dd6984808bd3ca21d33a5

 1use std::convert::Infallible;
 2use std::{include_str};
 3use std::net::SocketAddr;
 4use hyper::{Body, Request, Response, Server};
 5use hyper::service::{make_service_fn, service_fn};
 6use sailfish::TemplateOnce;
 7
 8
 9#[derive(TemplateOnce)]
10#[template(path = "index.html")]
11struct IndexTemplate {
12    pico: &'static str,
13}
14
15const PICO_CSS: &str = include_str!("../assets/pico.min.css");
16
17async fn hello_world(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
18    let body = IndexTemplate { pico: PICO_CSS }
19        .render_once()
20        .unwrap();
21
22    let resp: Response<Body> = Response::builder()
23        .status(200)
24        .header("content-type", "text/html")
25        .body(body.into())
26        .unwrap();
27
28    Ok(resp)
29}
30
31
32#[tokio::main]
33async fn main() {
34    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
35
36    let make_svc = make_service_fn(|_conn| async {
37        Ok::<_, Infallible>(service_fn(hello_world))
38    });
39
40    let server = Server::bind(&addr).serve(make_svc);
41
42    if let Err(e) = server.await {
43        eprintln!("server error: {}", e);
44    }
45}