Skip to content

Commit

Permalink
Use rust-embed instead of include_dir! (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra authored Dec 8, 2023
1 parent c3f5c29 commit 3cd7dba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 56 deletions.
28 changes: 4 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ license = false
eula = false

[dependencies]
ahash = "0.8.6"
athena = { path = "../lib/athena" }
argon2 = { version = "0.5.2", features = ["std"] }
askama = { version = "0.12.1", features = [
Expand All @@ -40,7 +39,6 @@ futures-util = "0.3.29"
headers = "0.3.9"
http = "0.2.11"
hyper = { version = "0.14.27", features = ["deprecated"] }
include_dir = "0.7.3"
iso8601-timestamp = "0.2.13"
kitsune-activitypub = { path = "../crates/kitsune-activitypub" }
kitsune-blocking = { path = "../crates/kitsune-blocking" }
Expand Down Expand Up @@ -72,10 +70,10 @@ mimalloc = "0.1.39"
mime = "0.3.17"
mime_guess = { version = "2.0.4", default-features = false }
pkcs8 = "0.10.2"
once_cell = "1.19.0"
oxide-auth = "0.5.4"
oxide-auth-async = "0.1.1"
oxide-auth-axum = "0.3.0"
rust-embed = { version = "8.1.0", features = ["include-exclude"] }
scoped-futures = "0.1.3"
serde = { version = "1.0.193", features = ["derive"] }
serde_urlencoded = "0.7.1"
Expand Down
40 changes: 11 additions & 29 deletions kitsune/src/http/handler/public.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
use ahash::AHashMap;
use std::borrow::Cow;

use axum::{extract::Path, routing, Router, TypedHeader};
use axum_extra::either::Either;
use headers::ContentType;
use http::StatusCode;
use include_dir::include_dir;
use mime::Mime;
use once_cell::sync::Lazy;
use std::{path::Path as FsPath, sync::RwLock};
use rust_embed::RustEmbed;

static PUBLIC_DIR: include_dir::Dir<'_> = include_dir!("public");
static PUBLIC_DIR_MIME_TYPE: Lazy<RwLock<AHashMap<&'static FsPath, Mime>>> =
Lazy::new(RwLock::default);
#[derive(RustEmbed)]
#[folder = "../public"]
#[exclude = "*.scss"]
struct AssetsDir;

#[allow(clippy::unused_async)]
async fn get(
Path(path): Path<String>,
) -> Either<(TypedHeader<ContentType>, &'static [u8]), StatusCode> {
let Some(file) = PUBLIC_DIR.get_file(path) else {
) -> Either<(TypedHeader<ContentType>, Cow<'static, [u8]>), StatusCode> {
let Some(file) = AssetsDir::get(&path) else {
return Either::E2(StatusCode::NOT_FOUND);
};
let mime_type = mime_guess::from_path(&path).first_or_octet_stream();

let mime_type = PUBLIC_DIR_MIME_TYPE
.read()
.unwrap()
.get(file.path())
.map(Mime::clone);

let mime_type = if let Some(mime_type) = mime_type {
mime_type
} else {
let mime_type = mime_guess::from_path(file.path()).first_or_octet_stream();
PUBLIC_DIR_MIME_TYPE
.write()
.unwrap()
.insert(file.path(), mime_type.clone());

mime_type
};

Either::E1((TypedHeader(ContentType::from(mime_type)), file.contents()))
Either::E1((TypedHeader(ContentType::from(mime_type)), file.data))
}

pub fn routes<T>() -> Router<T>
Expand Down

0 comments on commit 3cd7dba

Please sign in to comment.