move asset stuff to its own crate

this should seperate out the asset handling code, which was practically
its own module inside the main codebase, into a little crate.
This commit is contained in:
Fries 2023-07-12 22:10:08 -07:00
parent f4e9ef6af9
commit 60c2ac15e7
23 changed files with 129 additions and 180 deletions

103
Cargo.lock generated
View File

@ -17,15 +17,6 @@ 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"
@ -452,15 +443,6 @@ 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"
@ -677,16 +659,6 @@ 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"
@ -857,20 +829,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "meowy-webring"
version = "0.2.0"
name = "meowy-assets"
version = "0.1.0"
dependencies = [
"askama",
"askama_rocket",
"hex",
"notify",
"proc_macros",
"rocket",
"rocket_cors",
"rust-embed",
"sha2",
"shared",
]
[[package]]
name = "meowy-webring"
version = "0.2.0"
dependencies = [
"meowy-assets",
"notify",
"rocket",
"serde",
"serde_json",
"sha2",
"shared",
"simple_logger",
]
@ -1206,8 +1186,6 @@ 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",
]
@ -1287,20 +1265,6 @@ 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"
@ -1650,21 +1614,6 @@ 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"
@ -1861,44 +1810,18 @@ 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"

View File

@ -7,44 +7,22 @@ version = "0.2.0"
edition = "2021"
rust-version = "1.70"
[profile.dev]
lto = false
[profile.release]
lto = "thin"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hex = "0.4"
[dependencies.rocket]
version = "=0.5.0-rc.3"
default-features = false
features = ["json"]
[dependencies.rust-embed]
version = "6"
features = ["debug-embed"]
[dependencies.serde]
version = "1.0"
[dependencies.serde_json]
version = "1.0"
[dependencies.askama_rocket]
git = "https://github.com/djc/askama.git"
package = "askama_rocket"
rev = "b9e51601560398766eac445517fb17c35090a952"
default-features = false
[dependencies.askama]
git = "https://github.com/djc/askama.git"
package = "askama"
rev = "b9e51601560398766eac445517fb17c35090a952"
version = "0.12"
default-features = false
[dependencies.shared]
path = "./crates/shared"
@ -57,13 +35,5 @@ version = "6"
default-features = false
features = ["macos_fsevent"]
[dependencies.rocket_cors]
version = "=0.6.0-alpha2"
default_features = false
[dependencies.proc_macros]
path = "./crates/proc-macros"
[dependencies.sha2]
version = "0.10"
features = ["asm"]
[dependencies.meowy-assets]
path = "./crates/meowy-assets"

View File

@ -0,0 +1,40 @@
[package]
name = "meowy-assets"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hex = "0.4"
[dependencies.rocket]
version = "=0.5.0-rc.3"
default-features = false
[dependencies.askama_rocket]
git = "https://github.com/djc/askama.git"
package = "askama_rocket"
rev = "b9e51601560398766eac445517fb17c35090a952"
default-features = false
[dependencies.askama]
git = "https://github.com/djc/askama.git"
package = "askama"
rev = "b9e51601560398766eac445517fb17c35090a952"
version = "0.12"
default-features = false
[dependencies.rust-embed]
version = "6"
features = ["debug-embed"]
[dependencies.sha2]
version = "0.10"
features = ["asm"]
[dependencies.shared]
path = "../shared"
[dependencies.proc_macros]
path = "../proc-macros"

View File

@ -62,7 +62,7 @@ impl GetFile for BinaryFile {
};
Some(BinaryFile {
data: file.data,
metadata: metadata,
metadata,
})
}
None => None,

View File

@ -3,6 +3,9 @@ mod responders;
mod routes;
pub mod templates;
#[macro_use]
extern crate rocket;
pub use routes::style;
pub use routes::woff2_font;
pub use routes::woff_font;

View File

@ -39,6 +39,6 @@ where
T: Responder<'r, 'static>,
{
fn from(value: T) -> Self {
CachedResponse { inner: value }
Self { inner: value }
}
}

View File

