Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #38

Merged
merged 5 commits into from
Dec 2, 2024
Merged

Dev #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ Cargo.lock
/tmp
tags

tarpaulin-report.html
.DS_Store
all_code.txt
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ tokio = { version = "1.41", features = ["full"] }
# Utils
async-trait = { version = "0.1" }
uuid = { version = "1.11", features = ["v4", "serde"] }
rand = { version = "0.8" }
15 changes: 13 additions & 2 deletions capp-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ edition = "2021"

[dependencies]
serde = { workspace = true }
serde_yaml = "0.9"
serde_json = { workspace = true }
reqwest = { workspace = true, optional = true }
tokio = { workspace = true, optional = true}
tracing = { workspace = true }
rand = { workspace = true }
thiserror = { workspace = true }

backoff = { version = "0.4", optional = true, features = ["tokio"] }
regex = { version = "1.11", optional = true }
indexmap = { version = "2.6", optional = true }
url = { version = "2.5", optional = true }
serde_yaml = "0.9"

[dev-dependencies]
tempfile = "3"

[features]
http = ["dep:reqwest", "dep:tokio"]
http = ["dep:reqwest", "dep:tokio", "dep:backoff"]
router = ["dep:regex", "dep:indexmap", "dep:url"]
111 changes: 92 additions & 19 deletions capp-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ use std::{
path,
};

#[derive(thiserror::Error, Debug)]
pub enum ConfigError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("YAML parsing error: {0}")]
YamlParse(#[from] serde_yaml::Error),
#[error("Line parsing error: {0}")]
LineParse(String),
}

