From 514c7ae237afa4d4d5a67b5ec158c20bc826a1aa Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 5 Nov 2021 23:41:30 +0000 Subject: [PATCH] Refactor config, make ethereum integration optional --- config.yaml.example | 6 ++++-- src/config.rs | 18 ++++++------------ src/ethereum/gate.rs | 4 +++- src/ethereum/nft.rs | 6 ++++-- src/main.rs | 16 ++++++++++------ 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index 1b8a783..59fbb20 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -1,4 +1,5 @@ database_url: postgres://mitra:mitra@127.0.0.1:5432/mitra +storage_dir: files http_host: '127.0.0.1' http_port: 8380 @@ -6,13 +7,14 @@ http_port: 8380 # domain name instance_uri: myserver.net instance_title: myserver -instance_short_description: myserver is a federated social network +instance_short_description: my fedi instance # Long description can contain markdown syntax -instance_description: myserver is a federated social network +instance_description: my fedi instance registrations_open: false # Login message must contain instance URL login_message: 'Sign this message to log in to https://myserver.net. Do not sign this message on other sites!' +ethereum_contract_dir: contracts ethereum_json_rpc_url: 'http://127.0.0.1:8545' # Block explorer base URL (must be compatible with https://eips.ethereum.org/EIPS/eip-3091) ethereum_explorer_url: null diff --git a/src/config.rs b/src/config.rs index bb15cee..d55ec16 100644 --- a/src/config.rs +++ b/src/config.rs @@ -58,10 +58,6 @@ fn parse_env() -> EnvConfig { fn default_environment() -> Environment { Environment::Development } -fn default_storage_dir() -> PathBuf { PathBuf::from("files") } - -fn default_contract_dir() -> PathBuf { PathBuf::from("contracts") } - #[derive(Clone, Deserialize)] pub struct EthereumContract { pub address: String, @@ -80,8 +76,6 @@ pub struct Config { // Core settings pub database_url: String, - - #[serde(default = "default_storage_dir")] pub storage_dir: PathBuf, pub http_host: String, @@ -99,9 +93,7 @@ pub struct Config { pub login_message: String, // Ethereum & IPFS - #[serde(default = "default_contract_dir")] - pub contract_dir: PathBuf, - + pub ethereum_contract_dir: Option, pub ethereum_json_rpc_url: Option, pub ethereum_explorer_url: Option, pub ethereum_contract: Option, @@ -141,10 +133,12 @@ pub fn parse_config() -> Config { config.version = env.crate_version; // Validate config if !config.storage_dir.exists() { - panic!("storage_dir does not exist"); + panic!("storage directory does not exist"); }; - if !config.contract_dir.exists() { - panic!("contract directory does not exist"); + if let Some(contract_dir) = &config.ethereum_contract_dir { + if !contract_dir.exists() { + panic!("contract directory does not exist"); + }; }; config.try_instance_url().expect("invalid instance URI"); diff --git a/src/ethereum/gate.rs b/src/ethereum/gate.rs index db63dbf..e71911b 100644 --- a/src/ethereum/gate.rs +++ b/src/ethereum/gate.rs @@ -13,10 +13,12 @@ pub async fn is_allowed_user( let json_rpc_url = config.ethereum_json_rpc_url.as_ref() .ok_or(EthereumError::ImproperlyConfigured)?; let web3 = connect(json_rpc_url)?; + let contract_dir = config.ethereum_contract_dir.as_ref() + .ok_or(EthereumError::ImproperlyConfigured)?; let ethereum_config = config.ethereum_contract.as_ref() .ok_or(EthereumError::ImproperlyConfigured)?; - let manager_abi = load_abi(&config.contract_dir, MANAGER)?; + let manager_abi = load_abi(contract_dir, MANAGER)?; let manager_address = parse_address(ðereum_config.address)?; let manager = Contract::from_json( web3.eth(), diff --git a/src/ethereum/nft.rs b/src/ethereum/nft.rs index f6a91b6..9ec1b81 100644 --- a/src/ethereum/nft.rs +++ b/src/ethereum/nft.rs @@ -33,10 +33,12 @@ pub async fn get_nft_contract( let json_rpc_url = config.ethereum_json_rpc_url.as_ref() .ok_or(EthereumError::ImproperlyConfigured)?; let web3 = connect(json_rpc_url)?; + let contract_dir = config.ethereum_contract_dir.as_ref() + .ok_or(EthereumError::ImproperlyConfigured)?; let ethereum_config = config.ethereum_contract.as_ref() .ok_or(EthereumError::ImproperlyConfigured)?; - let manager_abi = load_abi(&config.contract_dir, MANAGER)?; + let manager_abi = load_abi(contract_dir, MANAGER)?; let manager_address = parse_address(ðereum_config.address)?; let manager = Contract::from_json( web3.eth(), @@ -48,7 +50,7 @@ pub async fn get_nft_contract( "collectible", (), None, Options::default(), None, ).await?; - let token_abi = load_abi(&config.contract_dir, COLLECTIBLE)?; + let token_abi = load_abi(contract_dir, COLLECTIBLE)?; let token = Contract::from_json( web3.eth(), token_address, diff --git a/src/main.rs b/src/main.rs index 55444b0..ce08913 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,7 @@ async fn main() -> std::io::Result<()> { .allow_any_header() }, }; - App::new() + let mut app = App::new() .wrap(ActixLogger::new("%r : %s : %{r}a")) .wrap(cors_config) .wrap(create_auth_error_handler()) @@ -71,10 +71,6 @@ async fn main() -> std::io::Result<()> { "/media", config.media_dir(), )) - .service(actix_files::Files::new( - "/contracts", - config.contract_dir.clone(), - )) .service(oauth_api_scope()) .service(profile_directory) .service(account_api_scope()) @@ -89,7 +85,15 @@ async fn main() -> std::io::Result<()> { .service(activitypub_scope()) .service(get_object) .service(nodeinfo::get_nodeinfo) - .service(nodeinfo::get_nodeinfo_2_0) + .service(nodeinfo::get_nodeinfo_2_0); + if let Some(contract_dir) = &config.ethereum_contract_dir { + // Serve artifacts if available + app = app.service(actix_files::Files::new( + "/contracts", + contract_dir, + )); + } + app }) .workers(num_workers) .bind(http_socket_addr)?