@ -1,5 +1,7 @@
use super::files::get_file_wrapper;
use crate::assets::responders::{CachedResponse, RawWoff2Font, RawWoffFont};
use crate::{
files::get_file_wrapper,
responders::{CachedResponse, RawWoff2Font, RawWoffFont},
};
use rocket::{http::Status, response::content::RawCss};
#[get("/css/<style>")]
@ -11,13 +13,9 @@ pub fn style(style: &str) -> Result<CachedResponse<RawCss<String>>, Status> {
let hyperlegible_name = hyperlegible_file.metadata.get_hash_filename();
if style == style_name {
Ok(CachedResponse::from(RawCss::<String>(
style_file.text.clone(),
)))
Ok(RawCss::<String>(style_file.text.clone()).into())
} else if style == hyperlegible_name {
Ok(CachedResponse::from(RawCss::<String>(
hyperlegible_file.text.clone(),
)))
Ok(RawCss::<String>(hyperlegible_file.text.clone()).into())
} else {
Err(Status::NotFound)
}
@ -32,11 +30,9 @@ pub fn woff2_font(font: &str) -> Result<CachedResponse<RawWoff2Font>, Status> {
let latin_ext = latin_file.metadata.get_hash_filename();
if font == latin {
Ok(CachedResponse::from(RawWoff2Font(latin_file.data.clone())))
Ok(RawWoff2Font(latin_file.data.clone()).into())
} else if font == latin_ext {
Ok(CachedResponse::from(RawWoff2Font(
latin_ext_file.data.clone(),
)))
Ok(RawWoff2Font(latin_ext_file.data.clone()).into())
} else {
Err(Status::NotFound)
}
@ -48,7 +44,7 @@ pub fn woff_font(font: &str) -> Result<CachedResponse<RawWoffFont>, Status> {
let all = all_file.metadata.get_hash_filename();
if font == all {
Ok(CachedResponse::from(RawWoffFont(all_file.data.clone())))
Ok(RawWoffFont(all_file.data.clone()).into())
} else {
Err(Status::NotFound)
}

View File

@ -6,6 +6,7 @@ pub struct BaseTemplate {
pub hyperlegible_filename: String,
pub style_filename: String,
}
#[derive(Template)]
#[template(path = "hyperlegible.css", escape = "none")]
pub(super) struct HyperlegibleTemplate {

View File

@ -5,31 +5,22 @@ use syn::{parse::Parser, parse_macro_input, DeriveInput};
#[proc_macro_attribute]
pub fn uses_base_template(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(item as DeriveInput);
let base_template_field = syn::Field::parse_named
.parse2(
quote! {
pub base_template: BaseTemplate
}
.into(),
)
.parse2(quote! {
pub base_template: BaseTemplate
})
.unwrap();
match &mut input.data {
syn::Data::Struct(ref mut struct_data) => {
match &mut struct_data.fields {
syn::Fields::Named(fields) => {
fields.named.push(base_template_field);
}
_ => (),
}
return quote! {
#input
}
.into();
if let syn::Data::Struct(ref mut struct_data) = &mut input.data {
if let syn::Fields::Named(fields) = &mut struct_data.fields {
fields.named.push(base_template_field);
}
_ => {
panic!("bad")
quote! {
#input
}
};
.into()
} else {
panic!("bad")
}
}

View File

@ -6,7 +6,7 @@ pub enum ErrorStatus {
LoggerInitializationError,
NotFoundError,
AlreadyExistsError,
GenericError
GenericError,
}
pub struct Error {

View File

@ -1,3 +1,3 @@
pub mod names;
pub mod errors;
pub mod directories;
pub mod errors;
pub mod names;

View File

@ -1,13 +1,13 @@
use crate::watcher::hot_reloading;
use assets::files::initialize_files;
use meowy_assets::files::initialize_files;
use rocket::tokio;
use sites::init_names;
#[macro_use]
extern crate rocket;
mod assets;
mod links;
mod responders;
mod routes;
mod sites;
mod watcher;
@ -16,11 +16,9 @@ mod watcher;
async fn rocket() -> _ {
init_names().unwrap();
initialize_files();
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],
@ -28,6 +26,10 @@ async fn rocket() -> _ {
.register("/", catchers![routes::not_found])
.mount(
"/public",
routes![assets::style, assets::woff2_font, assets::woff_font],
routes![
meowy_assets::style,
meowy_assets::woff2_font,
meowy_assets::woff_font
],
)
}

25
src/responders.rs Normal file
View File

@ -0,0 +1,25 @@
use rocket::{http::Header, response::Responder, Response};
pub struct CorsResponse<T> {
pub inner: T,
}
impl<'r, T> Responder<'r, 'static> for CorsResponse<T>
where
T: Responder<'r, 'static>,
{
fn respond_to(self, request: &'r rocket::Request<'_>) -> rocket::response::Result<'static> {
Response::build_from(self.inner.respond_to(request)?)
.header(Header::new("Access-Control-Allow-Origin", "*"))
.ok()
}
}
impl<'r, T> From<T> for CorsResponse<T>
where
T: Responder<'r, 'static>,
{
fn from(value: T) -> Self {
Self { inner: value }
}
}

View File

@ -1,17 +1,17 @@
use crate::{
assets::{
files::{get_file_wrapper, FileMetadata},
templates::{BaseTemplate, ErrorTemplate, IndexTemplate},
},
links::{next_name, next_url, previous_name, previous_url},
responders::CorsResponse,
sites::get_global_names,
};
use meowy_assets::{
files::{get_file_wrapper, FileMetadata},
templates::{BaseTemplate, ErrorTemplate, IndexTemplate},
};
use rocket::{
http::Status,
response::Redirect,
serde::{json::Json, Serialize},
};
use rocket_cors::{Guard, Responder};
fn get_hash_filename(metadata: &FileMetadata) -> Result<String, Status> {
Ok(metadata.get_hash_filename())
@ -75,22 +75,19 @@ pub async fn next(source_url: String) -> Result<Redirect, Status> {
}
#[get("/name?<source_url>")]
pub async fn name(
source_url: String,
cors: Guard<'_>,
) -> Responder<Result<Json<JsonResponse>, Status>> {
pub async fn name(source_url: String) -> Result<CorsResponse<Json<JsonResponse>>, 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 cors.responder(Err(Status::NotFound));
return Err(Status::NotFound);
}
let response = Ok(Json(JsonResponse {
Ok(Json(JsonResponse {
previous_site_name,
next_site_name,
}));
cors.responder(response)
})
.into())
}
#[catch(404)]

View File

@ -36,5 +36,6 @@ pub fn init_names() -> Result<(), Error> {
fn get_names() -> Result<Vec<Site>, Error> {
let names_path = directories::get_names_path()?;
let names_file = names::read_names_file(&names_path)?;
println!("sus");
names::load_names(names_file)
}