Implemented request to update the records.

This commit is contained in:
shad0wflame
2021-12-22 16:19:59 +01:00
parent 218dbd3770
commit e20d7b0d86
2 changed files with 127 additions and 24 deletions
+13 -16
View File
@@ -1,9 +1,9 @@
use serde::Deserialize;
use std::fs::File;
use std::io::{Read, Write};
use std::fs::{read_to_string, File};
use std::io::Write;
use std::path::Path;
const FILE_NAME: &'static str = "ddns_ip";
const IP_FILE_NAME: &'static str = "ddns_ip";
const WEBSITE_URL: &'static str = "https://httpbin.org/ip";
#[derive(Deserialize)]
@@ -13,16 +13,14 @@ struct IP {
/// Returns an Option holding the current IP address if the value has changed, otherwise returns None.
pub async fn get_ip_to_publish() -> Option<String> {
let current_ip = check_current_ip()
.await
.expect("Error getting the current IP.");
let previous_ip = match check_previous_ip() {
Some(x) => x,
None => String::new(),
};
println!("Current IP: {}, Previous IP: {}", current_ip, previous_ip);
let current_ip = check_current_ip()
.await
.expect("Error getting the current IP.");
if current_ip.eq(&previous_ip) {
return None;
@@ -47,22 +45,21 @@ async fn check_current_ip() -> Result<String, Box<dyn std::error::Error>> {
/// # Arguments
///
/// * `current_ip` - A &str holding the current IP value.
fn record_current_ip(current_ip: &str) {
let mut file = File::create(FILE_NAME).expect("Error creating file.");
fn record_current_ip(current_ip: &str) -> () {
let mut file = File::create(IP_FILE_NAME).expect("Error creating file.");
file.write_all(current_ip.as_ref())
.expect("Error writing file.");
}
/// Reads the current IP value from the FILE_NAME and returns it.
fn check_previous_ip() -> Option<String> {
if !Path::new(FILE_NAME).exists() {
let path = Path::new(IP_FILE_NAME);
if !path.exists() {
return None;
}
let mut file = File::open(FILE_NAME).ok()?;
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Error reading file.");
let contents = read_to_string(path).expect("Error reading file.");
return Some(contents);
Some(contents)
}