|
1 | 1 | use anyhow::{Result, anyhow}; |
2 | | -use bcr_ebill_core::NodeId; |
| 2 | +use bcr_common::core::NodeId; |
| 3 | +use bcr_ebill_persistence::db::surreal::SurrealWrapper; |
| 4 | +#[cfg(not(target_arch = "wasm32"))] |
| 5 | +use bcr_ebill_persistence::get_surreal_db; |
| 6 | +use bcr_ebill_persistence::{ |
| 7 | + ContactStoreApi, NostrChainEventStoreApi, NostrEventOffsetStoreApi, NotificationStoreApi, |
| 8 | + SurrealBillChainStore, SurrealBillStore, SurrealCompanyChainStore, SurrealCompanyStore, |
| 9 | + SurrealContactStore, SurrealDbConfig, SurrealIdentityChainStore, SurrealIdentityStore, |
| 10 | + SurrealNostrChainEventStore, SurrealNostrEventOffsetStore, SurrealNotificationStore, |
| 11 | + bill::{BillChainStoreApi, BillStoreApi}, |
| 12 | + company::{CompanyChainStoreApi, CompanyStoreApi}, |
| 13 | + db::{ |
| 14 | + email_notification::SurrealEmailNotificationStore, |
| 15 | + identity_proof::SurrealIdentityProofStore, mint::SurrealMintStore, |
| 16 | + nostr_contact_store::SurrealNostrContactStore, |
| 17 | + nostr_send_queue::SurrealNostrEventQueueStore, |
| 18 | + }, |
| 19 | + file_upload::FileUploadStoreApi, |
| 20 | + identity::{IdentityChainStoreApi, IdentityStoreApi}, |
| 21 | + identity_proof::IdentityProofStoreApi, |
| 22 | + mint::MintStoreApi, |
| 23 | + nostr::{NostrContactStoreApi, NostrQueuedMessageStoreApi}, |
| 24 | + notification::EmailNotificationStoreApi, |
| 25 | +}; |
3 | 26 | use bitcoin::Network; |
4 | | -use std::sync::OnceLock; |
| 27 | +use log::error; |
| 28 | +use std::sync::{Arc, OnceLock}; |
5 | 29 |
|
6 | | -mod blockchain; |
7 | 30 | pub mod constants; |
8 | | -pub mod data; |
9 | 31 | pub mod external; |
10 | | -pub mod persistence; |
11 | 32 | pub mod service; |
12 | 33 | #[cfg(test)] |
13 | 34 | mod tests; |
14 | 35 | pub mod util; |
15 | 36 |
|
16 | | -pub use blockchain::Block; |
17 | | -pub use blockchain::Blockchain; |
18 | | -pub use blockchain::bill::BillOpCode; |
19 | | -pub use persistence::DbContext; |
20 | | -pub use persistence::Error as PersistenceError; |
21 | | -pub use persistence::db::SurrealDbConfig; |
22 | | -pub use persistence::get_db_context; |
23 | | -pub use persistence::notification::NotificationFilter; |
24 | | - |
25 | 37 | #[derive(Debug, Clone)] |
26 | 38 | pub struct Config { |
27 | 39 | pub app_url: url::Url, |
28 | 40 | pub bitcoin_network: String, |
29 | 41 | pub esplora_base_url: url::Url, |
30 | 42 | pub db_config: SurrealDbConfig, |
31 | | - pub data_dir: String, |
| 43 | + pub files_db_config: SurrealDbConfig, |
32 | 44 | pub nostr_config: NostrConfig, |
33 | 45 | pub mint_config: MintConfig, |
34 | 46 | pub payment_config: PaymentConfig, |
@@ -110,3 +122,101 @@ pub fn init(conf: Config) -> Result<()> { |
110 | 122 | pub fn get_config() -> &'static Config { |
111 | 123 | CONFIG.get().expect("E-Bill API is not initialized") |
112 | 124 | } |
| 125 | + |
| 126 | +/// A container for all persistence related dependencies. |
| 127 | +#[derive(Clone)] |
| 128 | +pub struct DbContext { |
| 129 | + pub contact_store: Arc<dyn ContactStoreApi>, |
| 130 | + pub bill_store: Arc<dyn BillStoreApi>, |
| 131 | + pub bill_blockchain_store: Arc<dyn BillChainStoreApi>, |
| 132 | + pub identity_store: Arc<dyn IdentityStoreApi>, |
| 133 | + pub identity_chain_store: Arc<dyn IdentityChainStoreApi>, |
| 134 | + pub company_chain_store: Arc<dyn CompanyChainStoreApi>, |
| 135 | + pub company_store: Arc<dyn CompanyStoreApi>, |
| 136 | + pub file_upload_store: Arc<dyn FileUploadStoreApi>, |
| 137 | + pub nostr_event_offset_store: Arc<dyn NostrEventOffsetStoreApi>, |
| 138 | + pub notification_store: Arc<dyn NotificationStoreApi>, |
| 139 | + pub email_notification_store: Arc<dyn EmailNotificationStoreApi>, |
| 140 | + pub queued_message_store: Arc<dyn NostrQueuedMessageStoreApi>, |
| 141 | + pub nostr_contact_store: Arc<dyn NostrContactStoreApi>, |
| 142 | + pub mint_store: Arc<dyn MintStoreApi>, |
| 143 | + pub nostr_chain_event_store: Arc<dyn NostrChainEventStoreApi>, |
| 144 | + pub identity_proof_store: Arc<dyn IdentityProofStoreApi>, |
| 145 | +} |
| 146 | + |
| 147 | +/// Creates a new instance of the DbContext with the given SurrealDB configuration. |
| 148 | +pub async fn get_db_context( |
| 149 | + #[allow(unused)] conf: &Config, |
| 150 | +) -> bcr_ebill_persistence::Result<DbContext> { |
| 151 | + #[cfg(not(target_arch = "wasm32"))] |
| 152 | + let db = get_surreal_db(&conf.db_config).await?; |
| 153 | + #[cfg(not(target_arch = "wasm32"))] |
| 154 | + let files_db = get_surreal_db(&conf.files_db_config).await?; |
| 155 | + #[cfg(not(target_arch = "wasm32"))] |
| 156 | + let surreal_wrapper = SurrealWrapper { |
| 157 | + db: db.clone(), |
| 158 | + files: false, |
| 159 | + }; |
| 160 | + |
| 161 | + #[cfg(not(target_arch = "wasm32"))] |
| 162 | + let files_surreal_wrapper = SurrealWrapper { |
| 163 | + db: files_db.clone(), |
| 164 | + files: true, |
| 165 | + }; |
| 166 | + |
| 167 | + #[cfg(target_arch = "wasm32")] |
| 168 | + let surreal_wrapper = SurrealWrapper { files: false }; |
| 169 | + |
| 170 | + #[cfg(target_arch = "wasm32")] |
| 171 | + let files_surreal_wrapper = SurrealWrapper { files: true }; |
| 172 | + |
| 173 | + let company_store = Arc::new(SurrealCompanyStore::new(surreal_wrapper.clone())); |
| 174 | + let file_upload_store = Arc::new( |
| 175 | + bcr_ebill_persistence::db::file_upload::FileUploadStore::new(files_surreal_wrapper), |
| 176 | + ); |
| 177 | + |
| 178 | + if let Err(e) = file_upload_store.cleanup_temp_uploads().await { |
| 179 | + error!("Error cleaning up temp uploads: {e}"); |
| 180 | + } |
| 181 | + |
| 182 | + let contact_store = Arc::new(SurrealContactStore::new(surreal_wrapper.clone())); |
| 183 | + |
| 184 | + let bill_store = Arc::new(SurrealBillStore::new(surreal_wrapper.clone())); |
| 185 | + let bill_blockchain_store = Arc::new(SurrealBillChainStore::new(surreal_wrapper.clone())); |
| 186 | + |
| 187 | + let identity_store = Arc::new(SurrealIdentityStore::new(surreal_wrapper.clone())); |
| 188 | + let identity_chain_store = Arc::new(SurrealIdentityChainStore::new(surreal_wrapper.clone())); |
| 189 | + let identity_proof_store = Arc::new(SurrealIdentityProofStore::new(surreal_wrapper.clone())); |
| 190 | + let company_chain_store = Arc::new(SurrealCompanyChainStore::new(surreal_wrapper.clone())); |
| 191 | + |
| 192 | + let nostr_event_offset_store = |
| 193 | + Arc::new(SurrealNostrEventOffsetStore::new(surreal_wrapper.clone())); |
| 194 | + let notification_store = Arc::new(SurrealNotificationStore::new(surreal_wrapper.clone())); |
| 195 | + let email_notification_store = |
| 196 | + Arc::new(SurrealEmailNotificationStore::new(surreal_wrapper.clone())); |
| 197 | + |
| 198 | + let queued_message_store = Arc::new(SurrealNostrEventQueueStore::new(surreal_wrapper.clone())); |
| 199 | + let nostr_contact_store = Arc::new(SurrealNostrContactStore::new(surreal_wrapper.clone())); |
| 200 | + let mint_store = Arc::new(SurrealMintStore::new(surreal_wrapper.clone())); |
| 201 | + let nostr_chain_event_store = |
| 202 | + Arc::new(SurrealNostrChainEventStore::new(surreal_wrapper.clone())); |
| 203 | + |
| 204 | + Ok(DbContext { |
| 205 | + contact_store, |
| 206 | + bill_store, |
| 207 | + bill_blockchain_store, |
| 208 | + identity_store, |
| 209 | + identity_chain_store, |
| 210 | + company_chain_store, |
| 211 | + company_store, |
| 212 | + file_upload_store, |
| 213 | + nostr_event_offset_store, |
| 214 | + notification_store, |
| 215 | + email_notification_store, |
| 216 | + queued_message_store, |
| 217 | + nostr_contact_store, |
| 218 | + mint_store, |
| 219 | + nostr_chain_event_store, |
| 220 | + identity_proof_store, |
| 221 | + }) |
| 222 | +} |
0 commit comments