#![deny(warnings)] use std::task::{Context, Poll}; use futures_util::future; use hyper::service::Service; use hyper::{Body, Request, Response, Server}; const ROOT: &str = "/"; #[derive(Debug)] pub struct Svc; impl Service> for Svc { type Response = Response; type Error = hyper::Error; type Future = future::Ready>; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Ok(()).into() } fn call(&mut self, req: Request) -> Self::Future { let rsp = Response::builder(); let uri = req.uri(); if uri.path() != ROOT { let body = Body::from(Vec::new()); let rsp = rsp.status(404).body(body).unwrap(); return future::ok(rsp); } let body = Body::from(Vec::from(&b"heyo!"[..])); let rsp = rsp.status(200).body(body).unwrap(); future::ok(rsp) } } pub struct MakeSvc; impl Service for MakeSvc { type Response = Svc; type Error = std::io::Error; type Future = future::Ready>; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Ok(()).into() } fn call(&mut self, _: T) -> Self::Future { future::ok(Svc) } } #[tokio::main] async fn main() -> Result<(), Box> { pretty_env_logger::init(); let addr = "127.0.0.1:1337".parse().unwrap(); let server = Server::bind(&addr).serve(MakeSvc); println!("Listening on http://{}", addr); server.await?; Ok(()) }