From 2e799fa7ec57d284c643df8b8dc54471470f5c59 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 21 Nov 2025 04:04:05 +0000 Subject: feat: add database backend configuration options Add environment variable configuration for database backend selection: - Added DatabaseBackend enum (memory, nostrdb, lmdb) in src/config.rs - Updated relay builder to use configured backend in src/nostr/builder.rs - Added NGIT_DATABASE_BACKEND to .env.example with documentation - Updated docs/reference/configuration.md with backend comparison table NostrDB and LMDB backends prepared for future implementation when nostr-relay-builder adds support. Currently defaults to in-memory database with warning logs when persistent backends are selected. --- src/nostr/builder.rs | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'src/nostr/builder.rs') diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs index 09b2fe2..547db8e 100644 --- a/src/nostr/builder.rs +++ b/src/nostr/builder.rs @@ -11,7 +11,7 @@ use nostr::prelude::{Alphabet, SingleLetterTag}; use nostr::{EventId, Filter, Kind, PublicKey}; use nostr_relay_builder::prelude::*; -use crate::config::Config; +use crate::config::{Config, DatabaseBackend}; use crate::nostr::events::{ validate_announcement, validate_state, KIND_REPOSITORY_ANNOUNCEMENT, KIND_REPOSITORY_STATE, }; @@ -397,14 +397,36 @@ pub fn create_relay(config: &Config) -> Result { // Determine database path let db_path = Path::new(&config.relay_data_path); - // Create database - using in-memory for now, can switch to persistent later - // TODO: Add configuration for NostrDB or LMDB backends - let database = Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { - events: true, - max_events: Some(100_000), - })); - - tracing::info!("Using in-memory database (path: {})", db_path.display()); + // Create database based on configuration + let database = match config.database_backend { + DatabaseBackend::Memory => { + tracing::info!("Using in-memory database (no persistence)"); + Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { + events: true, + max_events: Some(100_000), + })) + } + DatabaseBackend::NostrDb => { + tracing::info!("Using NostrDB backend at: {}", db_path.display()); + // TODO: Implement NostrDB backend once nostr-relay-builder supports it + // For now, fall back to memory database + tracing::warn!("NostrDB backend not yet implemented, using in-memory database"); + Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { + events: true, + max_events: Some(100_000), + })) + } + DatabaseBackend::Lmdb => { + tracing::info!("Using LMDB backend at: {}", db_path.display()); + // TODO: Implement LMDB backend once nostr-relay-builder supports it + // For now, fall back to memory database + tracing::warn!("LMDB backend not yet implemented, using in-memory database"); + Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { + events: true, + max_events: Some(100_000), + })) + } + }; // Build relay with GRASP-01 validation // Clone Arc for the write policy so both relay and policy can access the database -- cgit v1.2.3