Skip to content
Snippets Groups Projects
main.rs 1.11 KiB
Newer Older
Stephen D's avatar
Stephen D committed
use std::sync::Arc;
use tokio::sync::RwLock;

use blog::{Blog, RenderedBlog};
Stephen D's avatar
Stephen D committed
use warp::{
    http::HeaderValue,
    hyper::{header::CONTENT_TYPE, Body},
    path::FullPath,
    reply, Filter,
};
Stephen D's avatar
Stephen D committed

mod blog;
mod date;
Stephen D's avatar
Stephen D committed
mod image;
mod load;
Stephen D's avatar
Stephen D committed
mod parser;
mod post;
Stephen D's avatar
Stephen D committed
mod resp;
mod util;
Stephen D's avatar
Stephen D committed

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());

Stephen D's avatar
Stephen D committed
    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)
Stephen D's avatar
Stephen D committed
}

#[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;
}