use std::sync::Arc; use tokio::sync::RwLock; use blog::{Blog, RenderedBlog}; use warp::{ http::HeaderValue, hyper::{header::CONTENT_TYPE, Body}, path::FullPath, reply, Filter, }; mod blog; mod date; mod image; mod load; mod misc; mod parser; mod post; mod resp; mod util; async fn handle_request( req: FullPath, blog: Arc<RwLock<RenderedBlog>>, ) -> Result<impl warp::Reply, warp::Rejection> { let blog = blog.read().await; let (status, content) = blog.get(req.as_str()); let mut reply = reply::Response::new(Body::from(content.data)); reply .headers_mut() .insert(CONTENT_TYPE, HeaderValue::from_static(content.content_type)); *reply.status_mut() = status; Ok(reply) } #[tokio::main] async fn main() { let blog: RenderedBlog = Blog::new().unwrap().try_into().unwrap(); let blog = Arc::new(RwLock::new(blog)); let blog_filter = warp::any().map(move || blog.clone()); warp::serve( warp::filters::path::full() .and(blog_filter.clone()) .and_then(handle_request), ) .run(([0, 0, 0, 0], 3030)) .await; }