// This example serves the docs from `target/doc/`. // // Run `cargo doc && cargo run --example doc_server`, then // point your browser to http://localhost:3000/ use futures_util::future; use http::response::Builder as ResponseBuilder; use http::{header, StatusCode}; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Request, Response}; use hyper_staticfile_jsutf8::Static; use std::io::Error as IoError; use std::path::Path; async fn handle_request(req: Request, static_: Static) -> Result, IoError> { if req.uri().path() == "/" { let res = ResponseBuilder::new() .status(StatusCode::MOVED_PERMANENTLY) .header(header::LOCATION, "/hyper_staticfile/") .body(Body::empty()) .expect("unable to build response"); Ok(res) } else { static_.clone().serve(req).await } } #[tokio::main] async fn main() { let static_ = Static::new(Path::new("target/doc/")); let make_service = make_service_fn(|_| { let static_ = static_.clone(); future::ok::<_, hyper::Error>(service_fn(move |req| handle_request(req, static_.clone()))) }); let addr = ([127, 0, 0, 1], 3000).into(); let server = hyper::Server::bind(&addr).serve(make_service); eprintln!("Doc server running on http://{}/", addr); server.await.expect("Server failed"); }