Compare commits

..

No commits in common. "2e3d6999fff5cefc6fa2f51d544e5639da234754" and "96cd918dfe3faa2b9dbc1f44efe8ef3bd0aa271a" have entirely different histories.

7 changed files with 70 additions and 118 deletions

View File

@ -21,7 +21,7 @@ use sqlx::types::time::OffsetDateTime;
fn main() {
// note: add error checking yourself.
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.args(&["rev-parse", "HEAD"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();

View File

@ -8,13 +8,4 @@ edition = "2021"
[dependencies]
serde = { version = "1", features=["derive"]}
serde_json = { version ="1", features = ["raw_value"]}
async-trait = { version = "0.1.57", optional = true}
[features]
default = [
"min",
"full",
]
full = ["async-trait", "min"]
min = []
async-trait = "0.1.57"

View File

@ -1,74 +0,0 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::event_types::EventType;
use async_trait::async_trait;
#[async_trait]
pub trait Conductor: std::marker::Send + std::marker::Sync + CloneConductor {
async fn process(&self, event: EventType);
async fn health(&self) -> bool;
fn name(&self) -> &'static str;
}
/// Trait to clone Conductor
pub trait CloneConductor {
/// clone DB
fn clone_conductor(&self) -> Box<dyn Conductor>;
}
impl<T> CloneConductor for T
where
T: Conductor + Clone + 'static,
{
fn clone_conductor(&self) -> Box<dyn Conductor> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn Conductor> {
fn clone(&self) -> Self {
(**self).clone_conductor()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone)]
struct TestConductor;
const TEST_CONDUCTOR_NAME: &str = "TEST_CONDUCTOR";
#[async_trait]
impl Conductor for TestConductor {
async fn process(&self, _event: EventType) {}
fn name(&self) -> &'static str {
TEST_CONDUCTOR_NAME
}
async fn health(&self) -> bool {
true
}
}
#[test]
fn all_good() {
let c = TestConductor {};
assert_eq!(c.name(), TEST_CONDUCTOR_NAME);
assert!(c.health().await);
}
}

View File

@ -1,23 +0,0 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
#[serde(untagged)]
pub enum EventType {
NewHostname(String),
}

View File

@ -14,13 +14,67 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
#[cfg(feature = "min")]
pub mod event_types;
#[cfg(feature = "min")]
pub use event_types::*;
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
#[serde(untagged)]
pub enum EventType {
NewHostname(String),
}
#[cfg(feature = "full")]
pub mod conductor;
#[cfg(feature = "full")]
pub use conductor::*;
#[async_trait]
pub trait Conductor: std::marker::Send + std::marker::Sync + CloneConductor {
async fn process(&self, event: EventType);
async fn health(&self) -> bool;
fn name(&self) -> &'static str;
}
/// Trait to clone Conductor
pub trait CloneConductor {
/// clone DB
fn clone_conductor(&self) -> Box<dyn Conductor>;
}
impl<T> CloneConductor for T
where
T: Conductor + Clone + 'static,
{
fn clone_conductor(&self) -> Box<dyn Conductor> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn Conductor> {
fn clone(&self) -> Self {
(**self).clone_conductor()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone)]
struct TestConductor;
const TEST_CONDUCTOR_NAME: &str = "TEST_CONDUCTOR";
#[async_trait]
impl Conductor for TestConductor {
async fn process(&self, _event: EventType) {}
fn name(&self) -> &'static str {
TEST_CONDUCTOR_NAME
}
async fn health(&self) -> bool {
true
}
}
#[test]
fn all_good() {
let c = TestConductor {};
assert_eq!(c.name(), TEST_CONDUCTOR_NAME);
assert!(c.health().await);
}
}

View File

@ -18,6 +18,8 @@ use actix_web::{web, HttpResponse, Responder};
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use libconductor::Conductor;
use crate::AppCtx;
use crate::{GIT_COMMIT_HASH, VERSION};

View File

@ -14,10 +14,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use actix_web::{web, HttpResponse, Responder};
use std::collections::HashMap;
use actix_web::{web, HttpRequest, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use libconductor::EventType;
use libconductor::{Conductor, EventType};
use crate::errors::*;
use crate::AppCtx;