Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::sync::Arc;
use tokio::sync::RwLock;
use blog::{Blog, RenderedBlog};
use warp::{path::FullPath, Filter};
mod blog;
mod date;
mod parser;
mod post;
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 content = content.to_owned();
Ok(warp::reply::with_status(warp::reply::html(content), status))
}
#[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;
}