diff options
author | Galen Guyer <galen@galenguyer.com> | 2022-06-24 16:07:14 -0400 |
---|---|---|
committer | Galen Guyer <galen@galenguyer.com> | 2022-06-24 16:07:14 -0400 |
commit | 896b340c5b987a95e73bde6d811dcc0c9de251ea (patch) | |
tree | 98e0fa14318abdd7f454bc1d2506453c57d883f6 | |
parent | 375a93d48e7fe490a62c958dfde7a48939db5cff (diff) |
Add route to get root zone for acme.sh
-rw-r--r-- | src/extractors.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/routes/v1/zones.rs | 29 |
3 files changed, 31 insertions, 1 deletions
diff --git a/src/extractors.rs b/src/extractors.rs index 9e70266..805b5c5 100644 --- a/src/extractors.rs +++ b/src/extractors.rs @@ -89,7 +89,7 @@ where email: user.email.to_owned(), admin: user.admin, }; - return Ok(Self(token)) + return Ok(Self(token)); } let claims: BTreeMap<String, String> = match token.verify_with_key(&key) { Ok(claims) => claims, diff --git a/src/main.rs b/src/main.rs index bc144f2..c2f8b62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,6 +71,7 @@ async fn main() { "/zones", Router::new() .route("/", get(routes::v1::zones::list_zones)) + .route("/root", get(routes::v1::zones::get_root_domain)) .route( "/:zone_id", get(routes::v1::records::get_records) diff --git a/src/routes/v1/zones.rs b/src/routes/v1/zones.rs index 2bff4d8..d863948 100644 --- a/src/routes/v1/zones.rs +++ b/src/routes/v1/zones.rs @@ -2,10 +2,12 @@ use crate::db; use crate::extractors::Json; use crate::extractors::Jwt; use axum::extract::Path; +use axum::extract::Query; use axum::http::StatusCode; use axum::response::IntoResponse; use axum::Extension; use lazy_static::lazy_static; +use serde::Deserialize; use serde_json::json; use sqlx::{Error, Pool, Postgres}; use std::sync::Arc; @@ -33,6 +35,33 @@ pub async fn list_zones( } } +#[derive(Deserialize)] +pub struct RootDomainQuery { + domain: String, +} +pub async fn get_root_domain( + Query(query): Query<RootDomainQuery>, + Jwt(user): Jwt, + Extension(pool): Extension<Arc<Pool<Postgres>>>, +) -> impl IntoResponse { + let zones = db::zones::get_zones(&pool, user.sub).await; + let domain = match query.domain.ends_with(".") { + true => query.domain, + false => format!("{}.", query.domain), + }; + + match zones { + Ok(zones) => { + let longest_zone = zones + .iter() + .filter(|zone| domain.ends_with(&format!(".{}", zone.id))) + .max_by(|x, y| x.id.len().cmp(&y.id.len())); + (StatusCode::OK, longest_zone.unwrap().id.clone()) + } + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()), + } +} + pub async fn create_zone( Path(zone_id): Path<String>, Jwt(user): Jwt, |