1use async_trait::async_trait;
2use ezhttp::{prelude::*, Sendable};
3
4struct EzSite(String);
5
6#[async_trait]
7impl HttpServer for EzSite {
8 async fn on_request(&self, req: &HttpRequest) -> Option<Box<dyn Sendable>> {
9 println!("{} > {} {}", req.addr, req.method, req.url.to_path_string());
10
11 if req.url.path == "/" {
12 Some(HttpResponse::new(
13 OK, Headers::from(vec![ ("Content-Type", "text/html"), ("Content-Length", self.0.len().to_string().as_str()) ]), Body::from_text(&self.0.clone()), ).as_box())
19 } else {
20 None }
22 }
23
24 async fn on_start(&self, host: &str) {
25 println!("Http server started on {}", host);
26 }
27
28 async fn on_close(&self) {
29 println!("Http server closed");
30 }
31}
32
33#[tokio::main]
34async fn main() {
35 start_server(EzSite("Hello World!".to_string()), "localhost:8080").await.expect("http server error");
36}