💾 Archived View for lists.flounder.online › patches › threads › 20210320212418.6834-1-johann@qwertqw… captured on 2022-04-28 at 19:24:41. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
From: johann@qwertqwefsday.eu
Date: Sat, 20 Mar 2021 22:24:18 +0100
Message-Id: 20210320212418.6834-1-johann@qwertqwefsday.eu
To: <~aw/patches@lists.sr.ht>
In-Reply-To: CA2D7MNMZT84.35AY7H7GNAAI5@debian-alex
Cc: "Johann150" <johann@qwertqwefsday.eu>
--------------------------------------
From: Johann150 <johann@qwertqwefsday.eu>
---
v1 was: add 404 page
src/main.rs | 19 +++++++++++++++++++
templates/error.html | 8 ++++++++
2 files changed, 27 insertions(+)
create mode 100644 templates/error.html
diff --git a/src/main.rs b/src/main.rs
index fe97363..f7cdc3b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@ use git2::{
use once_cell::sync::Lazy;
use pico_args;
use serde::Deserialize;
+use std::convert::TryInto;
use std::fs;
use std::path::Path;
use std::str;
@@ -95,6 +96,24 @@ fn args() -> Config {
}
}
+#[derive(Template)]
+#[template(path = "error.html")]
+struct ErrorTemplate<'a> {
+ looking_for: &'a str,
+}
+
+fn error_page<S: TryInto<tide::StatusCode>>(status: S, looking_for: &str) -> tide::Result {
+ let body: tide::Body = ErrorTemplate { looking_for }.try_into().unwrap();
+
+ Ok(tide::Response::builder(
+ status
+ .try_into()
+ .unwrap_or(tide::StatusCode::InternalServerError),
+ )
+ .body(body)
+ .build())
+}
+
#[derive(Template)]
#[template(path = "index.html")] // using the template in this path, relative
struct IndexTemplate {
diff --git a/templates/error.html b/templates/error.html
new file mode 100644
index 0000000..bf2f071
--- /dev/null
+++ b/templates/error.html
@@ -0,0 +1,8 @@
+{% extends "base.html" %}
+
+{% block content %}
+ <div class="page-title"><h1>Hmm, that didn't work.</h1></div>
+ <div>
+ That thing you were looking for - "{{looking_for}}" - I can't give it to you. Something went wrong in getting it.
+ </div>
+{% endblock %}
--
2.20.1
From: alex@alexwennerberg.com
Date: Sat, 20 Mar 2021 20:34:37 -0700
Message-Id: CA2PPIBB4GFF.1FMC1W0AAYOGY@debian-alex
To: "Johann Galle" <johann@qwertqwefsday.eu>, <~aw/patches@lists.sr.ht>
In-Reply-To: 20210320212418.6834-1-johann@qwertqwefsday.eu
--------------------------------------
Not to nitpick too much, but I have two comments:
1. I like the old school style of including the status code in the
error -- "404 -- {description}"
2. I would make this page a bit more generic, something like:
<h1>Error</h1>
{{status_code}} -- {{description}}
Thanks for your patches!
On Sat Mar 20, 2021 at 2:24 PM PDT, Johann Galle wrote:
From: Johann150 <johann@qwertqwefsday.eu>
---
v1 was: add 404 page
src/main.rs | 19 +++++++++++++++++++
templates/error.html | 8 ++++++++
2 files changed, 27 insertions(+)
create mode 100644 templates/error.html
diff --git a/src/main.rs b/src/main.rs
index fe97363..f7cdc3b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@ use git2::{
use once_cell::sync::Lazy;
use pico_args;
use serde::Deserialize;
+use std::convert::TryInto;
use std::fs;
use std::path::Path;
use std::str;
@@ -95,6 +96,24 @@ fn args() -> Config {
}
}
+#[derive(Template)]
+#[template(path = "error.html")]
+struct ErrorTemplate<'a> {
+ looking_for: &'a str,
+}
+
+fn error_page<S: TryInto<tide::StatusCode>>(status: S, looking_for:
&str) -> tide::Result {
+ let body: tide::Body = ErrorTemplate { looking_for
}.try_into().unwrap();
+
+ Ok(tide::Response::builder(
+ status
+ .try_into()
+ .unwrap_or(tide::StatusCode::InternalServerError),
+ )
+ .body(body)
+ .build())
+}
+
#[derive(Template)]
#[template(path = "index.html")] // using the template in this path,
relative
struct IndexTemplate {
diff --git a/templates/error.html b/templates/error.html
new file mode 100644
index 0000000..bf2f071
--- /dev/null
+++ b/templates/error.html
@@ -0,0 +1,8 @@
+{% extends "base.html" %}
+
+{% block content %}
+ <div class="page-title"><h1>Hmm, that didn't work.</h1></div>
+ <div>
+ That thing you were looking for - "{{looking_for}}" - I can't give it
to you. Something went wrong in getting it.
+ </div>
+{% endblock %}
--
2.20.1
From: johann@qwertqwefsday.eu
Date: Sun, 21 Mar 2021 11:52:37 +0100
Message-Id: 20210321105237.4379-1-johann@qwertqwefsday.eu
To: <~aw/patches@lists.sr.ht>
In-Reply-To: CA2PPIBB4GFF.1FMC1W0AAYOGY@debian-alex
Cc: "Johann150" <johann@qwertqwefsday.eu>
--------------------------------------
From: Johann150 <johann@qwertqwefsday.eu>
---
v1 was: add 404 page
Cargo.lock | 1 +
Cargo.toml | 1 +
src/errorpage.rs | 31 +++++++++++++++++++++++++++++++
src/main.rs | 13 ++++++++++---
templates/error.html | 9 +++++++++
5 files changed, 52 insertions(+), 3 deletions(-)
create mode 100644 src/errorpage.rs
create mode 100644 templates/error.html
diff --git a/Cargo.lock b/Cargo.lock
index f4d2325..38149c0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1167,6 +1167,7 @@ dependencies = [
"askama",
"askama_tide",
"async-std",
+ "async-trait",
"chrono",
"git2",
"once_cell",
diff --git a/Cargo.toml b/Cargo.toml
index c16d847..624ebbc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,7 @@ anyhow = "1.0"
askama = {version = "0.10", features = ["with-tide"]}
askama_tide = "0.13"
async-std = { version = "1.8.0", features = ["attributes"] }
+async-trait = "0.1.48"
chrono = "0.4"
git2 = {version="0.13", default-features = false}
once_cell = "1.7.2"
diff --git a/src/errorpage.rs b/src/errorpage.rs
new file mode 100644
index 0000000..dbfcba7
--- /dev/null
+++ b/src/errorpage.rs
@@ -0,0 +1,31 @@
+use askama::Template;
+use tide::{Middleware, Next, Request, StatusCode};
+
+#[derive(Template)]
+#[template(path = "error.html")]
+struct ErrorTemplate {
+ resource: String,
+ status: StatusCode,
+ message: String,
+}
+
+pub struct ErrorToErrorpage;
+
+#[async_trait::async_trait]
+impl<State: Clone + Send + Sync + 'static> Middleware<State> for ErrorToErrorpage{
+ async fn handle(&self, req: Request<State>, next: Next<'_, State>) -> tide::Result {
+ let resource = req.url().path().to_string();
+ let mut response = next.run(req).await;
+ if let Some(err) = response.take_error() {
+ let status = err.status();
+ response = ErrorTemplate{
+ resource,
+ status,
+ message: err.into_inner().to_string(),
+ }.into();
+ response.set_status(status);
+ }
+
+ Ok(response)
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index d2f390a..c1073c1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,6 +13,8 @@ use syntect::highlighting::{Color, ThemeSet};
use syntect::parsing::{SyntaxReference, SyntaxSet};
use tide::Request;
+mod errorpage;
+
#[derive(Deserialize, Debug)]
pub struct Config {
#[serde(default = "defaults::port")]
@@ -130,14 +132,18 @@ struct RepoHomeTemplate {
readme_text: String,
}
-fn repo_from_request(repo_name: &str) -> Result<Repository> {
+fn repo_from_request(repo_name: &str) -> Result<Repository, tide::Error> {
let repo_name = percent_encoding::percent_decode_str(repo_name)
.decode_utf8_lossy()
.into_owned();
let repo_path = Path::new(&CONFIG.projectroot).join(repo_name);
// TODO CLEAN PATH! VERY IMPORTANT! DONT FORGET!
- let r = Repository::open(repo_path)?;
- Ok(r)
+ Repository::open(repo_path).or_else(|_| {
+ Err(tide::Error::from_str(
+ 404,
+ "This repository does not exist.",
+ ))
+ })
}
async fn repo_home(req: Request<()>) -> tide::Result {
@@ -469,6 +475,7 @@ mod filters {
async fn main() -> Result<(), std::io::Error> {
tide::log::start();
let mut app = tide::new();
+ app.with(errorpage::ErrorToErrorpage);
app.at("/").get(index);
app.at("/robots.txt")
.serve_file("templates/static/robots.txt")?; // TODO configurable
diff --git a/templates/error.html b/templates/error.html
new file mode 100644
index 0000000..7a2c946
--- /dev/null
+++ b/templates/error.html
@@ -0,0 +1,9 @@
+{% extends "base.html" %}
+
+{% block content %}
+ <div class="page-title"><h1>Hmm, that did not work.</h1></div>
+ <div>
+ Something went wrong in getting "{{resource}}":<br>
+ {{status}} — {{message}}
+ </div>
+{% endblock %}
--
2.20.1