Refactor config, make ethereum integration optional

This commit is contained in:
silverpill 2021-11-05 23:41:30 +00:00
parent d975b00990
commit 514c7ae237
5 changed files with 27 additions and 23 deletions

View file

@ -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

View file

@ -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<PathBuf>,
pub ethereum_json_rpc_url: Option<String>,
pub ethereum_explorer_url: Option<String>,
pub ethereum_contract: Option<EthereumContract>,
@ -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");

View file

@ -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(&ethereum_config.address)?;
let manager = Contract::from_json(
web3.eth(),

View file

@ -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(&ethereum_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,

View file

@ -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)?