From 21c7b74ce280ba32e34c31bf9eea8276dc42197c Mon Sep 17 00:00:00 2001 From: Drew Short Date: Wed, 12 Sep 2018 10:22:04 -0500 Subject: [PATCH] Adding health encdpoint * Reorgnaized to use router for the actix_web::App factory *Inlined the version from the cargo metadata * Changed the project name to rsddns * Added a healthcheck function * Added dependencies serde and serde_derive * Added build configuration to the release profile to shrink binary --- .gitlab-ci.yml | 6 +++--- Cargo.lock | 22 ++++++++++++---------- Cargo.toml | 10 +++++++++- src/main.rs | 11 +++++++++-- src/router.rs | 13 +++++++++++++ src/server.rs | 7 ------- src/server/mod.rs | 16 ++++++++++++++++ 7 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 src/router.rs delete mode 100644 src/server.rs create mode 100644 src/server/mod.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 873dca2..29f6023 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -17,7 +17,7 @@ debug: - cargo build artifacts: paths: - - target/debug/ddns + - target/debug/rsddns tags: - docker @@ -26,9 +26,9 @@ release: stage: build script: - cargo build --release - - strip target/release/ddns + - strip target/release/rsddns artifacts: paths: - - target/release/ddns + - target/release/rsddns tags: - docker \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index b8fe53c..9d1c87f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -360,16 +360,6 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "ddns" -version = "0.1.0" -dependencies = [ - "actix-web 0.7.7 (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)", - "yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "dtoa" version = "0.4.3" @@ -1113,6 +1103,18 @@ dependencies = [ "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rsddns" +version = "0.1.0" +dependencies = [ + "actix-web 0.7.7 (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)", + "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)", +] + [[package]] name = "rustc-demangle" version = "0.1.9" diff --git a/Cargo.toml b/Cargo.toml index 574a9d4..b87388d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,18 @@ [package] -name = "ddns" +name = "rsddns" version = "0.1.0" authors = ["Drew Short "] +[profile.release] +panic = "abort" +lto = true +codegen-units = 1 +incremental = false + [dependencies] clap = "2.32.0" yaml-rust = "0.4.0" +serde = "1.0.78" +serde_derive = "1.0.78" actix-web = "0.7.7" cloudflare = { git = "https://github.com/nocduro/cloudflare-rs", branch = "master" } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3e747a9..f24cb7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,20 @@ +#[macro_use] +extern crate serde_derive; + extern crate actix_web; extern crate clap; +extern crate serde; use clap::{App, Arg}; +mod router; mod server; +const VERSION: &'static str = env!("CARGO_PKG_VERSION"); + fn main() { let args = App::new("Dynamic DNS Server") - .version("0.1") + .version(VERSION) .author("Drew Short ") .about("Recieve DDNS requests and update cloudflare subdomains") .arg( @@ -37,7 +44,7 @@ fn main() { println!("Starting server on {} with config {}", port, config); - actix_web::server::new(|| actix_web::App::new().resource("/", |r| r.f(server::index))) + actix_web::server::new(|| router::create()) .bind(format!("localhost:{}", port)) .unwrap() .run(); diff --git a/src/router.rs b/src/router.rs new file mode 100644 index 0000000..45e06f2 --- /dev/null +++ b/src/router.rs @@ -0,0 +1,13 @@ +extern crate actix_web; + +use actix_web::{http, App}; + +use server; + +pub fn create() -> App { + actix_web::App::new() + .resource("/", |r| r.f(server::index)) + .resource("/health", |r| { + r.method(http::Method::GET).with(server::healthcheck) + }) +} diff --git a/src/server.rs b/src/server.rs deleted file mode 100644 index b75778e..0000000 --- a/src/server.rs +++ /dev/null @@ -1,7 +0,0 @@ -extern crate actix_web; - -use actix_web::HttpRequest; - -pub fn index(_req: &HttpRequest) -> &'static str { - "Hello, World!" -} diff --git a/src/server/mod.rs b/src/server/mod.rs new file mode 100644 index 0000000..01c8ebf --- /dev/null +++ b/src/server/mod.rs @@ -0,0 +1,16 @@ +use actix_web::{HttpRequest, Json, Result}; + +use VERSION; + +#[derive(Serialize)] +pub struct Health { + version: &'static str, +} + +pub fn index(_req: &HttpRequest) -> &'static str { + "Hello, World!" +} + +pub fn healthcheck(_req: HttpRequest) -> Result> { + Ok(Json(Health { version: VERSION })) +}