From 9b4ce4a8ad645a012d5bd31d4d588cb7c3962b1a Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 28 Aug 2023 12:16:44 +0200 Subject: admin api: refactor caddy check api code --- src/api/admin/api_server.rs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src/api') diff --git a/src/api/admin/api_server.rs b/src/api/admin/api_server.rs index 8bf467dc..50c79120 100644 --- a/src/api/admin/api_server.rs +++ b/src/api/admin/api_server.rs @@ -100,6 +100,20 @@ impl AdminApiServer { .get("domain") .ok_or_internal_error("Could not parse domain query string")?; + if self.check_domain(domain).await? { + Ok(Response::builder() + .status(StatusCode::OK) + .body(Body::from(format!( + "Domain '{domain}' is managed by Garage" + )))?) + } else { + Err(Error::bad_request(format!( + "Domain '{domain}' is not managed by Garage" + ))) + } + } + + async fn check_domain(&self, domain: &str) -> Result { // Resolve bucket from domain name, inferring if the website must be activated for the // domain to be valid. let (bucket_name, must_check_website) = if let Some(bname) = self @@ -123,19 +137,18 @@ impl AdminApiServer { (domain.to_string(), true) }; - let bucket_id = self + let bucket_id = match self .garage .bucket_helper() .resolve_global_bucket_name(&bucket_name) .await? - .ok_or(HelperError::NoSuchBucket(bucket_name.to_string()))?; + { + Some(bucket_id) => bucket_id, + None => return Ok(false), + }; if !must_check_website { - return Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(format!( - "Domain '{domain}' is managed by Garage" - )))?); + return Ok(true); } let bucket = self @@ -148,16 +161,8 @@ impl AdminApiServer { let bucket_website_config = bucket_state.website_config.get(); match bucket_website_config { - Some(_v) => { - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(format!( - "Domain '{domain}' is managed by Garage" - )))?) - } - None => Err(Error::bad_request(format!( - "Domain '{domain}' is not managed by Garage" - ))), + Some(_v) => Ok(true), + None => Ok(false), } } -- cgit v1.2.3 From cece1be1bb3aa779b0e01af475572d734c5e51f7 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 28 Aug 2023 13:17:26 +0200 Subject: bump version to 0.8.3 --- src/api/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api') diff --git a/src/api/Cargo.toml b/src/api/Cargo.toml index c2155eb9..a9279c37 100644 --- a/src/api/Cargo.toml +++ b/src/api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "garage_api" -version = "0.8.2" +version = "0.8.3" authors = ["Alex Auvolat "] edition = "2018" license = "AGPL-3.0" -- cgit v1.2.3