From e7c5dc01cf3bf382c82dd6984808bd3ca21d33a5 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Thu, 5 May 2022 23:15:41 +0200 Subject: Initial commit --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0c3565b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,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) -> Result, Infallible> { + let body = IndexTemplate { pico: PICO_CSS } + .render_once() + .unwrap(); + + let resp: Response = 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); + } +} -- cgit v1.2.3