split blog handlers into blog.rs

This commit is contained in:
Cadey Ratio 2020-07-13 08:19:33 -04:00
parent 67ff58b47a
commit cfd903dfd6
3 changed files with 73 additions and 64 deletions

68
src/handlers/blog.rs Normal file
View File

@ -0,0 +1,68 @@
use super::{PostNotFound, SeriesNotFound};
use crate::{
app::State,
post::Post,
templates::{self, Html, RenderRucte},
};
use std::sync::Arc;
use warp::{http::Response, Rejection, Reply};
pub async fn index(state: Arc<State>) -> Result<impl Reply, Rejection> {
let state = state.clone();
Response::builder().html(|o| templates::blogindex_html(o, state.blog.clone()))
}
pub async fn series(state: Arc<State>) -> Result<impl Reply, Rejection> {
let state = state.clone();
let mut series: Vec<String> = vec![];
for post in &state.blog {
if post.front_matter.series.is_some() {
series.push(post.front_matter.series.as_ref().unwrap().clone());
}
}
series.sort();
series.dedup();
Response::builder().html(|o| templates::series_html(o, series))
}
pub async fn series_view(series: String, state: Arc<State>) -> Result<impl Reply, Rejection> {
let state = state.clone();
let mut posts: Vec<Post> = vec![];
for post in &state.blog {
if post.front_matter.series.is_none() {
continue;
}
if post.front_matter.series.as_ref().unwrap() != &series {
continue;
}
posts.push(post.clone());
}
if posts.len() == 0 {
Err(SeriesNotFound(series).into())
} else {
Response::builder().html(|o| templates::series_posts_html(o, series, &posts))
}
}
pub async fn post_view(name: String, state: Arc<State>) -> Result<impl Reply, Rejection> {
let mut want: Option<Post> = None;
for post in &state.blog {
if post.link == format!("blog/{}", name) {
want = Some(post.clone());
}
}
match want {
None => Err(PostNotFound("blog".into(), name).into()),
Some(post) => {
let body = Html(post.body_html.clone());
Response::builder().html(|o| templates::blogpost_html(o, post, body))
}
}
}

View File

@ -1,6 +1,5 @@
use crate::{
app::State,
post::Post,
templates::{self, Html, RenderRucte},
};
use std::{convert::Infallible, fmt, sync::Arc};
@ -35,65 +34,7 @@ pub async fn not_found() -> Result<impl Reply, Rejection> {
Response::builder().html(|o| templates::notfound_html(o, "some path".into()))
}
pub async fn blog_index(state: Arc<State>) -> Result<impl Reply, Rejection> {
let state = state.clone();
Response::builder().html(|o| templates::blogindex_html(o, state.blog.clone()))
}
pub async fn blog_series(state: Arc<State>) -> Result<impl Reply, Rejection> {
let state = state.clone();
let mut series: Vec<String> = vec![];
for post in &state.blog {
if post.front_matter.series.is_some() {
series.push(post.front_matter.series.as_ref().unwrap().clone());
}
}
series.sort();
series.dedup();
Response::builder().html(|o| templates::series_html(o, series))
}
pub async fn blog_series_view(series: String, state: Arc<State>) -> Result<impl Reply, Rejection> {
let state = state.clone();
let mut posts: Vec<Post> = vec![];
for post in &state.blog {
if post.front_matter.series.is_none() {
continue;
}
if post.front_matter.series.as_ref().unwrap() != &series {
continue;
}
posts.push(post.clone());
}
if posts.len() == 0 {
Err(SeriesNotFound(series).into())
} else {
Response::builder().html(|o| templates::series_posts_html(o, series, &posts))
}
}
pub async fn blog_post_view(name: String, state: Arc<State>) -> Result<impl Reply, Rejection> {
let mut want: Option<Post> = None;
for post in &state.blog {
if post.link == format!("blog/{}", name) {
want = Some(post.clone());
}
}
match want {
None => Err(PostNotFound("blog".into(), name).into()),
Some(post) => {
let body = Html(post.body_html.clone());
Response::builder().html(|o| templates::blogpost_html(o, post, body))
}
}
}
pub mod blog;
#[derive(Debug, thiserror::Error)]
struct PostNotFound(String, String);

View File

@ -36,21 +36,21 @@ async fn main() -> Result<()> {
let index = base
.and(warp::path::end())
.and(with_state(state.clone()))
.and_then(handlers::blog_index);
.and_then(handlers::blog::index);
let series = base.and(
warp::path!("series").and(with_state(state.clone()).and_then(handlers::blog_series)),
warp::path!("series").and(with_state(state.clone()).and_then(handlers::blog::series)),
);
let series_view = base.and(
warp::path!("series" / String)
.and(with_state(state.clone()))
.and(warp::get())
.and_then(handlers::blog_series_view),
.and_then(handlers::blog::series_view),
);
let post_view = base.and(
warp::path!(String)
.and(with_state(state.clone()))
.and(warp::get())
.and_then(handlers::blog_post_view),
.and_then(handlers::blog::post_view),
);
index.or(series.or(series_view)).or(post_view)