From f27bd1a1928facb51cbbecdbc730b762b5711199 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 2 Nov 2020 15:54:23 -0500 Subject: [PATCH] add switchcounter client --- backend/.gitignore | 1 + backend/Cargo.toml | 3 +- backend/src/main.rs | 2 + backend/src/web/mod.rs | 1 + backend/src/web/switchcounter.rs | 72 ++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 backend/.gitignore create mode 100644 backend/src/web/mod.rs create mode 100644 backend/src/web/switchcounter.rs diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..add6cf0 --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1 @@ +Rocket.toml \ No newline at end of file diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 0730202..e05dad1 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -40,5 +40,4 @@ features = ["sqlite", "r2d2", "uuidv07", "chrono"] [dependencies.rocket_contrib] version = "0.4" default-features = false -features = ["json", "diesel_sqlite_pool", "uuid", "helmet"] - +features = ["json", "diesel_sqlite_pool", "uuid", "helmet"] \ No newline at end of file diff --git a/backend/src/main.rs b/backend/src/main.rs index 6d07c65..ec12939 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -19,6 +19,7 @@ const APPLICATION_NAME: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_ pub mod api; pub mod models; pub mod schema; +pub mod web; #[database("main_data")] pub struct MainDatabase(SqliteConnection); @@ -35,6 +36,7 @@ fn main() -> Result<()> { .attach(prometheus.clone()) .attach(MainDatabase::fairing()) .attach(SpaceHelmet::default()) + .attach(web::switchcounter::Client::fairing()) .mount("/metrics", prometheus) .mount( "/", diff --git a/backend/src/web/mod.rs b/backend/src/web/mod.rs new file mode 100644 index 0000000..0ec293d --- /dev/null +++ b/backend/src/web/mod.rs @@ -0,0 +1 @@ +pub mod switchcounter; diff --git a/backend/src/web/switchcounter.rs b/backend/src/web/switchcounter.rs new file mode 100644 index 0000000..51451f8 --- /dev/null +++ b/backend/src/web/switchcounter.rs @@ -0,0 +1,72 @@ +use color_eyre::eyre::{eyre, Result}; +use rocket::fairing::AdHoc; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Debug)] +pub struct FrontAsk { + pub command: String, +} + +impl Default for FrontAsk { + fn default() -> Self { + FrontAsk { + command: "switch".to_string(), + } + } +} + +#[derive(Serialize, Debug)] +pub struct SwitchCommand { + pub command: String, + pub member_name: String, +} + +#[derive(Deserialize, Debug)] +pub struct Status { + pub member_name: String, + pub started_at: chrono::NaiveDateTime, +} + +pub struct Client { + webhook_url: String, +} + +impl Client { + pub fn new(webhook_url: String) -> Self { + Client { + webhook_url: webhook_url, + } + } + + pub fn fairing() -> AdHoc { + AdHoc::on_attach("Switch Counter API", |rocket| { + let webhook_url = rocket.config().get_string("switchcounter_webhook").unwrap(); + Ok(rocket.manage(Client::new(webhook_url))) + }) + } + + #[instrument(err, skip(self))] + pub fn status(&self) -> Result { + let resp = + ureq::post(&self.webhook_url).send_json(serde_json::to_value(FrontAsk::default())?); + if resp.ok() { + Ok(resp.into_json_deserialize()?) + } else { + Err(eyre!("{}", resp.status_line())) + } + } + + #[instrument(err, skip(self))] + pub fn switch(&self, member_name: String) -> Result { + let resp = ureq::post(&self.webhook_url).send_json(serde_json::to_value(SwitchCommand { + command: "switch".to_string(), + member_name: member_name, + })?); + + if resp.ok() { + Ok(resp.into_json_deserialize()?) + } else { + Err(eyre!("{}", resp.status_line())) + } + } +}