From 79e88cb2f44ef74d1412e84ed648849958132ed1 Mon Sep 17 00:00:00 2001 From: Fries Date: Mon, 3 Jul 2023 23:59:56 -0700 Subject: [PATCH 1/7] add a WIP index page --- LICENSE | 64 ++++++++++++++++++++++---------------------- public/style.css | 5 ++++ src/assets.rs | 7 +++++ src/routes.rs | 6 ++--- templates/index.html | 26 ++++++++++++++++++ 5 files changed, 73 insertions(+), 35 deletions(-) create mode 100644 templates/index.html diff --git a/LICENSE b/LICENSE index 0e259d4..98d0b28 100644 --- a/LICENSE +++ b/LICENSE @@ -2,14 +2,14 @@ Creative Commons Legal Code CC0 1.0 Universal - CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE - LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN - ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS - INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES - REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS - PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM - THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED - HEREUNDER. + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. Statement of Purpose @@ -43,22 +43,22 @@ Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, - communicate, and translate a Work; + communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or - likeness depicted in a Work; + likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, - subject to the limitations in paragraph 4(a), below; + subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data - in a Work; + in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the - European Parliament and of the Council of 11 March 1996 on the legal - protection of databases, and under any national implementation - thereof, including any amended or successor version of such - directive); and + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and vii. other similar, equivalent or corresponding rights throughout the - world based on applicable law or treaty, and any national - implementations thereof. + world based on applicable law or treaty, and any national + implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, @@ -102,20 +102,20 @@ express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, - surrendered, licensed or otherwise affected by this document. + surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or - warranties of any kind concerning the Work, express, implied, - statutory or otherwise, including without limitation warranties of - title, merchantability, fitness for a particular purpose, non - infringement, or the absence of latent or other defects, accuracy, or - the present or absence of errors, whether or not discoverable, all to - the greatest extent permissible under applicable law. + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons - that may apply to the Work or any use thereof, including without - limitation any person's Copyright and Related Rights in the Work. - Further, Affirmer disclaims responsibility for obtaining any necessary - consents, permissions or other rights required for any use of the - Work. + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. d. Affirmer understands and acknowledges that Creative Commons is not a - party to this document and has no duty or obligation with respect to - this CC0 or use of the Work. + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/public/style.css b/public/style.css index b1801cb..a1019a2 100644 --- a/public/style.css +++ b/public/style.css @@ -23,6 +23,11 @@ body { color: var(--text-color) } +a { + color: var(--link-color); + font-size: 22px; +} + p { font-size: 22px; } diff --git a/src/assets.rs b/src/assets.rs index 35ea3a0..83579a0 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use askama_rocket::Template; use rocket::http::Status; use rust_embed::RustEmbed; +use shared::names::Site; #[derive(RustEmbed)] #[folder = "public/"] @@ -23,6 +24,12 @@ pub struct ErrorTemplate<'a> { pub error_description: &'a str } +#[derive(Template)] +#[template(path = "index.html")] +pub struct IndexTemplate { + pub sites: Vec +} + #[derive(Responder)] pub struct ErrorTemplateResponder<'a> { template: ErrorTemplate<'a> diff --git a/src/routes.rs b/src/routes.rs index 5c4c95b..ae59684 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,5 +1,5 @@ use crate::{ - assets::ErrorTemplate, + assets::{ErrorTemplate, IndexTemplate}, links::{next_url, previous_url}, sites::get_global_names, }; @@ -23,8 +23,8 @@ pub struct JsonResponse { } #[get("/")] -pub fn index() -> &'static str { - "Like, this is a webring, meow!" +pub async fn index() -> IndexTemplate { + IndexTemplate { sites: get_global_names().await } } #[get("/previous?")] diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..71bb1a4 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,26 @@ + + + + + + + Meowy Webring + + + + +
+

Welcome

+

Sites

+ {% for site in sites %} + {% match site.name %} + {% when Some with (value) %} +

{{ value }}

+ {% when None %} +

{{ site.url }}

+ {% endmatch %} + {% endfor %} +
+ + + From 6e9bbe1d6094e6ae97478528283b77f9fcab6ebe Mon Sep 17 00:00:00 2001 From: Fries Date: Wed, 5 Jul 2023 02:10:31 -0700 Subject: [PATCH 2/7] add cors and previous and next name functions --- Cargo.lock | 86 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 +++++ src/links.rs | 28 ++++++++++++++- src/main.rs | 2 ++ src/routes.rs | 25 ++++++++----- templates/index.html | 16 ++++----- 6 files changed, 148 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf0cf9b..930c0b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.3.2" @@ -443,6 +452,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "form_urlencoded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" +dependencies = [ + "percent-encoding", +] + [[package]] name = "fsevent-sys" version = "4.1.0" @@ -653,6 +671,16 @@ dependencies = [ "want", ] +[[package]] +name = "idna" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -831,6 +859,7 @@ dependencies = [ "log", "notify", "rocket", + "rocket_cors", "rust-embed", "serde", "serde_json", @@ -1161,6 +1190,8 @@ version = "1.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f" dependencies = [ + "aho-corasick", + "memchr", "regex-syntax 0.7.2", ] @@ -1240,6 +1271,20 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "rocket_cors" +version = "0.6.0-alpha2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b12771b47f52e34d5d0e0e444aeba382863e73263cb9e18847e7d5b74aa2cbd0" +dependencies = [ + "http", + "log", + "regex", + "rocket", + "unicase", + "url", +] + [[package]] name = "rocket_http" version = "0.5.0-rc.3" @@ -1579,6 +1624,21 @@ dependencies = [ "time-core", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.29.0" @@ -1775,18 +1835,44 @@ dependencies = [ "version_check", ] +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + [[package]] name = "unicode-ident" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-xid" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "url" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50bff7831e19200a85b17131d085c25d7811bc4e186efdaf54bbd132994a88cb" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 79e9b30..f3c97d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,11 @@ version = "0.1.0" edition = "2021" rust-version = "1.70" +[profile.dev] +lto = "thin" +[profile.release] +lto = "thin" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -51,3 +56,7 @@ default-features = false version = "6" default-features = false features = ["macos_fsevent"] + +[dependencies.rocket_cors] +version = "=0.6.0-alpha2" +default_features = false diff --git a/src/links.rs b/src/links.rs index fb742a3..d28f8f0 100644 --- a/src/links.rs +++ b/src/links.rs @@ -17,4 +17,30 @@ pub fn next_url(source_url: &String, names: &Vec) -> Option { } } -// TODO: previous_name, next_name +pub fn previous_name(source_url: &String, names: &Vec) -> Option { + match names.iter().position(|r| &r.url == source_url) { + Some(index) if index == 0 => match &names[names.len() - 1].name { + Some(name) => Some(name.clone()), + None => previous_url(source_url, names), + }, + Some(index) => match &names[index - 1].name { + Some(name) => Some(name.clone()), + None => previous_url(source_url, names), + }, + None => None, + } +} + +pub fn next_name(source_url: &String, names: &Vec) -> Option { + match names.iter().position(|r| &r.url == source_url) { + Some(index) if index == names.len() - 1 => match &names[names.len() - 1].name { + Some(name) => Some(name.clone()), + None => next_url(source_url, names), + }, + Some(index) => match &names[index + 1].name { + Some(name) => Some(name.clone()), + None => next_url(source_url, names), + }, + None => None, + } +} diff --git a/src/main.rs b/src/main.rs index cbbd994..5d86c16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,9 +14,11 @@ mod watcher; #[launch] async fn rocket() -> _ { init_names().unwrap(); + let cors = rocket_cors::CorsOptions::default().to_cors().unwrap(); tokio::task::spawn_blocking(hot_reloading); rocket::build() + .manage(cors) .mount( "/", routes![routes::index, routes::previous, routes::next, routes::name], diff --git a/src/routes.rs b/src/routes.rs index ae59684..6e87759 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,6 +1,6 @@ use crate::{ assets::{ErrorTemplate, IndexTemplate}, - links::{next_url, previous_url}, + links::{next_url, previous_url, previous_name, next_name}, sites::get_global_names, }; @@ -9,6 +9,7 @@ use rocket::{ response::Redirect, serde::{json::Json, Serialize}, }; +use rocket_cors::{Guard, Responder}; const NOT_FOUND_ERROR: ErrorTemplate = ErrorTemplate { error: "Not Found", @@ -23,8 +24,10 @@ pub struct JsonResponse { } #[get("/")] -pub async fn index() -> IndexTemplate { - IndexTemplate { sites: get_global_names().await } +pub async fn index() -> IndexTemplate { + IndexTemplate { + sites: get_global_names().await, + } } #[get("/previous?")] @@ -44,18 +47,22 @@ pub async fn next(source_url: String) -> Result { } #[get("/name?")] -pub async fn name(source_url: String) -> Result, Status> { - let previous_site_name = previous_url(&source_url, &get_global_names().await); - let next_site_name = next_url(&source_url, &get_global_names().await); +pub async fn name( + source_url: String, + cors: Guard<'_>, +) -> Responder, Status>> { + let previous_site_name = previous_name(&source_url, &get_global_names().await); + let next_site_name = next_name(&source_url, &get_global_names().await); if previous_site_name.is_none() && next_site_name.is_none() { - return Err(Status::NotFound); + return cors.responder(Err(Status::NotFound)); } - Ok(Json(JsonResponse { + let response = Ok(Json(JsonResponse { previous_site_name, next_site_name, - })) + })); + cors.responder(response) } #[catch(404)] diff --git a/templates/index.html b/templates/index.html index 71bb1a4..41aec12 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,14 +12,14 @@

Welcome

Sites

- {% for site in sites %} - {% match site.name %} - {% when Some with (value) %} -

{{ value }}

- {% when None %} -

{{ site.url }}

- {% endmatch %} - {% endfor %} + {% for site in sites %} + {% match site.name %} + {% when Some with (value) %} +

{{ value }}

+ {% when None %} +

{{ site.url }}

+ {% endmatch %} + {% endfor %}
From 74f3cd1cb42af4ecd1e77ed959b1f3ac291a37ae Mon Sep 17 00:00:00 2001 From: Fries Date: Fri, 7 Jul 2023 23:03:29 -0700 Subject: [PATCH 3/7] fix a bug with the next_name function looks like i used the previous_name logic for element of the vector i used which is wrong. --- Cargo.toml | 2 +- src/links.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f3c97d9..8bf10b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" rust-version = "1.70" [profile.dev] -lto = "thin" +lto = false [profile.release] lto = "thin" diff --git a/src/links.rs b/src/links.rs index d28f8f0..de60dd5 100644 --- a/src/links.rs +++ b/src/links.rs @@ -33,7 +33,7 @@ pub fn previous_name(source_url: &String, names: &Vec) -> Option { pub fn next_name(source_url: &String, names: &Vec) -> Option { match names.iter().position(|r| &r.url == source_url) { - Some(index) if index == names.len() - 1 => match &names[names.len() - 1].name { + Some(index) if index == names.len() - 1 => match &names[0].name { Some(name) => Some(name.clone()), None => next_url(source_url, names), }, From 4e17ef7dff5a389a9246bff40f7fe9647574fd2f Mon Sep 17 00:00:00 2001 From: Fries Date: Sun, 9 Jul 2023 22:09:07 -0700 Subject: [PATCH 4/7] update stylesheet and clean up asset code the stylesheet got a new update from my websites stylesheet with the bigger font sizes and i split the hyperlegible font face css into its own file and made the asset code cleaner. --- public/hyperlegible.css | 43 ++++++++++++++++++++++++ public/style.css | 73 +++++++++++++++++------------------------ src/assets.rs | 54 +++++++++++++++++++++--------- templates/base.html | 16 +++++++++ templates/error.html | 25 ++++++-------- templates/index.html | 40 +++++++++------------- 6 files changed, 152 insertions(+), 99 deletions(-) create mode 100644 public/hyperlegible.css create mode 100644 templates/base.html diff --git a/public/hyperlegible.css b/public/hyperlegible.css new file mode 100644 index 0000000..eca87f6 --- /dev/null +++ b/public/hyperlegible.css @@ -0,0 +1,43 @@ +@font-face { + font-family: Atkinson Hyperlegible; + font-style: normal; + font-display: swap; + font-weight: 400; + src: url(/public/woff2/atkinson-hyperlegible-latin-ext-400-normal.woff2) format("woff2"), + url(/public/woff/atkinson-hyperlegible-all-400-normal.woff) format("woff"); + unicode-range: U+0100-024F, + U+0259, + U+1E00-1EFF, + U+2020, + U+20A0-20AB, + U+20AD-20CF, + U+2113, + U+2C60-2C7F, + U+A720-A7FF +} + +@font-face { + font-family: Atkinson Hyperlegible; + font-style: normal; + font-display: swap; + font-weight: 400; + src: url(/public/woff2/atkinson-hyperlegible-latin-400-normal.woff2) format("woff2"), + url(/public/woff/atkinson-hyperlegible-all-400-normal.woff) format("woff"); + unicode-range: U+0000-00FF, + U+0131, + U+0152-0153, + U+02BB-02BC, + U+02C6, + U+02DA, + U+02DC, + U+2000-206F, + U+2074, + U+20AC, + U+2122, + U+2191, + U+2193, + U+2212, + U+2215, + U+FEFF, + U+FFFD +} diff --git a/public/style.css b/public/style.css index a1019a2..2674a9d 100644 --- a/public/style.css +++ b/public/style.css @@ -14,64 +14,51 @@ } } +:root { + --h1-font-size: 3.225rem; + --h2-font-size: 2.825rem; + --h3-font-size: 2.225rem; + --h4-font-size: 1.665rem; + --default-font-size: 1.375rem; + --h6-font-size: 1.185rem; +} + body { font-family: "Atkinson Hyperlegible", sans-serif; text-align: center; + font-size: var(--default-font-size); max-width: 600px; margin: auto; background-color: var(--background-color); color: var(--text-color) } + a { + font-size: var(--default-font-size); color: var(--link-color); - font-size: 22px; } -p { - font-size: 22px; +h1 { + font-size: var(--h1-font-size); } -@font-face { - font-family: Atkinson Hyperlegible; - font-style: normal; - font-display: swap; - font-weight: 400; - src: url(/public/woff2/atkinson-hyperlegible-latin-ext-400-normal.woff2) format("woff2"), - url(/public/woff/atkinson-hyperlegible-all-400-normal.woff) format("woff"); - unicode-range: U+0100-024F, - U+0259, - U+1E00-1EFF, - U+2020, - U+20A0-20AB, - U+20AD-20CF, - U+2113, - U+2C60-2C7F, - U+A720-A7FF +h2 { + font-size: var(--h2-font-size); +} + +h3 { + font-size: var(--h3-font-size); } -@font-face { - font-family: Atkinson Hyperlegible; - font-style: normal; - font-display: swap; - font-weight: 400; - src: url(/public/woff2/atkinson-hyperlegible-latin-400-normal.woff2) format("woff2"), - url(/public/woff/atkinson-hyperlegible-all-400-normal.woff) format("woff"); - unicode-range: U+0000-00FF, - U+0131, - U+0152-0153, - U+02BB-02BC, - U+02C6, - U+02DA, - U+02DC, - U+2000-206F, - U+2074, - U+20AC, - U+2122, - U+2191, - U+2193, - U+2212, - U+2215, - U+FEFF, - U+FFFD +h4 { + font-size: var(--h4-font-size); +} + +h5 { + font-size: var(--default-font-size); +} + +h6 { + font-size: var(--h6-font-size); } diff --git a/src/assets.rs b/src/assets.rs index 83579a0..683cd94 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -1,14 +1,29 @@ -use std::borrow::Cow; - use askama_rocket::Template; -use rocket::http::Status; -use rust_embed::RustEmbed; +use rocket::{http::Status, response::content::RawCss}; +use rust_embed::{EmbeddedFile, RustEmbed}; use shared::names::Site; +use std::borrow::Cow; #[derive(RustEmbed)] #[folder = "public/"] pub struct PublicAssets; +impl PublicAssets { + fn get_asset(file_path: &str) -> Result { + match PublicAssets::get(file_path) { + Some(asset) => Ok(asset), + None => Err(Status::NotFound), + } + } + fn get_string(file_path: &str) -> Result { + let asset = PublicAssets::get_asset(file_path)?; + match std::str::from_utf8(&asset.data) { + Ok(string) => Ok(string.to_string()), + Err(_) => Err(Status::InternalServerError), + } + } +} + #[derive(Responder)] #[response(status = 200, content_type = "font/woff2")] pub struct RawWoff2Font(pub Cow<'static, [u8]>); @@ -21,26 +36,33 @@ pub struct RawWoffFont(pub Cow<'static, [u8]>); #[template(path = "error.html")] pub struct ErrorTemplate<'a> { pub error: &'a str, - pub error_description: &'a str + pub error_description: &'a str, } #[derive(Template)] #[template(path = "index.html")] pub struct IndexTemplate { - pub sites: Vec + pub sites: Vec, } #[derive(Responder)] pub struct ErrorTemplateResponder<'a> { - template: ErrorTemplate<'a> + template: ErrorTemplate<'a>, } -#[get("/style.css")] -pub fn style() -> Result, Status> { - let style = PublicAssets::get("style.css").unwrap(); - match std::str::from_utf8(&style.data) { - Ok(style) => Ok(rocket::response::content::RawCss::(style.to_string())), - Err(_) => Err(Status::InternalServerError), +#[get("/css/ + {% endblock %} From 70bb0691d30e7c7a810801cf953c7af297dd021b Mon Sep 17 00:00:00 2001 From: Fries Date: Tue, 11 Jul 2023 02:37:31 -0700 Subject: [PATCH 7/7] implement a CachedResponse Responder struct this adds a Cache-Control header to existing responses so they can be cached in the browser. cache busting means this cache can be immutable as if the file changes, the filename changes, so the browser will get new files. --- src/assets/routes.rs | 52 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/assets/routes.rs b/src/assets/routes.rs index 35fa391..db2be30 100644 --- a/src/assets/routes.rs +++ b/src/assets/routes.rs @@ -1,5 +1,9 @@ use super::{files::get_file_wrapper, templates::ErrorTemplate}; -use rocket::{http::Status, response::content::RawCss}; +use rocket::{ + http::{Header, Status}, + response::{self, content::RawCss, Responder}, + Response, +}; use std::borrow::Cow; #[derive(Responder)] @@ -15,8 +19,32 @@ pub struct ErrorTemplateResponder<'a> { template: ErrorTemplate<'a>, } +pub struct CachedResponse { + inner: T, +} + +impl<'r, T> Responder<'r, 'static> for CachedResponse +where + T: Responder<'r, 'static>, +{ + fn respond_to(self, request: &'r rocket::Request<'_>) -> response::Result<'static> { + Response::build_from(self.inner.respond_to(request)?) + .header(Header::new("Cache-Control", "max-age=31536000, immutable")) + .ok() + } +} + +impl<'r, T> From for CachedResponse +where + T: Responder<'r, 'static>, +{ + fn from(value: T) -> Self { + CachedResponse { inner: value } + } +} + #[get("/css/