From 5191f6787d947e0b9fa03bed5bf746c0e74a3e76 Mon Sep 17 00:00:00 2001 From: Patrice Tisserand Date: Fri, 19 Apr 2024 15:27:17 +0200 Subject: [PATCH] indexer: add lib.rs to allow using crate in another binary --- apps/indexer/src/ethereum_indexer/indexer.rs | 1 - apps/indexer/src/lib.rs | 13 +++++++ apps/indexer/src/main.rs | 39 ++++---------------- apps/indexer/src/storage/mod.rs | 17 +++++++++ 4 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 apps/indexer/src/lib.rs diff --git a/apps/indexer/src/ethereum_indexer/indexer.rs b/apps/indexer/src/ethereum_indexer/indexer.rs index 0fef5d4d..c5886a16 100644 --- a/apps/indexer/src/ethereum_indexer/indexer.rs +++ b/apps/indexer/src/ethereum_indexer/indexer.rs @@ -39,7 +39,6 @@ where xchain_txor_config: XchainTxConfig, ) -> Result> { let client = EthereumClient::new(config.clone()).await?; - /// TODO: should we add moralis api key to configuration file? let pricer = MoralisPrice::new(None); Ok(EthereumIndexer { client, diff --git a/apps/indexer/src/lib.rs b/apps/indexer/src/lib.rs new file mode 100644 index 00000000..64ccd411 --- /dev/null +++ b/apps/indexer/src/lib.rs @@ -0,0 +1,13 @@ +pub mod config; +pub mod ethereum_indexer; +pub mod handlers; +pub mod price; +pub mod starknet_indexer; +pub mod storage; +pub mod utils; + +#[derive(Clone, Debug)] +pub struct ChainsBlocks { + pub sn: u64, + pub eth: u64, +} diff --git a/apps/indexer/src/main.rs b/apps/indexer/src/main.rs index 77dfb636..20f74f0f 100644 --- a/apps/indexer/src/main.rs +++ b/apps/indexer/src/main.rs @@ -1,24 +1,19 @@ //! Starklane indexer main entry point. extern crate config as external_crate_config; -use crate::config::StarklaneIndexerConfig; +use starklane_indexer::config::StarklaneIndexerConfig; use anyhow::Result; use axum::{http::Request, middleware::Next, response::Response, routing::get, Router, Server}; use clap::Parser; -use ethereum_indexer::EthereumIndexer; -use handlers::{requests, AppState}; -use starknet_indexer::StarknetIndexer; +use starklane_indexer::ethereum_indexer::EthereumIndexer; +use starklane_indexer::handlers::{requests, AppState}; +use starklane_indexer::starknet_indexer::StarknetIndexer; use std::sync::Arc; -use storage::mongo::MongoStore; +use starklane_indexer::storage::{extract_database_name, mongo::MongoStore}; +use starklane_indexer::ChainsBlocks; use tokio::sync::RwLock as AsyncRwLock; -pub mod config; -pub mod ethereum_indexer; -pub mod handlers; -pub mod price; -pub mod starknet_indexer; -pub mod storage; -pub mod utils; + const ENV_PREFIX: &'static str = "INDEXER"; const ENV_SEPARATOR: &'static str = "__"; // "_" can't be used since we have key with '_' in json @@ -36,11 +31,6 @@ struct Args { api_server_ip: Option, } -#[derive(Clone, Debug)] -pub struct ChainsBlocks { - sn: u64, - eth: u64, -} async fn version_header(req: Request, next: Next) -> Response { let mut response = next.run(req).await; @@ -153,18 +143,3 @@ async fn main() -> Result<()> { Ok(()) } -/// Extracts database name from connection string. -/// Expecting the database name to be the latest fragment -/// of the string after the right most '/'. -fn extract_database_name(connection_string: &str) -> Option<&str> { - if let Some(pos) = connection_string.rfind('/') { - let db_name_start = pos + 1; - if let Some(pos) = connection_string[db_name_start..].find('?') { - Some(&connection_string[db_name_start..db_name_start + pos]) - } else { - Some(&connection_string[db_name_start..]) - } - } else { - None - } -} diff --git a/apps/indexer/src/storage/mod.rs b/apps/indexer/src/storage/mod.rs index 871c5322..1ae67c71 100644 --- a/apps/indexer/src/storage/mod.rs +++ b/apps/indexer/src/storage/mod.rs @@ -179,3 +179,20 @@ pub struct PendingWithdraw { pub message_hash: [u8; 32], } + + +/// Extracts database name from connection string. +/// Expecting the database name to be the latest fragment +/// of the string after the right most '/'. +pub fn extract_database_name(connection_string: &str) -> Option<&str> { + if let Some(pos) = connection_string.rfind('/') { + let db_name_start = pos + 1; + if let Some(pos) = connection_string[db_name_start..].find('?') { + Some(&connection_string[db_name_start..db_name_start + pos]) + } else { + Some(&connection_string[db_name_start..]) + } + } else { + None + } +}