implement post aliases
This commit is contained in:
parent
baa5cbc344
commit
6be5525bae
4 changed files with 21 additions and 4 deletions
|
@ -20,10 +20,11 @@ lazy_static! {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn routes() -> Router<Arc<AppState>> {
|
||||
pub fn routes(state: &Arc<AppState>) -> Router<Arc<AppState>> {
|
||||
Router::new()
|
||||
.route("/", get(index))
|
||||
.nest("/posts", posts::router())
|
||||
.merge(posts::alias_router(state.posts.values()))
|
||||
.route("/healthcheck", get(healthcheck))
|
||||
.route("/metrics", get(metrics))
|
||||
.nest_service("/static", tower_http::services::ServeDir::new("./static"))
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{extract::{Path, State}, response::Html, routing::get, Extension, Router};
|
||||
use axum::{extract::{Path, State}, response::{Html, Redirect}, routing::get, Extension, Router};
|
||||
use serde_derive::Serialize;
|
||||
use tracing::{instrument, log::*};
|
||||
|
||||
|
@ -17,6 +17,21 @@ pub fn router() -> Router<Arc<AppState>> {
|
|||
.fallback_service(tower_http::services::ServeDir::new("./posts"))
|
||||
}
|
||||
|
||||
pub fn alias_router<'a>(posts: impl IntoIterator<Item=&'a Post>) -> Router<Arc<AppState>> {
|
||||
let mut router = Router::new();
|
||||
|
||||
for post in posts {
|
||||
for alias in &post.aliases {
|
||||
let path = post.absolute_path.to_owned();
|
||||
router = router.route(alias, get(move || async {
|
||||
let path = path;
|
||||
Redirect::permanent(&path)
|
||||
}));
|
||||
}
|
||||
}
|
||||
router
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct PageContext<'a> {
|
||||
title: &'a str,
|
||||
|
|
|
@ -27,7 +27,8 @@ async fn main() -> Result<()> {
|
|||
let posts = post::load_all().await?;
|
||||
let state = Arc::new(AppState { tera, posts });
|
||||
|
||||
let app = handlers::routes()
|
||||
|
||||
let app = handlers::routes(&state)
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.layer(CompressionLayer::new())
|
||||
.with_state(state);
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<li>✅ post metadata (frontmatter, toml)</li>
|
||||
<li>✅ app metrics (page hits, etc)</li>
|
||||
<li>✅ tests</li>
|
||||
<li>⬜ page aliases (redirects, for back-compat with old routes)</li>
|
||||
<li>✅ page aliases (redirects, for back-compat with old routes)</li>
|
||||
<li>⬜ sass compilation (using rsass? grass?)</li>
|
||||
<li>⬜ rss/atom/jsonfeed</li>
|
||||
<li>✅ proper error handling (i guess??)</li>
|
||||
|
|
Loading…
Reference in a new issue