mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-09-07 00:01:49 +00:00
57 lines
1.5 KiB
Rust
57 lines
1.5 KiB
Rust
/*
|
|
* Author & Copyright: Ruben Fiszel 2021
|
|
* This file and its contents are licensed under the AGPLv3 License.
|
|
* Please see the included NOTICE for copyright information and
|
|
* LICENSE-AGPL for a copy of the license.
|
|
*/
|
|
|
|
use axum::{
|
|
body::{self, BoxBody},
|
|
http::{header, Response, Uri},
|
|
response::IntoResponse,
|
|
};
|
|
|
|
use rust_embed::RustEmbed;
|
|
|
|
// static_handler is a handler that serves static files from the
|
|
pub async fn static_handler(uri: Uri) -> impl IntoResponse {
|
|
let path = uri.path().trim_start_matches('/').to_string();
|
|
StaticFile(path)
|
|
}
|
|
|
|
#[derive(RustEmbed)]
|
|
#[folder = "../frontend/build/"]
|
|
struct Asset;
|
|
pub struct StaticFile<T>(pub T);
|
|
|
|
impl<T> IntoResponse for StaticFile<T>
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
fn into_response(self) -> Response<BoxBody> {
|
|
let path = self.0.into();
|
|
serve_path(path)
|
|
}
|
|
}
|
|
|
|
fn serve_path(path: String) -> Response<BoxBody> {
|
|
if path.starts_with("api/") {
|
|
return Response::builder()
|
|
.status(404)
|
|
.body(body::boxed(body::Empty::new()))
|
|
.unwrap();
|
|
}
|
|
match Asset::get(path.as_str()) {
|
|
Some(content) => {
|
|
let body = body::boxed(body::Full::from(content.data));
|
|
let mime = mime_guess::from_path(path).first_or_octet_stream();
|
|
Response::builder()
|
|
.header(header::CONTENT_TYPE, mime.as_ref())
|
|
.header(header::CACHE_CONTROL, "max-age=3600".to_owned())
|
|
.body(body)
|
|
.unwrap()
|
|
}
|
|
None => serve_path("200.html".to_owned()),
|
|
}
|
|
}
|