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 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/config.rs') 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, }) } } -- cgit v1.2.3