-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add base path handling * add test for studio behind base path
- Loading branch information
1 parent
82541f5
commit 766347e
Showing
12 changed files
with
127 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,121 @@ | ||
use crate::config::Config; | ||
use crate::routes::ServerResult; | ||
use crate::server_error::ServerError; | ||
use axum::extract::Path; | ||
use axum::response::IntoResponse; | ||
use axum_extra::headers::ContentType; | ||
use include_dir::include_dir; | ||
use include_dir::Dir; | ||
use reqwest::StatusCode; | ||
use std::sync::OnceLock; | ||
|
||
static AGDB_STUDIO: Dir = include_dir!("agdb_studio/dist"); | ||
static AGDB_STUDIO_INDEX_JS: OnceLock<String> = OnceLock::new(); | ||
static AGDB_STUDIO_INDEX_HTML: OnceLock<String> = OnceLock::new(); | ||
|
||
pub(crate) fn init(config: &Config) { | ||
AGDB_STUDIO_INDEX_JS.get_or_init(|| { | ||
let index = AGDB_STUDIO | ||
.get_dir("assets") | ||
.expect("assets dir not found") | ||
.files() | ||
.find(|f| { | ||
f.path().extension().unwrap_or_default() == "js" | ||
&& f.path() | ||
.file_stem() | ||
.unwrap_or_default() | ||
.to_string_lossy() | ||
.starts_with("index") | ||
}) | ||
.expect("index.js not found") | ||
.contents_utf8() | ||
.expect("Failed to read index.js"); | ||
|
||
let f = index.replace("\"/studio", &format!("\"{}/studio", config.basepath)); | ||
|
||
if !config.basepath.is_empty() { | ||
f.replace( | ||
"http://localhost:3000", | ||
&format!( | ||
"{}{}", | ||
config.address.trim_end_matches("/"), | ||
&config.basepath | ||
), | ||
) | ||
} else { | ||
f | ||
} | ||
}); | ||
|
||
AGDB_STUDIO_INDEX_HTML.get_or_init(|| { | ||
AGDB_STUDIO | ||
.get_file("index.html") | ||
.expect("index.html not found") | ||
.contents_utf8() | ||
.expect("Failed to read index.html") | ||
.replace("\"/studio/", &format!("\"{}/studio/", config.basepath)) | ||
}); | ||
} | ||
|
||
async fn studio_index() -> ServerResult<( | ||
reqwest::StatusCode, | ||
[(&'static str, &'static str); 1], | ||
&'static [u8], | ||
)> { | ||
let content = AGDB_STUDIO_INDEX_HTML.get().ok_or(ServerError::new( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
"index.html not found", | ||
))?; | ||
|
||
Ok(( | ||
StatusCode::OK, | ||
[("Content-Type", "text/html")], | ||
content.as_str().as_bytes(), | ||
)) | ||
} | ||
|
||
pub(crate) async fn studio_root() -> ServerResult<impl IntoResponse> { | ||
studio(Path("index.html".to_string())).await | ||
studio_index().await | ||
} | ||
|
||
pub(crate) async fn studio(Path(file): Path<String>) -> ServerResult<impl IntoResponse> { | ||
if file.ends_with("index.html") { | ||
return studio_index().await; | ||
} | ||
|
||
if file.ends_with(".js") && file.contains("index") { | ||
let content = AGDB_STUDIO_INDEX_JS.get().ok_or(ServerError::new( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
"index.js not found", | ||
))?; | ||
|
||
return Ok(( | ||
StatusCode::OK, | ||
[("Content-Type", "application/javascript")], | ||
content.as_str().as_bytes(), | ||
)); | ||
} | ||
|
||
let f = if let Some(f) = AGDB_STUDIO.get_file(&file) { | ||
f | ||
} else { | ||
AGDB_STUDIO.get_file("index.html").ok_or(ServerError::new( | ||
StatusCode::NOT_FOUND, | ||
"index.html not found", | ||
))? | ||
return studio_index().await; | ||
}; | ||
|
||
let content_type = if file.ends_with(".js") { | ||
"application/javascript".to_string() | ||
"application/javascript" | ||
} else if file.ends_with(".css") { | ||
"text/css".to_string() | ||
"text/css" | ||
} else if file.ends_with(".svg") { | ||
"image/svg+xml".to_string() | ||
"image/svg+xml" | ||
} else if file.ends_with(".ico") { | ||
"image/x-icon" | ||
} else { | ||
ContentType::html().to_string() | ||
"text/html" | ||
}; | ||
|
||
Ok(( | ||
StatusCode::CREATED, | ||
StatusCode::OK, | ||
[("Content-Type", content_type)], | ||
f.contents_utf8().ok_or(ServerError::new( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
&format!("Failed to read content of '{file}'"), | ||
))?, | ||
f.contents(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters