feat(web): support context path deployments
This commit is contained in:
1 parent
45a60819ca
commit
d01ffb0d55
18 files changed
+248
-43
No files matched your search
@@ -32,6 +32,50 @@ fn web_body_limit_bytes() -> usize {
|
||||
mb.saturating_mul(1024 * 1024)
|
||||
}
|
||||
|
||||
fn normalize_public_base_path(value: Option<String>) -> String {
|
||||
let trimmed = value
|
||||
.unwrap_or_else(|| "/".to_string())
|
||||
.split(['?', '#'])
|
||||
.next()
|
||||
.unwrap_or("/")
|
||||
.trim()
|
||||
.trim_matches('/')
|
||||
.to_string();
|
||||
if trimmed.chars().any(|ch| ch.is_ascii_control() || ch.is_ascii_whitespace() || matches!(ch, ';' | ',')) {
|
||||
panic!("DBX_PUBLIC_BASE_PATH contains invalid characters");
|
||||
}
|
||||
if trimmed.is_empty() {
|
||||
"/".to_string()
|
||||
} else {
|
||||
format!("/{trimmed}")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::normalize_public_base_path;
|
||||
|
||||
#[test]
|
||||
fn normalize_public_base_path_defaults_to_root() {
|
||||
assert_eq!(normalize_public_base_path(None), "/");
|
||||
assert_eq!(normalize_public_base_path(Some("".to_string())), "/");
|
||||
assert_eq!(normalize_public_base_path(Some("/".to_string())), "/");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_public_base_path_trims_and_preserves_segments() {
|
||||
assert_eq!(normalize_public_base_path(Some("dbx".to_string())), "/dbx");
|
||||
assert_eq!(normalize_public_base_path(Some("/dbx/".to_string())), "/dbx");
|
||||
assert_eq!(normalize_public_base_path(Some("/tools/dbx/?v=1".to_string())), "/tools/dbx");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "DBX_PUBLIC_BASE_PATH contains invalid characters")]
|
||||
fn normalize_public_base_path_rejects_invalid_characters() {
|
||||
normalize_public_base_path(Some("/dbx admin".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mq-admin")]
|
||||
fn add_mq_routes(router: Router<Arc<WebState>>) -> Router<Arc<WebState>> {
|
||||
router
|
||||
@@ -125,9 +169,12 @@ async fn main() {
|
||||
app_state.storage.load_password_hash().await.unwrap_or(None)
|
||||
};
|
||||
|
||||
let public_base_path = normalize_public_base_path(std::env::var("DBX_PUBLIC_BASE_PATH").ok());
|
||||
|
||||
let web_state = Arc::new(WebState {
|
||||
app: app_state,
|
||||
data_dir,
|
||||
public_base_path: public_base_path.clone(),
|
||||
password_disabled,
|
||||
password_hash: RwLock::new(password_hash),
|
||||
sessions: RwLock::new(HashSet::new()),
|
||||
@@ -481,11 +528,18 @@ async fn main() {
|
||||
app = app.fallback_service(serve_dir);
|
||||
}
|
||||
|
||||
if public_base_path != "/" {
|
||||
app = Router::new().nest(&public_base_path, app);
|
||||
}
|
||||
|
||||
// Bind address
|
||||
let port: u16 = std::env::var("DBX_PORT").ok().and_then(|p| p.parse().ok()).unwrap_or(4224);
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], port));
|
||||
|
||||
tracing::info!("DBX Web server starting on http://{}", addr);
|
||||
if public_base_path != "/" {
|
||||
tracing::info!("Serving DBX Web under context path {}", public_base_path);
|
||||
}
|
||||
if password_disabled {
|
||||
tracing::info!("Password protection is disabled");
|
||||
} else if std::env::var("DBX_PASSWORD").is_ok() {
|
||||
|
||||
Reference in new issue
Block a user