Split config module into sub-modules
This commit is contained in:
parent
a804d0e8bb
commit
0e8943a24b
5 changed files with 79 additions and 62 deletions
40
src/config/blockchain.rs
Normal file
40
src/config/blockchain.rs
Normal 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
29
src/config/environment.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,3 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::os::unix::fs::MetadataExt;
|
use std::os::unix::fs::MetadataExt;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -11,7 +10,6 @@ use url::Url;
|
||||||
use crate::activitypub::constants::ACTOR_KEY_SUFFIX;
|
use crate::activitypub::constants::ACTOR_KEY_SUFFIX;
|
||||||
use crate::activitypub::identifiers::local_instance_actor_id;
|
use crate::activitypub::identifiers::local_instance_actor_id;
|
||||||
use crate::errors::ConversionError;
|
use crate::errors::ConversionError;
|
||||||
use crate::ethereum::utils::{parse_caip2_chain_id, ChainIdError};
|
|
||||||
use crate::utils::crypto::{
|
use crate::utils::crypto::{
|
||||||
deserialize_private_key,
|
deserialize_private_key,
|
||||||
generate_private_key,
|
generate_private_key,
|
||||||
|
@ -20,31 +18,8 @@ use crate::utils::crypto::{
|
||||||
use crate::utils::currencies::Currency;
|
use crate::utils::currencies::Currency;
|
||||||
use crate::utils::files::{set_file_permissions, write_file};
|
use crate::utils::files::{set_file_permissions, write_file};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
use super::blockchain::BlockchainConfig;
|
||||||
pub enum Environment {
|
use super::environment::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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct EnvConfig {
|
struct EnvConfig {
|
||||||
environment: Environment,
|
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_log_level() -> LogLevel { LogLevel::Info }
|
||||||
|
|
||||||
fn default_login_message() -> String { "Do not sign this message on other sites!".to_string() }
|
fn default_login_message() -> String { "Do not sign this message on other sites!".to_string() }
|
7
src/config/mod.rs
Normal file
7
src/config/mod.rs
Normal 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};
|
|
@ -10,7 +10,7 @@ use tokio::sync::Mutex;
|
||||||
|
|
||||||
use mitra::activitypub::views as activitypub;
|
use mitra::activitypub::views as activitypub;
|
||||||
use mitra::atom::views as atom;
|
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::{get_database_client, create_pool};
|
||||||
use mitra::database::migrate::apply_migrations;
|
use mitra::database::migrate::apply_migrations;
|
||||||
use mitra::ethereum::contracts::get_contracts;
|
use mitra::ethereum::contracts::get_contracts;
|
||||||
|
|
Loading…
Reference in a new issue