Browse Source

Stabalizing api code

* Added logging
* Moved api routing into the api module
* Added method stubs for getting address, updating address, and updating address manually.
master
Drew Short 6 years ago
parent
commit
a7f3901656
  1. 54
      Cargo.lock
  2. 4
      Cargo.toml
  3. 25
      src/main.rs
  4. 5
      src/router.rs
  5. 56
      src/server/api.rs
  6. 2
      src/server/mod.rs

54
Cargo.lock

@ -430,6 +430,18 @@ dependencies = [
"cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "env_logger"
version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"termcolor 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "error-chain"
version = "0.1.12"
@ -567,6 +579,14 @@ name = "httparse"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "humantime"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hyper"
version = "0.11.27"
@ -1108,8 +1128,12 @@ name = "rsddns"
version = "0.1.0"
dependencies = [
"actix-web 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
"bytes 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cloudflare 0.1.0 (git+https://github.com/nocduro/cloudflare-rs)",
"env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1330,6 +1354,14 @@ dependencies = [
"remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termcolor"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.1"
@ -1774,11 +1806,28 @@ name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-util"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wincolor"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winreg"
version = "0.5.1"
@ -1856,6 +1905,7 @@ dependencies = [
"checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
"checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
"checksum encoding_rs 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2a91912d6f37c6a8fef8a2316a862542d036f13c923ad518b5aca7bcaac7544c"
"checksum env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)" = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38"
"checksum error-chain 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faa976b4fd2e4c2b2f3f486874b19e61944d3de3de8b61c9fcf835d583871bcc"
"checksum error-chain 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6930e04918388a9a2e41d518c25cf679ccafe26733fb4127dbf21993f2575d46"
"checksum failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7efb22686e4a466b1ec1a15c2898f91fa9cb340452496dca654032de20ff95b9"
@ -1873,6 +1923,7 @@ dependencies = [
"checksum htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163"
"checksum http 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "3e5765436510a2021e6917dfb87d1afa4b32d17c71353c013897fa5da29527a1"
"checksum httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b6288d7db100340ca12873fd4d08ad1b8f206a9457798dfb17c018a33fee540"
"checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e"
"checksum hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)" = "34a590ca09d341e94cddf8e5af0bbccde205d5fbc2fa3c09dd67c7f85cea59d7"
"checksum hyper-tls 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ffb1bd5e518d3065840ab315dbbf44e4420e5f7d80e2cb93fa6ffffc50522378"
"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e"
@ -1962,6 +2013,7 @@ dependencies = [
"checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7"
"checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5"
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
"checksum termcolor 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ff3bac0e465b59f194e7037ed404b0326e56ff234d767edc4c5cc9cd49e7a2c7"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
@ -2008,7 +2060,9 @@ dependencies = [
"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "afc5508759c5bf4285e61feb862b6083c8480aec864fa17a81fdec6f69b461ab"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "561ed901ae465d6185fa7864d63fbd5720d0ef718366c9a4dc83cf6170d7e9ba"
"checksum winreg 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a27a759395c1195c4cc5cda607ef6f8f6498f64e78f7900f5de0a127a424704a"
"checksum winutil 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7daf138b6b14196e3830a588acf1e86966c694d3e8fb026fb105b8b5dca07e6e"
"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"

4
Cargo.toml

@ -10,9 +10,13 @@ codegen-units = 1
incremental = false
[dependencies]
bytes = "0.4.10"
clap = "2.32.0"
log = "0.4.0"
env_logger = "0.5.13"
yaml-rust = "0.4.0"
serde = "1.0.78"
serde_derive = "1.0.78"
futures = "0.1.24"
actix-web = "0.7.7"
cloudflare = { git = "https://github.com/nocduro/cloudflare-rs", branch = "master" }

25
src/main.rs

@ -1,8 +1,13 @@
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;
extern crate actix_web;
extern crate bytes;
extern crate clap;
extern crate env_logger;
extern crate futures;
extern crate serde;
use clap::{App, Arg};
@ -13,36 +18,42 @@ mod server;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
fn main() {
match std::env::var("RUST_LOG") {
Ok(_) => (),
Err(_) => std::env::set_var("RUST_LOG", "actix_web=info"),
}
env_logger::init();
let args = App::new("Dynamic DNS Server")
.version(VERSION)
.author("Drew Short <warrick@sothr.com>")
.about("Recieve DDNS requests and update cloudflare subdomains")
.arg(
.args(&[
Arg::with_name("config")
.short("c")
.long("config")
.value_name("PATH")
.help("Set a custom configuration file path. DEFAULT=/etc/rsddns/rsddns.yml.")
.default_value("/etc/rsddns/rsddns.yml")
.help("Set a custom configuration file path.")
.takes_value(true),
)
.arg(
Arg::with_name("port")
.short("p")
.long("port")
.value_name("PORT")
.default_value("8080")
.help("The port to run the server on.")
.takes_value(true),
)
])
.get_matches();
let config: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml");
let port: i32 = args
.value_of("port")
.unwrap_or("8080")
.unwrap()
.parse::<i32>()
.unwrap_or(8080);
println!("Starting server on {} with config {}", port, config);
info!("Starting server on {} with config {}", port, config);
actix_web::server::new(|| router::create())
.bind(format!("localhost:{}", port))

5
src/router.rs

@ -1,13 +1,16 @@
extern crate actix_web;
use actix_web::middleware::Logger;
use actix_web::{http, App};
use server;
pub fn create() -> App {
actix_web::App::new()
.resource("/", |r| r.f(server::index))
.middleware(Logger::default())
.scope("api/", |api_scope| server::api::route(api_scope))
.resource("/health", |r| {
r.method(http::Method::GET).with(server::healthcheck)
})
.resource("/", |r| r.f(server::index))
}

56
src/server/api.rs

@ -0,0 +1,56 @@
use actix_web::http::Method;
use actix_web::http::StatusCode;
use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Scope};
use bytes::{Buf, Bytes, IntoBuf};
use futures::future::Future;
use std::io::Read;
pub fn route(scope: Scope<()>) -> Scope<()> {
scope.nested("{root}/{zone}", |zone_scope| {
zone_scope
.resource("", |r| r.method(Method::GET).f(get_address))
.resource("update", |r| {
r.method(Method::GET).f(update_address_automatically);
r.method(Method::POST).f(update_address_manually)
})
})
}
fn update_address(address: String) -> String {
info!("Updating Address {}", address);
address
}
fn get_address(req: &HttpRequest) -> HttpResponse {
match req.connection_info().remote() {
Some(addr) => HttpResponse::build(StatusCode::OK)
.content_type("text/plain")
.body(format!("{}", addr)),
None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(),
}
}
fn update_address_automatically(req: &HttpRequest) -> HttpResponse {
match req.connection_info().remote() {
Some(addr) => HttpResponse::build(StatusCode::OK)
.content_type("text/plain")
.body(format!("{}", addr)),
None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(),
}
}
fn update_address_manually(req: &HttpRequest) -> FutureResponse<HttpResponse> {
req.body()
.limit(48)
.from_err()
.and_then(|bytes: Bytes| {
let mut buffer = String::new();
match bytes.into_buf().reader().read_to_string(&mut buffer) {
Ok(_) => Ok(HttpResponse::Ok()
.content_type("text/plain")
.body(update_address(buffer))),
Err(_) => Ok(HttpResponse::build(StatusCode::BAD_REQUEST).finish()),
}
})
.responder()
}

2
src/server/mod.rs

@ -2,6 +2,8 @@ use actix_web::{HttpRequest, Json, Result};
use VERSION;
pub mod api;
#[derive(Serialize)]
pub struct Health {
version: &'static str,

Loading…
Cancel
Save