Skip to content

Commit

Permalink
add how to create to not found page
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Sep 6, 2020
1 parent 563329c commit 874cceb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/handler/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::handler_helpers::is_all;
use crate::helpers::{read_obsoleted_map, read_title_map};
use crate::page_id::PageId;
use crate::page_title::PageTitle;
use crate::template::{PageItemTemplate, TitleTemplate};
use crate::template::{PageItemTemplate, TitleNotFoundTemplate, TitleTemplate};
use crate::url_helpers::{page_url, title_url};
use actix_web::HttpResponse;
use askama::Template;
Expand Down Expand Up @@ -41,6 +41,13 @@ pub async fn title(req: actix_web::HttpRequest) -> std::io::Result<HttpResponse>
Ok(HttpResponse::Ok().content_type("text/html").body(html))
}
} else {
Ok(HttpResponse::NotFound().body("Not Found"))
let template = TitleNotFoundTemplate {
title: title.as_str(),
title_url: &title_url(&title),
};
let html = template.render().unwrap();
Ok(HttpResponse::NotFound()
.content_type("text/html")
.body(html))
}
}
7 changes: 7 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ pub struct TitleTemplate<'a> {
pub title_url: &'a str,
pub pages: &'a [PageItemTemplate],
}

#[derive(Template)]
#[template(path = "title-not-found.html")]
pub struct TitleNotFoundTemplate<'a> {
pub title: &'a str,
pub title_url: &'a str,
}
27 changes: 27 additions & 0 deletions templates/title-not-found.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
</head>
<body>
<header class="page-header">
<h1><a href="{{ title_url }}">{{ title }}</a></h1>
<nav class="breadcrumbs">
<ul>
<li><a href="/">/</a></li>
<li><a href="/titles">titles</a></li>
<li><a href="{{ title_url }}">{{ title }}</a></li>
</ul>
</nav>
</header>
<main class="page-body">
<p>The title was not found</p>
<p>
You can create a memo with that title using the following command:
<pre><code>rust-memo new --title {{title}}</code></pre>
</p>
</main>
<footer class="page-footer"></footer>
</body>
</html>

0 comments on commit 874cceb

Please sign in to comment.