fix routing?

This commit is contained in:
Cadey Ratio 2020-07-12 22:58:00 -04:00
parent 10346d4621
commit 512e6eb9a5
4 changed files with 91 additions and 8 deletions

View File

@ -1,5 +1,6 @@
use crate::{
app::State,
post::Post,
templates::{self, Html, RenderRucte},
};
use std::sync::Arc;
@ -27,11 +28,44 @@ pub async fn signalboost(state: Arc<State>) -> Result<impl Reply, Rejection> {
Response::builder().html(|o| templates::signalboost_html(o, state.signalboost.clone()))
}
pub async fn not_found(path: String) -> Result<impl Reply, Rejection> {
Response::builder().html(|o| templates::notfound_html(o, path))
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());
}
Response::builder().html(|o| templates::series_posts_html(o, series, &posts))
}

View File

@ -30,6 +30,24 @@ async fn main() -> Result<()> {
let healthcheck = warp::get().and(warp::path(".within").and(warp::path("health")).map(|| "OK"));
let blog = {
let base = warp::path!("blog" / ..);
let index = base
.and(warp::path::end())
.and(with_state(state.clone()))
.and_then(handlers::blog_index);
let series = base.and(
warp::path!("series").and(with_state(state.clone()).and_then(handlers::blog_series)),
);
index.or(series)
};
// let blog_series_view = warp::path!("blog" / "series" / String)
// .and(warp::get())
// .and(with_state(state.clone())
// .and_then( move | series: String, state: Arc<State> | handlers::blog_series_view(series, state)));
let routes = warp::get()
.and(path::end().and_then(handlers::index))
.or(warp::path!("contact").and_then(handlers::contact))
@ -40,12 +58,8 @@ async fn main() -> Result<()> {
.or(warp::path!("signalboost")
.and(with_state(state.clone()))
.and_then(handlers::signalboost))
.or(warp::path!("blog").and(
warp::path::end()
.and(with_state(state.clone()))
.and_then(handlers::blog_index),
));
.or(blog)
.or(warp::any().and_then(handlers::not_found));
let files = warp::path("static")
.and(warp::fs::dir("./static"))
.or(warp::path("css").and(warp::fs::dir("./css")))

17
templates/series.rs.html Normal file
View File

@ -0,0 +1,17 @@
@use super::{header_html, footer_html};
@(series: Vec<String>)
@:header_html(Some("Blog"), None)
<h1>Post Series</h1>
<p>
<ul>
@for set in series {
<li><a href="/blog/series/@set">@set</a></li>
}
</ul>
</p>
@:footer_html()

View File

@ -0,0 +1,18 @@
@use crate::post::Post;
@use super::{header_html, footer_html};
@(name: String, posts: &Vec<Post>)
@:header_html(Some(&name), None)
<h1>Series: @name</h1>
<p>
<ul>
@for post in posts {
<li>@post.date - <a href="/@post.link">@post.front_matter.title</a></li>
}
</ul>
</p>
@:footer_html()