diff --git a/.gitignore b/.gitignore index 3d6c2ab..d9b27fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ .gradle build/ !gradle/wrapper/gradle-wrapper.jar -!src/main/**/build/ -!src/test/**/build/ +!aquila/src/main/**/build/ +!aquila/src/test/**/build/ ### IntelliJ IDEA ### .idea/modules.xml @@ -13,8 +13,8 @@ build/ *.iml *.ipr out/ -!src/main/**/out/ -!src/test/**/out/ +!aquila/src/main/**/out/ +!aquila/src/test/**/out/ ### Eclipse ### .apt_generated @@ -25,8 +25,8 @@ out/ .springBeans .sts4-cache bin/ -!src/main/**/bin/ -!src/test/**/bin/ +!aquila/src/main/**/bin/ +!aquila/src/test/**/bin/ ### NetBeans ### /nbproject/private/ diff --git a/configuration/configuration.json b/aquila/configuration/configuration.json similarity index 100% rename from configuration/configuration.json rename to aquila/configuration/configuration.json diff --git a/src/client/mod.rs b/aquila/src/client/mod.rs similarity index 100% rename from src/client/mod.rs rename to aquila/src/client/mod.rs diff --git a/src/client/sagittarius/action_client.rs b/aquila/src/client/sagittarius/action_client.rs similarity index 100% rename from src/client/sagittarius/action_client.rs rename to aquila/src/client/sagittarius/action_client.rs diff --git a/src/client/sagittarius/flow_client.rs b/aquila/src/client/sagittarius/flow_client.rs similarity index 80% rename from src/client/sagittarius/flow_client.rs rename to aquila/src/client/sagittarius/flow_client.rs index 7b321a1..70f53f6 100644 --- a/src/client/sagittarius/flow_client.rs +++ b/aquila/src/client/sagittarius/flow_client.rs @@ -1,4 +1,4 @@ -use crate::service::flow_service::{FlowService, FlowServiceBase}; +use aquila_store::{FlowService, FlowServiceBase}; use async_trait::async_trait; use futures::StreamExt; use log::{error, info}; @@ -22,7 +22,10 @@ pub struct SagittariusFlowClientBase { /// Trait representing a service for receiving flows from `Sagittarius`. #[async_trait] pub trait SagittariusFlowClient { - async fn new(sagittarius_url: String, flow_service: Arc>) -> SagittariusFlowClientBase; + async fn new( + sagittarius_url: String, + flow_service: Arc>, + ) -> SagittariusFlowClientBase; async fn send_flow_update_request(&mut self); async fn send_start_request(&mut self); } @@ -35,7 +38,10 @@ impl SagittariusFlowClient for SagittariusFlowClientBase { /// /// Behavior: /// Will panic when a connection can`t be established - async fn new(sagittarius_url: String, flow_service: Arc>) -> SagittariusFlowClientBase { + async fn new( + sagittarius_url: String, + flow_service: Arc>, + ) -> SagittariusFlowClientBase { let client = match FlowServiceClient::connect(sagittarius_url).await { Ok(res) => res, Err(start_error) => { @@ -43,7 +49,10 @@ impl SagittariusFlowClient for SagittariusFlowClientBase { } }; - SagittariusFlowClientBase { flow_service, client } + SagittariusFlowClientBase { + flow_service, + client, + } } /// Will send a request `FlowGetRequest` to `Sagittarius` @@ -89,7 +98,10 @@ impl SagittariusFlowClient for SagittariusFlowClientBase { let mut stream = response.into_inner(); - async fn handle_response(response: FlowResponse, flow_service: Arc>) { + async fn handle_response( + response: FlowResponse, + flow_service: Arc>, + ) { match response.r#type { INSERT => { let flow = response.updated_flow; @@ -137,11 +149,14 @@ impl SagittariusFlowClient for SagittariusFlowClientBase { #[cfg(test)] mod tests { - use crate::client::sagittarius::action_client::{SagittariusActionClient, SagittariusActionClientBase}; - use crate::client::sagittarius::flow_client::{SagittariusFlowClient, SagittariusFlowClientBase}; - use crate::data::redis::setup_redis_test_container; - use crate::service::flow_service::FlowService; - use crate::service::flow_service::FlowServiceBase; + use crate::client::sagittarius::action_client::{ + SagittariusActionClient, SagittariusActionClientBase, + }; + use crate::client::sagittarius::flow_client::{ + SagittariusFlowClient, SagittariusFlowClientBase, + }; + use aquila_container::setup_redis_test_container; + use aquila_store::{FlowService, FlowServiceBase}; use async_trait::async_trait; use std::pin::Pin; use std::sync::Arc; @@ -152,8 +167,12 @@ mod tests { use tonic::codegen::tokio_stream::Stream; use tonic::transport::Server; use tonic::{Request, Response, Status}; - use tucana::sagittarius::flow_service_server::{FlowService as SagittariusFlowService, FlowServiceServer}; - use tucana::sagittarius::{Flow, FlowGetRequest, FlowGetResponse, FlowLogonRequest, FlowResponse}; + use tucana::sagittarius::flow_service_server::{ + FlowService as SagittariusFlowService, FlowServiceServer, + }; + use tucana::sagittarius::{ + Flow, FlowGetRequest, FlowGetResponse, FlowLogonRequest, FlowResponse, + }; struct MockFlowService { flow_get_result: FlowGetResponse, @@ -170,13 +189,19 @@ mod tests { #[async_trait] impl SagittariusFlowService for MockFlowService { - async fn get(&self, _request: Request) -> Result, Status> { + async fn get( + &self, + _request: Request, + ) -> Result, Status> { Ok(Response::new(self.flow_get_result.clone())) } - type UpdateStream = Pin> + Send>>; + type UpdateStream = Pin> + Send>>; - async fn update(&self, _request: Request) -> Result, Status> { + async fn update( + &self, + _request: Request, + ) -> Result, Status> { let flow = Flow { flow_id: 1, start_node: None, @@ -191,24 +216,34 @@ mod tests { }; }; - Ok(Response::new(Box::pin(response_stream) as Self::UpdateStream)) + Ok(Response::new( + Box::pin(response_stream) as Self::UpdateStream + )) } } #[async_trait] impl SagittariusFlowService for BrokenMockFlowService { - async fn get(&self, _request: Request) -> Result, Status> { + async fn get( + &self, + _request: Request, + ) -> Result, Status> { Err(Status::internal("An unhandled error occurred!")) } - type UpdateStream = Pin> + Send>>; + type UpdateStream = Pin> + Send>>; - async fn update(&self, _request: Request) -> Result, Status> { + async fn update( + &self, + _request: Request, + ) -> Result, Status> { Err(Status::internal("An unhandled error occurred!")) } } - async fn setup_sagittarius_mock(flow_get_response: FlowGetResponse) -> (JoinHandle<()>, oneshot::Sender<()>, String) { + async fn setup_sagittarius_mock( + flow_get_response: FlowGetResponse, + ) -> (JoinHandle<()>, oneshot::Sender<()>, String) { let (shutdown_tx, shutdown_rx) = oneshot::channel(); let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); @@ -300,9 +335,21 @@ mod tests { async fn test_get_flows_update_only() { let response = FlowGetResponse { updated_flows: vec![ - Flow { flow_id: 1, start_node: None, definition: None }, - Flow { flow_id: 2, start_node: None, definition: None }, - Flow { flow_id: 3, start_node: None, definition: None }, + Flow { + flow_id: 1, + start_node: None, + definition: None, + }, + Flow { + flow_id: 2, + start_node: None, + definition: None, + }, + Flow { + flow_id: 3, + start_node: None, + definition: None, + }, ], deleted_flow_ids: vec![], }; @@ -337,9 +384,21 @@ mod tests { { let response = FlowGetResponse { updated_flows: vec![ - Flow { flow_id: 1, start_node: None, definition: None }, - Flow { flow_id: 2, start_node: None, definition: None }, - Flow { flow_id: 3, start_node: None, definition: None }, + Flow { + flow_id: 1, + start_node: None, + definition: None, + }, + Flow { + flow_id: 2, + start_node: None, + definition: None, + }, + Flow { + flow_id: 3, + start_node: None, + definition: None, + }, ], deleted_flow_ids: vec![], }; @@ -395,4 +454,4 @@ mod tests { let sagittarius_url = "http://127.0.0.1:25565".to_string(); let _client = SagittariusActionClientBase::new(sagittarius_url).await; } -} \ No newline at end of file +} diff --git a/src/client/sagittarius/mod.rs b/aquila/src/client/sagittarius/mod.rs similarity index 100% rename from src/client/sagittarius/mod.rs rename to aquila/src/client/sagittarius/mod.rs diff --git a/src/configuration/config.rs b/aquila/src/configuration/config.rs similarity index 98% rename from src/configuration/config.rs rename to aquila/src/configuration/config.rs index ed8d961..3420442 100644 --- a/src/configuration/config.rs +++ b/aquila/src/configuration/config.rs @@ -1,6 +1,7 @@ use std::env; use std::fmt::{Debug, Display}; use std::str::FromStr; +use dotenv::from_filename; use log::{error, info}; use crate::configuration::environment::Environment; @@ -48,7 +49,7 @@ pub struct Config { /// Searches for the env. file at root level. Filename: `.env` impl Config { pub fn new() -> Self { - let result = dotenv::from_filename(".env"); + let result = from_filename("../../../.env"); match result { Ok(_) => info!(".env file loaded successfully"), Err(e) => error!("Error loading .env file: {}", e), diff --git a/src/configuration/environment.rs b/aquila/src/configuration/environment.rs similarity index 99% rename from src/configuration/environment.rs rename to aquila/src/configuration/environment.rs index 02f9b5c..afea0ce 100644 --- a/src/configuration/environment.rs +++ b/aquila/src/configuration/environment.rs @@ -12,4 +12,4 @@ impl Environment { _ => Environment::Development, } } -} \ No newline at end of file +} diff --git a/src/configuration/mod.rs b/aquila/src/configuration/mod.rs similarity index 100% rename from src/configuration/mod.rs rename to aquila/src/configuration/mod.rs diff --git a/src/configuration/start_configuration.rs b/aquila/src/configuration/start_configuration.rs similarity index 74% rename from src/configuration/start_configuration.rs rename to aquila/src/configuration/start_configuration.rs index d0580ac..101a511 100644 --- a/src/configuration/start_configuration.rs +++ b/aquila/src/configuration/start_configuration.rs @@ -1,16 +1,17 @@ -use std::fs::File; -use std::io::Read; -use std::sync::Arc; +use crate::client::sagittarius::flow_client::{SagittariusFlowClient, SagittariusFlowClientBase}; +use crate::configuration::config::Config; +use aquila_store::{FlowService, FlowServiceBase}; use async_trait::async_trait; -use clokwerk::{AsyncScheduler}; +use clokwerk::AsyncScheduler; use clokwerk::Interval::Seconds; use log::{debug, error, info}; use redis::aio::MultiplexedConnection; +use serde_json::from_str; +use std::fs::File; +use std::io::Read; +use std::sync::Arc; use tokio::sync::Mutex; use tucana::sagittarius::Flow; -use crate::client::sagittarius::flow_client::{SagittariusFlowClient, SagittariusFlowClientBase}; -use crate::configuration::config::Config; -use crate::service::flow_service::{FlowService, FlowServiceBase}; pub struct StartConfigurationBase { connection_arc: Arc>>, @@ -19,7 +20,10 @@ pub struct StartConfigurationBase { #[async_trait] pub trait StartConfiguration { - async fn new(connection_arc: Arc>>, config: Config) -> StartConfigurationBase; + async fn new( + connection_arc: Arc>>, + config: Config, + ) -> StartConfigurationBase; async fn init_flows_from_sagittarius(&mut self); async fn init_flows_from_json(mut self); } @@ -27,8 +31,14 @@ pub trait StartConfiguration { /// `Aquila's` startup configuration logic. #[async_trait] impl StartConfiguration for StartConfigurationBase { - async fn new(connection_arc: Arc>>, config: Config) -> StartConfigurationBase { - StartConfigurationBase { connection_arc, config } + async fn new( + connection_arc: Arc>>, + config: Config, + ) -> StartConfigurationBase { + StartConfigurationBase { + connection_arc, + config, + } } /// Function to initialize the connection to `Sagittarius` to receive latest flows. @@ -43,7 +53,8 @@ impl StartConfiguration for StartConfigurationBase { async fn init_flows_from_sagittarius(&mut self) { let flow_service = FlowServiceBase::new(self.connection_arc.clone()).await; let flow_service_arc = Arc::new(Mutex::new(flow_service)); - let mut sagittarius_client = SagittariusFlowClientBase::new(self.config.backend_url.clone(), flow_service_arc).await; + let mut sagittarius_client = + SagittariusFlowClientBase::new(self.config.backend_url.clone(), flow_service_arc).await; if !self.config.enable_scheduled_update { info!("Receiving flows from sagittarius once"); @@ -55,16 +66,14 @@ impl StartConfiguration for StartConfigurationBase { let schedule_interval = self.config.update_schedule_interval; let mut scheduler = AsyncScheduler::new(); - scheduler - .every(Seconds(schedule_interval)) - .run(move || { - let local_flow_client = Arc::new(Mutex::new(sagittarius_client.clone())); + scheduler.every(Seconds(schedule_interval)).run(move || { + let local_flow_client = Arc::new(Mutex::new(sagittarius_client.clone())); - async move { - let mut current_flow_client = local_flow_client.lock().await; - current_flow_client.send_start_request().await - } - }); + async move { + let mut current_flow_client = local_flow_client.lock().await; + current_flow_client.send_start_request().await + } + }); } /// Function to start `Aquila` from a JSON containing the flows. @@ -105,15 +114,18 @@ impl StartConfiguration for StartConfigurationBase { } } - let flows: Vec = match serde_json::from_str(&data) { + let flows: Vec = match from_str(&data) { Ok(flows) => flows, Err(error) => { error!("Error deserializing json file {}", error); - panic!("There was a problem deserializing the json file: {:?}", error); + panic!( + "There was a problem deserializing the json file: {:?}", + error + ); } }; info!("Loaded {} Flows!", &flows.len()); flow_service.insert_flows(flows).await; } -} \ No newline at end of file +} diff --git a/src/main.rs b/aquila/src/main.rs similarity index 89% rename from src/main.rs rename to aquila/src/main.rs index 41226a3..3ce3bc1 100644 --- a/src/main.rs +++ b/aquila/src/main.rs @@ -1,19 +1,19 @@ +use crate::configuration::config::Config; use crate::configuration::start_configuration::{StartConfiguration, StartConfigurationBase}; +use aquila_cache::build_connection; +use std::env::set_var; use std::sync::Arc; use tokio::sync::Mutex; -use crate::configuration::config::Config; -use crate::data::redis::build_connection; mod client; mod configuration; -mod data; -mod service; mod server; +mod service; #[tokio::main] async fn main() { // Configure logging - std::env::set_var("RUST_LOG", "info"); + set_var("RUST_LOG", "info"); json_env_logger2::init(); json_env_logger2::panic_hook(); @@ -29,4 +29,4 @@ async fn main() { let mut startup = StartConfigurationBase::new(connection, config).await; startup.init_flows_from_sagittarius().await; startup.init_flows_from_json().await -} \ No newline at end of file +} diff --git a/src/server/action/action_server.rs b/aquila/src/server/action/action_server.rs similarity index 100% rename from src/server/action/action_server.rs rename to aquila/src/server/action/action_server.rs diff --git a/src/server/action/mod.rs b/aquila/src/server/action/mod.rs similarity index 100% rename from src/server/action/mod.rs rename to aquila/src/server/action/mod.rs diff --git a/src/server/mod.rs b/aquila/src/server/mod.rs similarity index 100% rename from src/server/mod.rs rename to aquila/src/server/mod.rs diff --git a/src/service/action_service.rs b/aquila/src/service/action_service.rs similarity index 100% rename from src/service/action_service.rs rename to aquila/src/service/action_service.rs diff --git a/src/service/mod.rs b/aquila/src/service/mod.rs similarity index 51% rename from src/service/mod.rs rename to aquila/src/service/mod.rs index 6e7882b..5018d1d 100644 --- a/src/service/mod.rs +++ b/aquila/src/service/mod.rs @@ -1,2 +1 @@ -pub mod flow_service; pub mod action_service; \ No newline at end of file