Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed public directory in binary #433

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ headers = "0.3.9"
http = "0.2.10"
human-panic = "1.2.2"
hyper = { version = "0.14.27", features = ["deprecated"] }
include_dir = "0.7.3"
iso8601-timestamp = "0.2.12"
kitsune-blocking = { path = "../crates/kitsune-blocking" }
kitsune-cache = { path = "../crates/kitsune-cache" }
Expand All @@ -55,6 +56,7 @@ mimalloc = "0.1.39"
mime = "0.3.17"
mime_guess = { version = "2.0.4", default-features = false }
pkcs8 = "0.10.2"
once_cell = "1.18.0"
oxide-auth = "0.5.4"
oxide-auth-async = "0.1.1"
oxide-auth-axum = "0.3.0"
Expand Down
1 change: 1 addition & 0 deletions kitsune/src/http/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod oauth;
#[cfg(feature = "oidc")]
pub mod oidc;
pub mod posts;
pub mod public;
pub mod users;
pub mod well_known;
48 changes: 48 additions & 0 deletions kitsune/src/http/handler/public.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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::{collections::HashMap, path::Path as FsPath, sync::RwLock};

static PUBLIC_DIR: include_dir::Dir<'_> = include_dir!("public");
static PUBLIC_DIR_MIME_TYPE: Lazy<RwLock<HashMap<&'static FsPath, Mime>>> =
Lazy::new(RwLock::default);

#[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 {
return Either::E2(StatusCode::NOT_FOUND);
};

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()))
}

pub fn routes<T>() -> Router<T>
where
T: Clone + Send + Sync + 'static,
{
Router::new().route("/*path", routing::get(get))
}
6 changes: 4 additions & 2 deletions kitsune/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use self::{
handler::{confirm_account, custom_emojis, media, nodeinfo, oauth, posts, users, well_known},
handler::{
confirm_account, custom_emojis, media, nodeinfo, oauth, posts, public, users, well_known,
},
openapi::api_docs,
};
use crate::state::Zustand;
Expand Down Expand Up @@ -54,7 +56,7 @@ pub fn create_router(
.nest("/posts", posts::routes())
.nest("/users", users::routes())
.nest("/.well-known", well_known::routes())
.nest_service("/public", ServeDir::new("public"));
.nest("/public", public::routes());

#[cfg(feature = "oidc")]
{
Expand Down
Loading