diff --git a/deploy_prod.sh b/deploy_prod.sh old mode 100644 new mode 100755 diff --git a/src/cli.rs b/src/cli.rs deleted file mode 100644 index d6d8b4d..0000000 --- a/src/cli.rs +++ /dev/null @@ -1,23 +0,0 @@ -use structopt::StructOpt; - -#[derive(Debug, StructOpt)] -#[structopt( - name = "peach-dyndns-host", - rename_all = "kebab-case", - long_about = "\nTODO", - raw(setting = "structopt::clap::AppSettings::ColoredHelp") -)] -pub struct CliArgs { - #[structopt(flatten)] - log: clap_log_flag::Log, - #[structopt(flatten)] - verbose: clap_verbosity_flag::Verbosity, -} - -pub fn args() -> Result> { - let args = CliArgs::from_args(); - - args.log.log_all(Some(args.verbose.log_level()))?; - - Ok(args) -} diff --git a/src/lib.rs b/src/lib.rs index 2ab3a1d..67bd1aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ /* -* 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 +* LIST OF METHODS +* register_domain (add a new domain and get back the TSIG key for subsequent updating with nsupdate) +* is_domain_available (check if given domain is available and return boolean result) +* register_user (sends an email verification to create a new account) NOT IMPLEMENTED +* verify_user (for clicking the link in the email) NOT IMPLEMENTED */ mod errors; mod generate_zone; @@ -24,6 +24,11 @@ pub struct RegisterDomainPost { domain: String, } +#[derive(Deserialize, Debug)] +pub struct IsDomainAvailablePost { + domain: String, +} + /// Create JSON-RPC I/O handler, add RPC methods and launch HTTP server. pub fn run() -> Result<(), BoxError> { info!("Starting up."); @@ -45,6 +50,24 @@ pub fn run() -> Result<(), BoxError> { } }); + io.add_method("is_domain_available", move |params: Params| { + let d: Result = params.parse(); + match d { + Ok(d) => { + // if the domain has an invalid format return an erro + if !validate_domain(&d.domain) { + Err(Error::from(PeachDynDnsError::InvalidDomain{ domain: d.domain.to_string() })) + } + // if it has a valid format, check if its available + else { + let result = check_domain_available(&d.domain); + Ok(Value::Bool(result)) + } + }, + Err(e) => Err(Error::from(PeachDynDnsError::MissingParams { e })), + } + }); + let http_server = env::var("PEACH_DYNDNS_SERVER").unwrap_or_else(|_| "127.0.0.1:3001".to_string()); @@ -64,90 +87,3 @@ pub fn run() -> Result<(), BoxError> { Ok(()) } - - - -// -//#[get("/")] -//pub fn index() -> &'static str { -// "This is the peach-dyndns server." -//} -// -//#[derive(Serialize)] -//pub struct JsonResponse { -// pub status: String, -// #[serde(skip_serializing_if = "Option::is_none")] -// pub data: Option, -// #[serde(skip_serializing_if = "Option::is_none")] -// pub msg: Option, -//} -// -//// helper function to build a JsonResponse object -//pub fn build_json_response( -// status: String, -// data: Option, -// msg: Option, -//) -> JsonResponse { -// JsonResponse { status, data, msg } -//} -// -//#[derive(Deserialize, Debug)] -//pub struct RegisterDomainPost { -// domain: String, -//} -// -//#[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 -// // check if its a valid domain -// if !validate_domain(&data.domain) { -// let status = "error".to_string(); -// let msg = "domain is not in a valid format".to_string(); -// Json(build_json_response(status, None, Some(msg))) -// } else { -// // check if the domain is available -// let is_domain_available = check_domain_available(&data.domain); -// if !is_domain_available { -// let status = "error".to_string(); -// let msg = "can't register a domain that is already registered".to_string(); -// Json(build_json_response(status, None, Some(msg))) -// } else { -// // generate configs for the zone -// let result = generate_zone(&data.domain); -// match result { -// Ok(key_file_text) => { -// let status = "success".to_string(); -// let msg = key_file_text.to_string(); -// Json(build_json_response(status, None, Some(msg))) -// } -// Err(_err) => { -// let status = "error".to_string(); -// let msg = "there was an error creating the zone file".to_string(); -// Json(build_json_response(status, None, Some(msg))) -// } -// } -// } -// } -//} -// -//#[derive(Deserialize, Debug)] -//pub struct CheckAvailableDomainPost { -// domain: String, -//} -// -//// route which returns a msg of "true" if the domain is available and "false" if it is already taken -//#[post("/domain/check-available", data = "")] -//pub async fn check_available(data: Json) -> Json { -// info!("post request to check if domain is available {:?}", data); -// if !validate_domain(&data.domain) { -// let status = "error".to_string(); -// let msg = "domain is not in a valid format".to_string(); -// Json(build_json_response(status, None, Some(msg))) -// } else { -// 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))) -// } -//}