-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9d04a3
commit 55b563d
Showing
10 changed files
with
181 additions
and
625 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod sender; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use reqwest::Method; | ||
|
||
use crate::config::OutputMethods; | ||
|
||
pub struct Sender { | ||
client: reqwest::Client, | ||
method: Method, | ||
endpoint: String, | ||
} | ||
|
||
impl Sender { | ||
pub fn new( | ||
method: &OutputMethods, | ||
endpoint: String, | ||
) -> Result<Self, Box<dyn std::error::Error>> { | ||
let client = reqwest::Client::new(); | ||
let method = Method::from_bytes(method.to_string().as_bytes())?; | ||
Ok(Self { | ||
client, | ||
method, | ||
endpoint, | ||
}) | ||
} | ||
|
||
pub async fn send(&self, payload: &str) -> Result<(), reqwest::Error> { | ||
let req = self | ||
.client | ||
.request(self.method.clone(), &self.endpoint) | ||
.body(payload.to_owned()) | ||
.build()?; | ||
self.client.execute(req).await?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,56 @@ | ||
use std::sync::Arc; | ||
|
||
use config::Config; | ||
use http::sender::Sender; | ||
use output::Output; | ||
use rmq::producer::Producer; | ||
use tracing::error; | ||
|
||
mod config; | ||
mod http; | ||
mod models; | ||
mod output; | ||
mod rmq; | ||
|
||
pub enum WorkerError { | ||
ConfigError(String), | ||
} | ||
|
||
pub enum Output { | ||
RMQ(rmq::producer::Producer), | ||
} | ||
|
||
impl Output { | ||
pub async fn send(&self, payload: String) -> Result<(), Box<dyn std::error::Error>> { | ||
match self { | ||
Output::RMQ(p) => { | ||
p.send(payload).await?; | ||
Ok(()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
async fn start(config: Config) -> Result<(), Box<dyn std::error::Error>> { | ||
let mut outputs = Vec::new(); | ||
if let Some(outputs_config) = config.outputs { | ||
if let Some(rmq) = outputs_config.rmq { | ||
let rmq_out = Producer::new(&rmq).await?; | ||
outputs.push(Output::RMQ(rmq_out)); | ||
} | ||
|
||
if let Some(http) = outputs_config.http { | ||
let http_out = Sender::new(&http.method, http.endpoint)?; | ||
outputs.push(Output::HTTP(http_out)); | ||
} | ||
} | ||
|
||
let aoutputs = Arc::new(outputs); | ||
if let Some(http_config) = config.inputs.http { | ||
http::start_http_consumer(&http_config).await?; | ||
http::server::start_http_consumer(&http_config, aoutputs.clone()).await?; | ||
} else if let Some(rmq_config) = config.inputs.rmq { | ||
let mut consumer = rmq::consumer::Consumer::new(&rmq_config).await?; | ||
consumer.start().await.expect("error starting consumer"); | ||
rmq::consumer::Consumer::new(&rmq_config, aoutputs.clone()) | ||
.await? | ||
.start() | ||
.await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
if std::env::var("RUST_LOG").is_err() { | ||
std::env::set_var("RUST_LOG", "info"); | ||
} | ||
|
||
let config = Config::load_file(None).unwrap_or_default(); | ||
if let Err(e) = start(config).await { | ||
println!("error starting worker: {e}") | ||
error!("error starting worker: {e}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::{http, rmq}; | ||
|
||
pub enum Output { | ||
RMQ(rmq::producer::Producer), | ||
HTTP(http::sender::Sender), | ||
} | ||
|
||
impl Output { | ||
pub async fn send(&self, payload: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
match self { | ||
Output::RMQ(p) => { | ||
p.send(payload).await?; | ||
Ok(()) | ||
} | ||
Output::HTTP(sender) => { | ||
sender.send(payload).await?; | ||
Ok(()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters