Split config module into sub-modules

This commit is contained in:
silverpill 2022-08-09 21:21:17 +00:00
parent a804d0e8bb
commit 0e8943a24b
5 changed files with 79 additions and 62 deletions

40
src/config/blockchain.rs Normal file
View file

@ -0,0 +1,40 @@
use std::collections::HashMap;
use std::path::PathBuf;
use serde::Deserialize;
use crate::ethereum::utils::{parse_caip2_chain_id, ChainIdError};
fn default_chain_sync_step() -> u64 { 1000 }
fn default_chain_reorg_max_depth() -> u64 { 10 }
#[derive(Clone, Deserialize)]
pub struct BlockchainConfig {
// CAIP-2 chain ID (https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md)
pub chain_id: String,
// Additional information for clients
pub chain_info: Option<HashMap<String, String>>,
pub contract_address: String,
pub contract_dir: PathBuf,
pub api_url: String,
// Block explorer base URL (should be compatible with https://eips.ethereum.org/EIPS/eip-3091)
pub explorer_url: Option<String>,
// Instance private key
pub signing_key: String,
#[serde(default = "default_chain_sync_step")]
pub chain_sync_step: u64,
#[serde(default = "default_chain_reorg_max_depth")]
pub chain_reorg_max_depth: u64,
}
impl BlockchainConfig {
pub fn try_ethereum_chain_id(&self) -> Result<u32, ChainIdError> {
parse_caip2_chain_id(&self.chain_id)
}
pub fn ethereum_chain_id(&self) -> u32 {
self.try_ethereum_chain_id().unwrap()
}
}

29
src/config/environment.rs Normal file
View file

@ -0,0 +1,29 @@
use std::str::FromStr;
use crate::errors::ConversionError;
#[derive(Clone, Debug)]
pub enum Environment {
Development,
Production,
}
impl Default for Environment {
#[cfg(feature = "production")]
fn default() -> Self { Self::Production }
#[cfg(not(feature = "production"))]
fn default() -> Self { Self::Development }
}
impl FromStr for Environment {
type Err = ConversionError;
fn from_str(val: &str) -> Result<Self, Self::Err> {
let environment = match val {
"development" => Environment::Development,
"production" => Environment::Production,
_ => return Err(ConversionError),
};
Ok(environment)
}
}

View file

@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::str::FromStr;
@ -11,7 +10,6 @@ use url::Url;
use crate::activitypub::constants::ACTOR_KEY_SUFFIX;
use crate::activitypub::identifiers::local_instance_actor_id;
use crate::errors::ConversionError;
use crate::ethereum::utils::{parse_caip2_chain_id, ChainIdError};
use crate::utils::crypto::{
deserialize_private_key,
generate_private_key,
@ -20,31 +18,8 @@ use crate::utils::crypto::{
use crate::utils::currencies::Currency;
use crate::utils::files::{set_file_permissions, write_file};
#[derive(Clone, Debug)]
pub enum Environment {
Development,
Production,
}
impl Default for Environment {
#[cfg(feature = "production")]
fn default() -> Self { Self::Production }
#[cfg(not(feature = "production"))]
fn default() -> Self { Self::Development }
}
impl FromStr for Environment {
type Err = ConversionError;
fn from_str(val: &str) -> Result<Self, Self::Err> {
let environment = match val {
"development" => Environment::Development,
"production" => Environment::Production,
_ => return Err(ConversionError),
};
Ok(environment)
}
}
use super::blockchain::BlockchainConfig;
use super::environment::Environment;
struct EnvConfig {
environment: Environment,
@ -74,40 +49,6 @@ fn parse_env() -> EnvConfig {
}
}
fn default_chain_sync_step() -> u64 { 1000 }
fn default_chain_reorg_max_depth() -> u64 { 10 }
#[derive(Clone, Deserialize)]
pub struct BlockchainConfig {
// CAIP-2 chain ID (https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md)
pub chain_id: String,
// Additional information for clients
pub chain_info: Option<HashMap<String, String>>,
pub contract_address: String,
pub contract_dir: PathBuf,
pub api_url: String,
// Block explorer base URL (should be compatible with https://eips.ethereum.org/EIPS/eip-3091)
pub explorer_url: Option<String>,
// Instance private key
pub signing_key: String,
#[serde(default = "default_chain_sync_step")]
pub chain_sync_step: u64,
#[serde(default = "default_chain_reorg_max_depth")]
pub chain_reorg_max_depth: u64,
}
impl BlockchainConfig {
fn try_ethereum_chain_id(&self) -> Result<u32, ChainIdError> {
parse_caip2_chain_id(&self.chain_id)
}
pub fn ethereum_chain_id(&self) -> u32 {
self.try_ethereum_chain_id().unwrap()
}
}
fn default_log_level() -> LogLevel { LogLevel::Info }
fn default_login_message() -> String { "Do not sign this message on other sites!".to_string() }

7
src/config/mod.rs Normal file
View file

@ -0,0 +1,7 @@
mod blockchain;
mod environment;
mod main;
pub use blockchain::BlockchainConfig;
pub use environment::Environment;
pub use main::{parse_config, Config, Instance};

View file

@ -10,7 +10,7 @@ use tokio::sync::Mutex;
use mitra::activitypub::views as activitypub;
use mitra::atom::views as atom;
use mitra::config::{Environment, parse_config};
use mitra::config::{parse_config, Environment};
use mitra::database::{get_database_client, create_pool};
use mitra::database::migrate::apply_migrations;
use mitra::ethereum::contracts::get_contracts;