diff --git a/.env b/.env index edc80e8..aca7254 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ DB_URL=postgres://postgres:postgres@localhost:5432/postgres QUEUE_NAME=pagamento-queue -RABBITMQ_URL=amqp://rabbitmq:rabbitmq@127.0.0.1:5672/%2f +RABBITMQ_ADDR=amqp://rabbitmq:rabbitmq@127.0.0.1:5672/%2f ENV=dev \ No newline at end of file diff --git a/src/adapters/rabbitmq_pagament_update_subscriber.rs b/src/adapters/rabbitmq_pagament_update_subscriber.rs index 0ee837e..6dfb9e1 100644 --- a/src/adapters/rabbitmq_pagament_update_subscriber.rs +++ b/src/adapters/rabbitmq_pagament_update_subscriber.rs @@ -42,7 +42,7 @@ impl RabbitMQPagamentoUpdateSubscriber { self, ) -> Result<()> { let res: Result<()> = async_global_executor::block_on(async { - let conn = Connection::connect(self.config.rabbitmq_addr.as_str(), ConnectionProperties::default()).await?; + let conn = Connection::connect(self.config.rabbitmq_addr.unwrap().as_str(), ConnectionProperties::default()).await?; let channel = conn.create_channel().await?; let mut consumer = channel .basic_consume( diff --git a/src/api/config.rs b/src/api/config.rs index 42cdfbd..655eea4 100644 --- a/src/api/config.rs +++ b/src/api/config.rs @@ -36,7 +36,7 @@ pub struct Config { pub env: Env, pub db_url: String, pub api_key: String, - pub rabbitmq_addr: String, + pub rabbitmq_addr: Option, pub queue_name: String, } @@ -48,7 +48,11 @@ impl Config { let db_url = env::var("DB_URL") .unwrap_or("postgres://postgres:postgres@localhost:5432/postgres".to_string()); let api_key = env::var("API_KEY").unwrap_or("api_key".to_string()); - let rabbitmq_addr = env::var("RABBITMQ_ADDR").unwrap_or("amqp://rabbitmq:rabbitmq@localhost:5672/%2f".to_string()); + let rabbitmq_addr = match env::var("RABBITMQ_ADDR"){ + Ok(addr) => Some(addr), + Err(_) => None + }; + let queue_name = env::var("QUEUE_NAME").unwrap_or("queue_name".to_string()); Config { diff --git a/src/api/server.rs b/src/api/server.rs index 69d4396..f5556ca 100644 --- a/src/api/server.rs +++ b/src/api/server.rs @@ -86,13 +86,20 @@ pub async fn main() -> Rocket { let user_group_validator: Arc = Arc::new(user_group_validator); - let pagamento_update_subscriber = RabbitMQPagamentoUpdateSubscriber::new( - config.clone(), - produto_gateway.clone(), - pedido_gateway.clone(), - ); - - pagamento_update_subscriber.subscribe(); + match config.rabbitmq_addr { + Some(_) => { + let pagamento_update_subscriber = RabbitMQPagamentoUpdateSubscriber::new( + config.clone(), + produto_gateway.clone(), + pedido_gateway.clone(), + ); + + pagamento_update_subscriber.subscribe() + } + None => { + println!("Not subscribed to RabbitMQ"); + } + } rocket::build() .mount("/", routes![redirect_to_docs]) diff --git a/src/bin/api.rs b/src/bin/api.rs index a42af42..0ddb9b0 100644 --- a/src/bin/api.rs +++ b/src/bin/api.rs @@ -15,3 +15,35 @@ async fn main() -> Result<(), rocket::Error> { } } } + +#[cfg(test)] +mod tests { + use super::*; + use std::{env, thread}; + + #[tokio::test] + async fn test_api() { + test_api_works().await; + test_api_breaks().await; + } + + async fn test_api_works() { + env::set_var("ENV", "test"); + env::remove_var("RABBITMQ_ADDR"); + + let service = thread::spawn(|| { + let _rocket = main(); + }); + + assert!(service.join().is_ok()); + } + + async fn test_api_breaks() { + env::set_var("ENV", "prod"); + + thread::spawn(|| { + let rocket = main(); + assert!(rocket.is_err()); + }); + } +}