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/config.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/nostr/builder.rs | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index b4dd853..f04b7d8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,40 @@ use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; use std::env; +/// Database backend type for the relay +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum DatabaseBackend { + /// In-memory database (default, fastest, no persistence) + Memory, + /// NostrDB backend (persistent, optimized for Nostr) + NostrDb, + /// LMDB backend (persistent, general purpose) + Lmdb, +} + +impl Default for DatabaseBackend { + fn default() -> Self { + Self::Memory + } +} + +impl std::str::FromStr for DatabaseBackend { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "memory" => Ok(Self::Memory), + "nostrdb" => Ok(Self::NostrDb), + "lmdb" => Ok(Self::Lmdb), + _ => Err(anyhow::anyhow!( + "Invalid database backend: {}. Valid options: memory, nostrdb, lmdb", + s + )), + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { pub domain: String, @@ -11,6 +45,7 @@ pub struct Config { pub git_data_path: String, pub relay_data_path: String, pub bind_address: String, + pub database_backend: DatabaseBackend, } impl Config { @@ -18,6 +53,12 @@ impl Config { // Load .env file if present dotenvy::dotenv().ok(); + // Parse database backend from environment + let database_backend = env::var("NGIT_DATABASE_BACKEND") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or_default(); + Ok(Config { domain: env::var("NGIT_DOMAIN").unwrap_or_else(|_| "localhost:8080".to_string()), owner_npub: env::var("NGIT_OWNER_NPUB").context("NGIT_OWNER_NPUB must be set")?, @@ -31,6 +72,7 @@ impl Config { .unwrap_or_else(|_| "./data/relay".to_string()), bind_address: env::var("NGIT_BIND_ADDRESS") .unwrap_or_else(|_| "127.0.0.1:8080".to_string()), + database_backend, }) } } 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