From 0785ad67f5f7d19e9ec3fbf3fecf363f618a22aa Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Tue, 28 Jan 2020 10:10:52 +0000 Subject: [PATCH] Merge with develop --- Cargo.lock | 71 +++++++++++++++++- nym-client/Cargo.toml | 1 + nym-client/src/config/mod.rs | 81 +++++++++++++++++++++ nym-client/src/config/template.rs | 68 +++++++++++++++++ sample-configs/validator-config.toml.sample | 2 +- validator/Cargo.toml | 1 - validator/src/validator/config/mod.rs | 2 +- 7 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 nym-client/src/config/template.rs diff --git a/Cargo.lock b/Cargo.lock index 3af9d02bc2..f1a86d084a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -847,6 +847,20 @@ dependencies = [ "tokio-io", ] +[[package]] +name = "handlebars" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba758d094d31274eb49d15da6f326b96bf3185239a6359bf684f3d5321148900" +dependencies = [ + "log", + "pest", + "pest_derive", + "quick-error", + "serde", + "serde_json", +] + [[package]] name = "healthcheck" version = "0.1.0" @@ -1135,6 +1149,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" + [[package]] name = "matches" version = "0.1.8" @@ -1355,6 +1375,7 @@ dependencies = [ "dirs", "dotenv", "futures 0.3.1", + "handlebars", "healthcheck", "log", "mix-client", @@ -1428,7 +1449,6 @@ dependencies = [ "log", "pretty_env_logger", "serde", - "serde_derive", "tokio 0.2.10", "toml", ] @@ -1531,6 +1551,49 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "pest" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e4fb201c5c22a55d8b24fef95f78be52738e5e1361129be1b5e862ecdb6894a" +dependencies = [ + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b9fcf299b5712d06ee128a556c94709aaa04512c4dffb8ead07c5c998447fc0" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df43fd99896fd72c485fe47542c7b500e4ac1e8700bf995544d1317a60ded547" +dependencies = [ + "maplit", + "pest", + "sha-1", +] + [[package]] name = "pin-project" version = "0.4.7" @@ -2474,6 +2537,12 @@ version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" +[[package]] +name = "ucd-trie" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f00ed7be0c1ff1e24f46c3d2af4859f7e863672ba3a6e92e7cff702bf9f06c2" + [[package]] name = "unicase" version = "2.6.0" diff --git a/nym-client/Cargo.toml b/nym-client/Cargo.toml index 52f5cb89da..e9c7e7e053 100644 --- a/nym-client/Cargo.toml +++ b/nym-client/Cargo.toml @@ -18,6 +18,7 @@ curve25519-dalek = "1.2.3" dirs = "2.0.2" dotenv = "0.15.0" futures = "0.3.1" +handlebars = "3.0.1" log = "0.4" pem = "0.7.0" pretty_env_logger = "0.3" diff --git a/nym-client/src/config/mod.rs b/nym-client/src/config/mod.rs index 4bf0f95209..93c8b0af32 100644 --- a/nym-client/src/config/mod.rs +++ b/nym-client/src/config/mod.rs @@ -1 +1,82 @@ +use crate::config::persistance::pathfinder::ClientPathfinder; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + pub mod persistance; +mod template; + +#[derive(Serialize, Deserialize)] +pub struct Config { + client: Client, +} + +impl Default for Config { + fn default() -> Self { + unimplemented!() + } +} + +#[derive(Serialize, Deserialize)] +pub struct Client { + // nym_home_directory specifies absolute path to the home nym Clients directory. + // It is expected to use default value and hence .toml file should not redefine this field. + nym_home_directory: String, + + // ID specifies the human readable ID of this particular client. + // If not provided a pseudorandom id will be generated instead. + id: String, + + // URL to the directory server. + directory_server: String, + + // Path to file containing private identity key. + private_identity_key_file: PathBuf, + + // Path to file containing public identity key. + public_identity_key_file: PathBuf, + + // mix_apps_directory specifies directory for mixapps, such as a chat client, + // to store their app-specific data. + mix_apps_directory: String, + + // provider_id specifies ID of the provider to which the client should send messages. + // If initially omitted, a random provider will be chosen from the available topology. + provider_id: String, +} + +impl Default for Client { + fn default() -> Self { + unimplemented!() + } +} + +impl Client { + fn new() -> Self { + Default::default() + } + + fn with_id(mut self, id: String) -> Self { + self.id = id; + self + } + + fn with_pathfinder(mut self, pathfinder: ClientPathfinder) -> Self { + // pub config_dir: PathBuf, + // pub private_mix_key: PathBuf, + // pub public_mix_key: PathBuf, + self + } +} + +pub struct Logging {} + +impl Default for Logging { + fn default() -> Self { + unimplemented!() + } +} + +#[cfg(test)] +mod client_config { + use super::*; +} diff --git a/nym-client/src/config/template.rs b/nym-client/src/config/template.rs new file mode 100644 index 0000000000..9233c2b5fe --- /dev/null +++ b/nym-client/src/config/template.rs @@ -0,0 +1,68 @@ +use super::Config; +use handlebars::{Handlebars, TemplateRenderError}; + +fn config_template() -> &'static str { + // While using normal toml marshalling would have been way simpler with less overhead, + // I think it's useful to have comments attached to the saved config file to explain behaviour of + // particular fields. + // Note: any changes to the template must be reflected in the appropriate structs in mod.rs. + r#" +# This is a TOML config file. +# For more information, see https://github.com/toml-lang/toml + +##### main base client config options ##### + +[client] +# Human readable ID of this particular client. +id = "{{ client.id }}" + +# URL to the directory server. +directory_server = "{{ client.directory_server }}" + +# Path to file containing private identity key. +private_identity_key_file = "{{ client.private_identity_key }}" + +# Path to file containing public identity key. +public_identity_key_file = "{{ client.public_identity_key }}" + +##### additional client config options ##### + +# ID of the provider to which the client should send messages. +provider_id = "{{ client.provider_id }}" + +# directory for mixapps, such as a chat client, to store their app-specific data. +mixapps_directory = "{{ client.mix_apps_directory }}" + +##### advanced configuration options ##### + +# Absolute path to the home Nym Clients directory. +nym_home_directory = "{{ client.home_directory }}" + "# +} + +fn render_template(config: &Config) -> Result { + let reg = Handlebars::new(); + reg.render_template(config_template(), &config) +} + +#[cfg(test)] +mod config_template { + use super::*; + + #[test] + fn it_works_for_default_config() { + render_template(&Default::default()).unwrap(); + } + + #[test] + fn it_works_for_dummy_config() { + let dummy_cfg = Config { + client: crate::config::Client { + id: "foomp".to_string(), + }, + }; + let render = render_template(&dummy_cfg).unwrap(); + + println!("{}", render); + } +} diff --git a/sample-configs/validator-config.toml.sample b/sample-configs/validator-config.toml.sample index c60c223287..2997abea29 100644 --- a/sample-configs/validator-config.toml.sample +++ b/sample-configs/validator-config.toml.sample @@ -1,7 +1,7 @@ [healthcheck] #directory-server = "http://localhost:8080" -directory-server = "https://qa-directory.nymtech.net" +directory-server = "https://directory.nymtech.net" interval = 10.0 test-packets-per-node = 2 # in seconds resolution-timeout = 5 # in seconds \ No newline at end of file diff --git a/validator/Cargo.toml b/validator/Cargo.toml index 6a6e83066c..99c880b52c 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -15,7 +15,6 @@ futures = "0.3.1" log = "0.4" pretty_env_logger = "0.3" serde = "1.0.104" -serde_derive = "1.0.104" tokio = { version = "0.2", features = ["full"] } toml = "0.5.5" diff --git a/validator/src/validator/config/mod.rs b/validator/src/validator/config/mod.rs index 4ef70b9c7e..a6ec068a4f 100644 --- a/validator/src/validator/config/mod.rs +++ b/validator/src/validator/config/mod.rs @@ -1,4 +1,4 @@ -use serde_derive::Deserialize; +use serde::Deserialize; #[derive(Deserialize, Debug)] pub struct Config {