diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 9fd45e0..82961c0 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -14,9 +14,16 @@ jobs: runs-on: ubuntu-latest + services: + mongodb: + image: nats + steps: - uses: actions/checkout@v4 - name: Build run: cargo build --verbose + env: + NATS_URL: "nats:4222" - name: Run tests run: cargo test --verbose + NATS_URL: "nats:4222" diff --git a/src/nkv.rs b/src/nkv.rs index 696c914..29cc096 100644 --- a/src/nkv.rs +++ b/src/nkv.rs @@ -6,6 +6,7 @@ use std::sync::Arc; use crate::persist_value::PersistValue; use crate::notifier::Notifier; use std::fmt; +use std::env; #[derive(Debug, PartialEq)] pub enum NotifyKeyValueError { @@ -47,10 +48,12 @@ pub struct NotifyKeyValue { impl NotifyKeyValue { pub fn new(path: std::path::PathBuf) -> Self { + let nats_url = env::var("NATS_URL") + .unwrap_or_else(|_| "nats://localhost:4222".to_string()); Self{ state: HashMap::new(), persist_path: path, - sock_path: "nats://localhost:4222".to_string(), + sock_path: nats_url, } } diff --git a/src/notifier.rs b/src/notifier.rs index b427a21..6a10c18 100644 --- a/src/notifier.rs +++ b/src/notifier.rs @@ -2,6 +2,7 @@ extern crate serde; extern crate serde_json; use serde::{Deserialize, Serialize}; +use std::env; #[derive(Serialize, Deserialize, Debug, PartialEq)] enum MessageType @@ -94,7 +95,10 @@ mod tests { // Helper function to create a Notifier instance for testing async fn create_test_notifier() -> Notifier { - let notifier = Notifier::new("nats://localhost:4222".into(), "topic".into()).await.unwrap(); + let nats_url = env::var("NATS_URL") + .unwrap_or_else(|_| "nats://localhost:4222".to_string()); + + let notifier = Notifier::new(nats_url, "topic".into()).await.unwrap(); notifier } diff --git a/src/srv.rs b/src/srv.rs index 4ebbd99..eb8bbd6 100644 --- a/src/srv.rs +++ b/src/srv.rs @@ -7,6 +7,7 @@ use crate::nkv::{self, NotifyKeyValue}; use crate::request_msg::{self, BaseResp, GetResp, PutResp, ServerResponse}; use http::StatusCode; use futures::StreamExt; +use std::env; pub struct PutMsg { key: String, @@ -254,7 +255,10 @@ mod tests { #[tokio::test] async fn test_server() { let temp_dir = TempDir::new().expect("Failed to create temporary directory"); - let srv = Server::new("localhost:4222".to_string(), temp_dir.path().to_path_buf()).await.unwrap(); + let nats_url = env::var("NATS_URL") + .unwrap_or_else(|_| "nats://localhost:4222".to_string()); + + let srv = Server::new(nats_url.to_string(), temp_dir.path().to_path_buf()).await.unwrap(); let put_tx = srv.put_tx(); let get_tx = srv.get_tx(); @@ -289,9 +293,11 @@ mod tests { let temp_dir = TempDir::new().expect("Failed to create temporary directory"); // creates background task where it serves threads - let _srv = Server::new("localhost:4222".to_string(), temp_dir.path().to_path_buf()).await.unwrap(); + let nats_url = env::var("NATS_URL") + .unwrap_or_else(|_| "nats://localhost:4222".to_string()); + + let _srv = Server::new(nats_url.clone(), temp_dir.path().to_path_buf()).await.unwrap(); - let nats_url = "localhost:4222".to_string(); let client = NatsClient::new(&nats_url).await.unwrap(); let value: Box<[u8]> = Box::new([1, 2, 3, 4, 5]);