pub trait Configurable {
fn config(&self) -> &serde_yaml::Value;

// read configuration from yaml config
fn load_config(
config_file_path: impl AsRef<path::Path>,
) -> Result<serde_yaml::Value, io::Error> {
) -> Result<serde_yaml::Value, ConfigError> {
let content: String = fs::read_to_string(config_file_path)?;
let config: serde_yaml::Value = serde_yaml::from_str(&content).unwrap();
let config: serde_yaml::Value = serde_yaml::from_str(&content)?;
Ok(config)
}

/// Load Vec<String> from file with path `file path`
fn load_text_file_lines(
file_path: impl AsRef<path::Path>,
) -> Result<Vec<String>, io::Error> {
) -> Result<Vec<String>, ConfigError> {
let file = fs::File::open(file_path)?;
let lines: Vec<String> = io::BufReader::new(file)
let lines = io::BufReader::new(file)
.lines()
.map(|l| l.expect("Could not parse line"))
.collect();

.map(|l| l.map_err(|e| ConfigError::LineParse(e.to_string())))
.collect::<Result<Vec<_>, _>>()?;
Ok(lines)
}

fn load_text_file_content(
file_path: impl AsRef<path::Path>,
) -> Result<String, io::Error> {
fs::read_to_string(file_path)
}

/// Extract Value from config using dot notation i.e. "app.concurrency"
fn get_config_value(&self, key: &str) -> Option<&serde_yaml::Value> {
let keys: Vec<&str> = key.split('.').collect();
Expand Down Expand Up @@ -74,19 +77,22 @@ pub trait Configurable {
#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;

pub struct Application {
pub struct TestApp {
config: serde_yaml::Value,
user_agents: Option<Vec<String>>,
}

impl Configurable for Application {
impl Configurable for TestApp {
fn config(&self) -> &serde_yaml::Value {
&self.config
}
}

impl Application {
impl TestApp {
fn from_config(config_file_path: impl AsRef<path::Path>) -> Self {
let config = Self::load_config(config_file_path);
Self {
Expand All @@ -103,17 +109,62 @@ mod tests {
#[test]
fn test_load_config() {
let config_path = "../tests/simple_config.yml";
let app = Application::from_config(config_path);
let app = TestApp::from_config(config_path);

assert_eq!(app.config["app"]["threads"].as_u64(), Some(4));
assert_eq!(app.config()["app"]["max_queue"].as_u64(), Some(500));
assert_eq!(app.user_agents, None);
}

#[test]
fn test_load_config_valid_yaml() {
let dir = tempdir().unwrap();
let config_path = dir.path().join("config.yml");
let mut file = File::create(&config_path).unwrap();
writeln!(file, "key: value\napp:\n setting: 42").unwrap();

let config = TestApp::load_config(&config_path);
assert!(config.is_ok());
let config = config.unwrap();
assert_eq!(config["key"].as_str(), Some("value"));
assert_eq!(config["app"]["setting"].as_i64(), Some(42));
}

#[test]
fn test_load_config_invalid_yaml() {
let dir = tempdir().unwrap();
let config_path = dir.path().join("config.yml");
let mut file = File::create(&config_path).unwrap();
writeln!(file, "invalid: : yaml: content").unwrap();

let config = TestApp::load_config(&config_path);
assert!(matches!(config, Err(ConfigError::YamlParse(_))));
}

#[test]
fn test_load_text_file_lines() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("test.txt");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "line1\nline2\nline3").unwrap();

let lines = TestApp::load_text_file_lines(&file_path);
assert!(lines.is_ok());
let lines = lines.unwrap();
assert_eq!(lines, vec!["line1", "line2", "line3"]);
}

#[test]
fn test_get_config_value_empty_keys() {
let config_path = "../tests/simple_config.yml";
let app = TestApp::from_config(config_path);
assert_eq!(app.get_config_value(""), None);
}

#[test]
fn test_get_config_value() {
let config_path = "../tests/simple_config.yml";
let app = Application::from_config(config_path);
let app = TestApp::from_config(config_path);

assert_eq!(
app.get_config_value("logging.log_to_redis")
Expand All @@ -123,10 +174,32 @@ mod tests {
)
}

#[test]
fn test_get_config_value_recursive() {
let yaml = r#"
app:
nested:
value: 42
"#;
let config: serde_yaml::Value = serde_yaml::from_str(yaml).unwrap();
let app = TestApp {
config,
user_agents: None,
};

assert_eq!(
app.get_config_value("app.nested.value")
.and_then(|v| v.as_i64()),
Some(42)
);
assert_eq!(app.get_config_value("app.missing.value"), None);
assert_eq!(app.get_config_value("missing"), None);
}

#[test]
fn test_load_lines() {
let config_path = "../tests/simple_config.yml";
let mut app = Application::from_config(config_path);
let mut app = TestApp::from_config(config_path);
let uas_file_path = {
app.get_config_value("app.user_agents_file")
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions capp/src/http.rs → capp-config/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//!
//! # Example
//! ```no_run
//! use capp::http::{HttpClientParams, build_http_client};
//! use capp_config::http::{HttpClientParams, build_http_client};
//! use serde_yaml::Value;
//!
//! let config: Value = serde_yaml::from_str(r#"
Expand Down Expand Up @@ -249,7 +249,7 @@ mod tests {
let config: serde_yaml::Value =
serde_yaml::from_str(YAML_CONF_TEXT).unwrap();
let client = build_http_client(HttpClientParams::from_config(
&config.get("http").unwrap(),
config.get("http").unwrap(),
"hellobot",
));
assert!(client.is_ok());
Expand All @@ -261,7 +261,7 @@ mod tests {
let config: serde_yaml::Value =
serde_yaml::from_str(WRONG_YAML_CONF_TEXT).unwrap();
let _ = build_http_client(HttpClientParams::from_config(
&config.get("http").unwrap(),
config.get("http").unwrap(),
"hellobot",
));
}
Expand Down
3 changes: 3 additions & 0 deletions capp-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod config;
#[cfg(feature = "http")]
pub mod healthcheck;
pub mod http;
pub mod proxy;
pub mod router;
2 changes: 1 addition & 1 deletion capp/src/proxy.rs → capp-config/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ProxyConfig {
Some(seq) => seq
.iter()
.filter_map(|v| v.as_str())
.flat_map(|uri| Self::expand_uri(uri))
.flat_map(Self::expand_uri)
.collect(),
None => {
if let Some(uri) = config["uri"].as_str() {
Expand Down
3 changes: 2 additions & 1 deletion capp/src/router.rs → capp-config/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! # Examples
//!
//! ```
//! use your_crate::router::Router;
//! use capp_config::router::Router;
//! use url::Url;
//!
//! let mut router = Router::new();
Expand Down Expand Up @@ -190,6 +190,7 @@ impl URLClassifier {
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;

Expand Down
1 change: 0 additions & 1 deletion capp-queue/src/backend/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! In-memory implementation of TaskStorage trait. The storage allows tasks to be
//! pushed to and popped from a queue, and also allows tasks to be set and
//! retrieved by their UUID.

use crate::queue::{TaskQueue, TaskQueueError};
use crate::task::{Task, TaskId};
use async_trait::async_trait;
Expand Down
4 changes: 0 additions & 4 deletions capp-queue/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ use crate::task::{Task, TaskId};
pub enum TaskQueueError {
#[error("Queue error: {0}")]
QueueError(String),

#[error("Ser/De error: {0}")]
SerdeError(String),

#[error("Task not found: {0}")]
TaskNotFound(TaskId),

#[error("Queue is empty")]
QueueEmpty,

#[cfg(feature = "redis")]
#[error("Redis error")]
RedisError(#[from] rustis::Error),
Expand Down
10 changes: 1 addition & 9 deletions capp-queue/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ impl<D: Clone> Task<D> {
}
}

//*****************************************************************************
// TaskId with ser/de traits implemented (to convert underlaying Uuid)
//*****************************************************************************

impl TaskId {
pub fn new() -> Self {
Self(Uuid::new_v4())
Expand Down Expand Up @@ -127,10 +123,6 @@ impl std::fmt::Display for TaskId {
}
}

//*****************************************************************************
// Tests
//*****************************************************************************

#[cfg(test)]
mod tests {
use core::panic;
Expand All @@ -146,7 +138,7 @@ mod tests {
#[test]
fn task_id_serde() {
let task = Task::new(TaskData { value: 1 });
let task_id = task.task_id.clone();
let task_id = task.task_id;
let serialized_task_value = serde_json::to_value(task).unwrap();
let serialized_task_json = serialized_task_value.to_string();
let desrialized_task: Task<TaskData> =
Expand Down
20 changes: 4 additions & 16 deletions capp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,14 @@ uuid.workspace = true
# TODO: figure out why can't have derive_builder as common dep.
# derive_builder.workspace = true

capp-config = { path = "../capp-config", features=["http"] }
capp-config = { path = "../capp-config", features=["http", "router"] }
capp-queue = { path = "../capp-queue", features=["redis"] }

# async-trait = { version = "0.1" }
backoff = { version = "0.4", optional = true, features = ["tokio"] }
derive_builder = { version = "0.20" }
reqwest = { version = "0.12", features = ["gzip", "rustls-tls", "json"], optional = true }
# serde = { version = "1.0", features = ["derive"] }
# serde_json = { version = "1.0" }
serde_yaml = "0.9"
# thiserror = { version = "1" }
# tokio = { version = "1.41", features = ["full"] }
# uuid = { version = "1.11", features = ["v4", "serde"] }
rustis = { version = "0.13", features = ["tokio-runtime"], optional = true }
# tracing = "0.1"
# tracing-subscriber = "0.3"
# anyhow = "1"
tracing-futures = "0.2"
indexmap = "2.6"
url = "2.5"
regex = "1.11"
rand = { version = "0.8", optional = true }

[dev-dependencies]
capp = { path = ".", features = ["http", "healthcheck", "redis"] }
Expand All @@ -73,8 +59,10 @@ rand = "0.8"
md5 = "0.7"
url = "2.5"
base64 = "0.22"
tempfile = "3"

[features]
http = ["dep:backoff", "dep:reqwest", "dep:rand", "capp-config/http"]
http = ["dep:reqwest", "capp-config/http"]
router = ["capp-config/router"]
healthcheck = ["dep:reqwest"]
redis = ["dep:rustis", "capp-queue/redis"]
Loading
Loading