From bf9d82ed9fa17c66a770593974f6ddc12728fd08 Mon Sep 17 00:00:00 2001 From: catvayor Date: Mon, 17 Jun 2024 09:51:44 +0200 Subject: [PATCH] custom errors --- Cargo.lock | 61 +++++++++++- Cargo.toml | 6 +- src/main.rs | 29 +++++- src/track.rs | 196 +++++++++++++++++++++----------------- static/errors/400.html | 19 ++++ static/errors/401.html | 19 ++++ static/errors/402.html | 19 ++++ static/errors/404.html | 19 ++++ static/errors/405.html | 19 ++++ static/errors/418.html | 19 ++++ static/errors/498.html | 19 ++++ static/errors/500.html | 19 ++++ static/errors/501.html | 19 ++++ static/errors/503.html | 19 ++++ static/errors/cat/400.jpg | Bin 0 -> 34757 bytes static/errors/cat/401.jpg | Bin 0 -> 30345 bytes static/errors/cat/402.jpg | Bin 0 -> 34385 bytes static/errors/cat/404.jpg | Bin 0 -> 38532 bytes static/errors/cat/405.jpg | Bin 0 -> 25369 bytes static/errors/cat/418.jpg | Bin 0 -> 21871 bytes static/errors/cat/498.jpg | Bin 0 -> 39542 bytes static/errors/cat/500.jpg | Bin 0 -> 33063 bytes static/errors/cat/501.jpg | Bin 0 -> 30657 bytes static/errors/cat/503.jpg | Bin 0 -> 26282 bytes templates/admin.html.hbs | 46 ++++----- 25 files changed, 414 insertions(+), 114 deletions(-) create mode 100644 static/errors/400.html create mode 100644 static/errors/401.html create mode 100644 static/errors/402.html create mode 100644 static/errors/404.html create mode 100644 static/errors/405.html create mode 100644 static/errors/418.html create mode 100644 static/errors/498.html create mode 100644 static/errors/500.html create mode 100644 static/errors/501.html create mode 100644 static/errors/503.html create mode 100644 static/errors/cat/400.jpg create mode 100644 static/errors/cat/401.jpg create mode 100644 static/errors/cat/402.jpg create mode 100644 static/errors/cat/404.jpg create mode 100644 static/errors/cat/405.jpg create mode 100644 static/errors/cat/418.jpg create mode 100644 static/errors/cat/498.jpg create mode 100644 static/errors/cat/500.jpg create mode 100644 static/errors/cat/501.jpg create mode 100644 static/errors/cat/503.jpg diff --git a/Cargo.lock b/Cargo.lock index c8c50b1..30f1c41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,16 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bstr" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bytemuck" version = "1.16.0" @@ -432,6 +442,19 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "globset" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata 0.4.6", + "regex-syntax 0.8.3", +] + [[package]] name = "h2" version = "0.3.26" @@ -1154,6 +1177,41 @@ dependencies = [ "uncased", ] +[[package]] +name = "rust-embed" +version = "8.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19549741604902eb99a7ed0ee177a0663ee1eda51a29f71401f166e47e77806a" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "8.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb9f96e283ec64401f30d3df8ee2aaeb2561f34c824381efa24a35f79bf40ee4" +dependencies = [ + "proc-macro2", + "quote", + "rust-embed-utils", + "syn", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "8.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c74a686185620830701348de757fd36bef4aa9680fd23c49fc539ddcc1af32" +dependencies = [ + "globset", + "sha2", + "walkdir", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1564,11 +1622,12 @@ dependencies = [ [[package]] name = "traque" -version = "0.1.0" +version = "0.2.4" dependencies = [ "rand", "rocket", "rocket_dyn_templates", + "rust-embed", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 136c681..e337b5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "traque" -version = "0.1.0" +version = "0.2.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -8,6 +8,10 @@ edition = "2021" [dependencies] rand = "0.8.5" +[dependencies.rust-embed] +version = "8.4.0" +features = ["include-exclude"] + [dependencies.rocket] version = "0.5.0" features = ["json"] diff --git a/src/main.rs b/src/main.rs index fea3101..c9da8df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,16 @@ extern crate rocket; use rocket::{ fairing::AdHoc, fs::{relative, FileServer}, - response::stream::Event, + http::Status, + response::{content, stream::Event}, tokio::{ self, select, time::{self, Duration}, }, + Request, }; use rocket_dyn_templates::Template; +use rust_embed::Embed; use std::{ collections::VecDeque, sync::{Arc, RwLock}, @@ -68,6 +71,27 @@ fn clean_expired_evt( } } +#[derive(Embed)] +#[folder = "static/errors"] +#[include = "*.html"] +#[exclude = "*.jpg"] +struct ErrorPages; + +#[catch(default)] +fn handler(status: Status, _: &Request) -> content::RawHtml { + content::RawHtml( + std::str::from_utf8( + ErrorPages::get(format!("{}.html", status.code).as_str()) + //501 not implemented + .unwrap_or(ErrorPages::get("501.html").unwrap()) + .data + .as_ref(), + ) + .unwrap() + .to_string(), + ) +} + #[launch] async fn rocket() -> _ { let rocket = rocket::build(); @@ -106,7 +130,8 @@ async fn rocket() -> _ { .manage(admin_evt_queue.clone()) .mount("/", routes![index]) .mount("/track", track::routes()) - .mount("/admin", admin::routes()); + .mount("/admin", admin::routes()) + .register("/", catchers![handler]); if config.serve_static { rocket = rocket.mount("/", FileServer::from(relative!("static"))); } diff --git a/src/track.rs b/src/track.rs index 4f4af99..b246e79 100644 --- a/src/track.rs +++ b/src/track.rs @@ -1,4 +1,5 @@ use rocket::{ + http::Status, response::stream::{Event, EventStream}, tokio::{ self, select, @@ -10,15 +11,20 @@ use rocket_dyn_templates::{context, Template}; use crate::global::{TrackedState::*, *}; +#[get("/")] +fn no_id() -> Status { + Status::ImATeapot +} + #[get("/?&")] fn tracked_view( id: &str, gpslog: Option, dbg: Option, tracking: &State, -) -> Option