use actix_web::HttpRequest;
pub fn x_jdbc_url(req: &HttpRequest) -> Option<String> {
req.headers()
.get("X-JDBC-URL")
.or_else(|| req.headers().get("x-jdbc-url"))
.and_then(|h| h.to_str().ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
}
pub fn jdbc_to_postgres_url(jdbc_url: &str) -> Option<String> {
let trimmed = jdbc_url.trim();
if trimmed.starts_with("jdbc:postgresql://") {
Some(trimmed.replacen("jdbc:postgresql://", "postgres://", 1))
} else if trimmed.starts_with("postgres://") || trimmed.starts_with("postgresql://") {
Some(trimmed.to_string())
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_jdbc_to_postgres_url() {
assert_eq!(
jdbc_to_postgres_url("jdbc:postgresql://localhost:5432/mydb"),
Some("postgres://localhost:5432/mydb".to_string())
);
assert_eq!(
jdbc_to_postgres_url("jdbc:postgresql://user:pass@host/db"),
Some("postgres://user:pass@host/db".to_string())
);
assert_eq!(
jdbc_to_postgres_url("postgres://localhost/db"),
Some("postgres://localhost/db".to_string())
);
assert_eq!(jdbc_to_postgres_url("jdbc:mysql://localhost/db"), None);
}
}