From c2431b6225e5fcb4ece38117cb02f46f4ac6d107 Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 18 May 2021 14:20:17 +0200 Subject: [PATCH] Add /domain/check-available endpoint --- src/main.rs | 5 ++--- src/routes.rs | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 26fab5c..5ece8de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,7 @@ #[macro_use] extern crate rocket; -use crate::routes::{index, register_domain}; -use futures::try_join; +use crate::routes::{index, register_domain, check_available}; use std::io; use tokio::task; @@ -17,7 +16,7 @@ mod generate_zone; #[tokio::main] async fn main() { let rocket_result = rocket::build() - .mount("/", routes![index, register_domain]) + .mount("/", routes![index, register_domain, check_available]) .launch() .await; diff --git a/src/routes.rs b/src/routes.rs index 5dd5bcb..d94e2c4 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,8 +1,9 @@ /* -* -* /register-user (sends an email verification to create a new account) NOT IMPLEMENTED -* /verify (for clicking the link in the email) NOT IMPLEMENTED -* /register-domain (add a new domain and get back the TSIG key for subsequent updating with nsupdate) +* LIST OF ROUTES +* /domain/register (add a new domain and get back the TSIG key for subsequent updating with nsupdate) +* /domain/check-available (check if given domain is available) +* /user/register sends an email verification to create a new account) NOT IMPLEMENTED +* /user/verify (for clicking the link in the email) NOT IMPLEMENTED */ use crate::generate_zone::{check_domain_available, generate_zone}; use rocket_contrib::json::{Json, JsonValue}; @@ -36,9 +37,10 @@ pub struct RegisterDomainPost { domain: String, } -#[post("/register-domain", data = "")] +#[post("/domain/register", data = "")] pub async fn register_domain(data: Json) -> Json { info!("++ post request to register new domain: {:?}", data); + // TODO: grab/create a mutex, so that only one rocket thread is calling register_domain at a time // TODO: first confirm domain is in the right format ("*.dyn.peachcloud.org") let is_domain_available = check_domain_available(&data.domain); if !is_domain_available{ @@ -61,3 +63,19 @@ pub async fn register_domain(data: Json) -> Json) -> Json { + info!("post request to check if domain is available {:?}", data); + // TODO: validate that domain is in correct format + let status = "success".to_string(); + let is_available = check_domain_available(&data.domain); + let msg = is_available.to_string(); + Json(build_json_response(status, None, Some(msg))) +}