From 1184f07519fbc0c2b82b4f1283ce6ee88bfcfaf1 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 5 Jun 2025 18:12:16 +0200 Subject: [PATCH 1/5] dependencies: updated code0-flow to 0.0.13 --- Cargo.lock | 23 +++++++++-------------- Cargo.toml | 7 ++----- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a6ee36..3e9c63f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -557,11 +557,12 @@ dependencies = [ [[package]] name = "code0-flow" -version = "0.0.12" +version = "0.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cf7a86a2ba2b0428289b0dc14b6a78d60ff94f84b80eb970e64382acac70319" +checksum = "ecc2b4e3ce2fb62521a5fc6825074525cc88269218e4919c1e4d3a9a83ba197b" dependencies = [ "async-trait", + "dotenv", "futures-lite 2.6.0", "lapin", "log", @@ -602,17 +603,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "config" -version = "0.1.0" -dependencies = [ - "env_logger", - "log", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "const-oid" version = "0.9.6" @@ -756,6 +746,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "either" version = "1.15.0" @@ -2065,7 +2061,6 @@ name = "rest" version = "0.1.0" dependencies = [ "code0-flow", - "config", "env_logger", "http 0.0.0", "log", diff --git a/Cargo.toml b/Cargo.toml index e68315b..cfec66b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [workspace] -members = ["crates/config", "crates/http", "crates/validator", "adapter/rest"] +members = ["crates/http", "crates/validator", "adapter/rest"] [workspace.package] version = "0.0.0" edition = "2021" [workspace.dependencies] -code0-flow = { version = "0.0.12" } +code0-flow = { version = "0.0.13" } tucana = { version = "0.0.28", features = ["aquila"] } serde_json = { version = "1.0.138" } serde = "1.0.219" @@ -20,9 +20,6 @@ tokio = { version = "1.44.1", features = ["rt-multi-thread"] } uuid = { version = "1.16.0", features = ["v4"] } tonic = "0.13.0" -[workspace.dependencies.config] -path = "../draco/crates/config" - [workspace.dependencies.http] path = "../draco/crates/http" From 92b14687276c1c8a5c77f98bdab17a90435acb1f Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 5 Jun 2025 18:12:30 +0200 Subject: [PATCH 2/5] drop: removed old config system --- crates/config/Cargo.toml | 14 ------ crates/config/src/lib.rs | 101 --------------------------------------- 2 files changed, 115 deletions(-) delete mode 100644 crates/config/Cargo.toml delete mode 100644 crates/config/src/lib.rs diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml deleted file mode 100644 index 6ce52d9..0000000 --- a/crates/config/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "config" -version = "0.1.0" -edition = "2021" - -[lib] -proc-macro = true - -[dependencies] -syn = { workspace = true } -quote = { workspace = true } -proc-macro2 = { workspace = true } -log = { workspace = true } -env_logger = { workspace = true } diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs deleted file mode 100644 index 1cf95c9..0000000 --- a/crates/config/src/lib.rs +++ /dev/null @@ -1,101 +0,0 @@ -use proc_macro::TokenStream; -use quote::quote; -use syn::{parse_macro_input, DeriveInput}; - -#[proc_macro_derive(FromEnv)] -pub fn from_env_macro(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as DeriveInput); - let struct_name = &input.ident; - - // Extract struct fields - let fields = if let syn::Data::Struct(s) = input.data { - s.fields - } else { - panic!("FromEnv can only be used on structs"); - }; - - // Generate parsing logic for each field - let field_parsing = fields.iter().map(|f| { - let field_name = f.ident.as_ref().unwrap(); - let field_type = &f.ty; - let env_var = field_name.to_string().to_uppercase(); // Convert to ENV_VAR format - - quote! { - #field_name: std::env::var(#env_var) - .expect(&format!("Missing env var: {}", #env_var)) - .parse::<#field_type>() - .expect(&format!("Failed to parse env var: {}", #env_var)) - } - }); - - // Generate print statements for each field - let field_print = fields.iter().map(|f| { - let field_name = f.ident.as_ref().unwrap(); - let field_str = field_name.to_string(); - - quote! { - log::info!(" {}: {}", #field_str, self.#field_name); - } - }); - - // Generate parsing logic for each field from file - let field_parsing_file = fields.iter().map(|f| { - let field_name = f.ident.as_ref().unwrap(); - let field_type = &f.ty; - let env_var = field_name.to_string().to_uppercase(); // Convert to ENV_VAR format - - quote! { - #field_name: config.get(#env_var) - .expect(&format!("Missing config key: {}", #env_var)) - .parse::<#field_type>() - .expect(&format!("Failed to parse config value: {}", #env_var)) - } - }); - - let expanded = quote! { - impl #struct_name { - pub fn from_env() -> Self { - let config = Self { - #(#field_parsing),* - }; - log::info!("Configuration loaded from environment:"); - config.print(); - config - } - - pub fn from_file(path: &str) -> Self { - use std::collections::HashMap; - use std::fs::File; - use std::io::{BufRead, BufReader}; - - let file = File::open(path).expect("Failed to open config file"); - let reader = BufReader::new(file); - let mut config: HashMap = HashMap::new(); - - for line in reader.lines() { - let line = line.expect("Failed to read line from config file"); - if line.trim().starts_with('#') || line.trim().is_empty() { - continue; - } - - if let Some((key, value)) = line.split_once('=') { - config.insert(key.trim().to_uppercase(), value.trim().to_string()); - } - } - - let config = Self { - #(#field_parsing_file),* - }; - log::info!("Configuration loaded from file '{}':", path); - config.print(); - config - } - - fn print(&self) { - #(#field_print)* - } - } - }; - - TokenStream::from(expanded) -} From 77684490bf8c9c56b96955e017f94d7d5a9e8da7 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 5 Jun 2025 18:12:40 +0200 Subject: [PATCH 3/5] feat: added example env --- .env-example | 6 ++++++ .gitignore | 2 ++ 2 files changed, 8 insertions(+) create mode 100644 .env-example diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..1f0a3bd --- /dev/null +++ b/.env-example @@ -0,0 +1,6 @@ +ENVIRONMENT='development' +MODE='dynamic' +REDIS_URL='redis://localhost:6379' +RABBITMQ_URL='amqp://localhost:5672' +AQUILA_URL='http://localhost:8080' +PORT=8080 diff --git a/.gitignore b/.gitignore index 80ca3e5..80fad13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ */target target + +.env From 82a1e37678d11f3fde9fbd1e0a5ab882eba992dc Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 5 Jun 2025 18:12:53 +0200 Subject: [PATCH 4/5] feat: added new config system to rest server --- adapter/rest/Cargo.toml | 1 - adapter/rest/src/config/mod.rs | 53 ++++++++++++++++++++++++++++++++++ adapter/rest/src/main.rs | 20 +++++-------- 3 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 adapter/rest/src/config/mod.rs diff --git a/adapter/rest/Cargo.toml b/adapter/rest/Cargo.toml index eac8078..a8e3913 100644 --- a/adapter/rest/Cargo.toml +++ b/adapter/rest/Cargo.toml @@ -5,7 +5,6 @@ edition = "2021" [dependencies] http = { workspace = true } -config = { workspace = true } validator = { workspace = true } code0-flow = { workspace = true } tokio = { workspace = true } diff --git a/adapter/rest/src/config/mod.rs b/adapter/rest/src/config/mod.rs new file mode 100644 index 0000000..4864378 --- /dev/null +++ b/adapter/rest/src/config/mod.rs @@ -0,0 +1,53 @@ +use code0_flow::flow_config::{env_with_default, environment::Environment, mode::Mode}; + +/// Struct for all relevant `Aquila` startup configurations +pub struct Config { + /// Aquila environment + /// + /// Options: + /// `development` (default) + /// `staging` + /// `production` + pub environment: Environment, + + /// Aquila mode + /// + /// Options: + /// `static` (default) + /// `hybrid` + pub mode: Mode, + + /// URL to the Redis Server. + /// Default none + pub redis_url: String, + + /// Verification Token required for internal communication + pub rabbitmq_url: String, + + /// URL to the `Sagittarius` Server. + pub aquila_url: String, + + /// Port for the HTTP server + pub port: u16, +} + +/// Implementation for all relevant `Aquila` startup configurations +/// +/// Behavior: +/// Searches for the env. file at root level. Filename: `.env` +impl Config { + pub fn new() -> Self { + Config { + environment: env_with_default("ENVIRONMENT", Environment::Development), + mode: env_with_default("MODE", Mode::STATIC), + redis_url: env_with_default("REDIS_URL", String::from("redis://localhost:6379")), + rabbitmq_url: env_with_default("RABBITMQ_URL", String::from("amqp://localhost:5672")), + aquila_url: env_with_default("AQUILA_URL", String::from("http://localhost:8080")), + port: env_with_default("PORT", 8080), + } + } + + pub fn is_static(&self) -> bool { + self.mode == Mode::STATIC + } +} diff --git a/adapter/rest/src/main.rs b/adapter/rest/src/main.rs index 3e5b9db..8689206 100644 --- a/adapter/rest/src/main.rs +++ b/adapter/rest/src/main.rs @@ -1,15 +1,16 @@ +mod config; pub mod queue; pub mod store; mod types; use code0_flow::{ + flow_config::mode::Mode, flow_queue::service::RabbitmqClient, flow_store::{ connection::create_flow_store_connection, service::{FlowStoreService, FlowStoreServiceBase}, }, }; -use config::FromEnv; use http::{ request::HttpRequest, response::HttpResponse, @@ -20,6 +21,8 @@ use std::{future::Future, pin::Pin, sync::Arc}; use tokio::sync::Mutex; use types::{get_data_types, get_flow_types}; +use crate::config::Config; + pub struct FlowConnectionHandler { flow_store: Arc>, rabbitmq_client: Arc, @@ -49,25 +52,18 @@ impl AsyncHandler for FlowConnectionHandler { } } -#[derive(FromEnv)] -pub struct Config { - port: u16, - redis_url: String, - rabbitmq_url: String, - aquila_url: String, - is_static: bool, -} - #[tokio::main] async fn main() { env_logger::Builder::from_default_env() .filter_level(log::LevelFilter::Debug) .init(); + code0_flow::flow_config::load_env_file(); + log::info!("Starting Draco REST server"); - let config = Config::from_file("./.env"); + let config = Config::new(); - if !config.is_static { + if config.mode != Mode::STATIC { let update_client = code0_flow::flow_definition::FlowUpdateService::from_url(config.aquila_url.clone()) .with_data_types(get_data_types()) From f88b3a560deb1a3b15c14bd93a4ca5db88727879 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 5 Jun 2025 18:21:55 +0200 Subject: [PATCH 5/5] ref: removed 'Aquila' leftovers --- adapter/rest/src/config/mod.rs | 4 +--- adapter/rest/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/adapter/rest/src/config/mod.rs b/adapter/rest/src/config/mod.rs index 4864378..938bef1 100644 --- a/adapter/rest/src/config/mod.rs +++ b/adapter/rest/src/config/mod.rs @@ -1,9 +1,7 @@ use code0_flow::flow_config::{env_with_default, environment::Environment, mode::Mode}; -/// Struct for all relevant `Aquila` startup configurations +/// Struct for all relevant `Draco` startup configurations pub struct Config { - /// Aquila environment - /// /// Options: /// `development` (default) /// `staging` diff --git a/adapter/rest/src/main.rs b/adapter/rest/src/main.rs index 8689206..b13e3ca 100644 --- a/adapter/rest/src/main.rs +++ b/adapter/rest/src/main.rs @@ -63,7 +63,7 @@ async fn main() { log::info!("Starting Draco REST server"); let config = Config::new(); - if config.mode != Mode::STATIC { + if !config.is_static() { let update_client = code0_flow::flow_definition::FlowUpdateService::from_url(config.aquila_url.clone()) .with_data_types(get_data_types())