upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-11-21 04:04:05 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-11-21 04:04:05 +0000
commit2e799fa7ec57d284c643df8b8dc54471470f5c59 (patch)
tree0d8affa3e742d34e8d5a251b4ae50678c10a10d5 /src
parent25b206c00766579f2322619f4716ca999f9b92ec (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/config.rs42
-rw-r--r--src/nostr/builder.rs40
2 files changed, 73 insertions, 9 deletions
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};
2use serde::{Deserialize, Serialize}; 2use serde::{Deserialize, Serialize};
3use std::env; 3use std::env;
4 4
5/// Database backend type for the relay
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7#[serde(rename_all = "lowercase")]
8pub enum DatabaseBackend {
9 /// In-memory database (default, fastest, no persistence)
10 Memory,
11 /// NostrDB backend (persistent, optimized for Nostr)
12 NostrDb,
13 /// LMDB backend (persistent, general purpose)
14 Lmdb,
15}
16
17impl Default for DatabaseBackend {
18 fn default() -> Self {
19 Self::Memory
20 }
21}
22
23impl std::str::FromStr for DatabaseBackend {
24 type Err = anyhow::Error;
25
26 fn from_str(s: &str) -> Result<Self> {
27 match s.to_lowercase().as_str() {
28 "memory" => Ok(Self::Memory),
29 "nostrdb" => Ok(Self::NostrDb),
30 "lmdb" => Ok(Self::Lmdb),
31 _ => Err(anyhow::anyhow!(
32 "Invalid database backend: {}. Valid options: memory, nostrdb, lmdb",
33 s
34 )),
35 }
36 }
37}
38
5#[derive(Debug, Clone, Serialize, Deserialize)] 39#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Config { 40pub struct Config {
7 pub domain: String, 41 pub domain: String,
@@ -11,6 +45,7 @@ pub struct Config {
11 pub git_data_path: String, 45 pub git_data_path: String,
12 pub relay_data_path: String, 46 pub relay_data_path: String,
13 pub bind_address: String, 47 pub bind_address: String,
48 pub database_backend: DatabaseBackend,
14} 49}
15 50
16impl Config { 51impl Config {
@@ -18,6 +53,12 @@ impl Config {
18 // Load .env file if present 53 // Load .env file if present
19 dotenvy::dotenv().ok(); 54 dotenvy::dotenv().ok();
20 55
56 // Parse database backend from environment
57 let database_backend = env::var("NGIT_DATABASE_BACKEND")
58 .ok()
59 .and_then(|s| s.parse().ok())
60 .unwrap_or_default();
61
21 Ok(Config { 62 Ok(Config {
22 domain: env::var("NGIT_DOMAIN").unwrap_or_else(|_| "localhost:8080".to_string()), 63 domain: env::var("NGIT_DOMAIN").unwrap_or_else(|_| "localhost:8080".to_string()),
23 owner_npub: env::var("NGIT_OWNER_NPUB").context("NGIT_OWNER_NPUB must be set")?, 64 owner_npub: env::var("NGIT_OWNER_NPUB").context("NGIT_OWNER_NPUB must be set")?,
@@ -31,6 +72,7 @@ impl Config {
31 .unwrap_or_else(|_| "./data/relay".to_string()), 72 .unwrap_or_else(|_| "./data/relay".to_string()),
32 bind_address: env::var("NGIT_BIND_ADDRESS") 73 bind_address: env::var("NGIT_BIND_ADDRESS")
33 .unwrap_or_else(|_| "127.0.0.1:8080".to_string()), 74 .unwrap_or_else(|_| "127.0.0.1:8080".to_string()),
75 database_backend,
34 }) 76 })
35 } 77 }
36} 78}
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};
11use nostr::{EventId, Filter, Kind, PublicKey}; 11use nostr::{EventId, Filter, Kind, PublicKey};
12use nostr_relay_builder::prelude::*; 12use nostr_relay_builder::prelude::*;
13 13
14use crate::config::Config; 14use crate::config::{Config, DatabaseBackend};
15use crate::nostr::events::{ 15use crate::nostr::events::{
16 validate_announcement, validate_state, KIND_REPOSITORY_ANNOUNCEMENT, KIND_REPOSITORY_STATE, 16 validate_announcement, validate_state, KIND_REPOSITORY_ANNOUNCEMENT, KIND_REPOSITORY_STATE,
17}; 17};
@@ -397,14 +397,36 @@ pub fn create_relay(config: &Config) -> Result<LocalRelay> {
397 // Determine database path 397 // Determine database path
398 let db_path = Path::new(&config.relay_data_path); 398 let db_path = Path::new(&config.relay_data_path);
399 399
400 // Create database - using in-memory for now, can switch to persistent later 400 // Create database based on configuration
401 // TODO: Add configuration for NostrDB or LMDB backends 401 let database = match config.database_backend {
402 let database = Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { 402 DatabaseBackend::Memory => {
403 events: true, 403 tracing::info!("Using in-memory database (no persistence)");
404 max_events: Some(100_000), 404 Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions {
405 })); 405 events: true,
406 406 max_events: Some(100_000),
407 tracing::info!("Using in-memory database (path: {})", db_path.display()); 407 }))
408 }
409 DatabaseBackend::NostrDb => {
410 tracing::info!("Using NostrDB backend at: {}", db_path.display());
411 // TODO: Implement NostrDB backend once nostr-relay-builder supports it
412 // For now, fall back to memory database
413 tracing::warn!("NostrDB backend not yet implemented, using in-memory database");
414 Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions {
415 events: true,
416 max_events: Some(100_000),
417 }))
418 }
419 DatabaseBackend::Lmdb => {
420 tracing::info!("Using LMDB backend at: {}", db_path.display());
421 // TODO: Implement LMDB backend once nostr-relay-builder supports it
422 // For now, fall back to memory database
423 tracing::warn!("LMDB backend not yet implemented, using in-memory database");
424 Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions {
425 events: true,
426 max_events: Some(100_000),
427 }))
428 }
429 };
408 430
409 // Build relay with GRASP-01 validation 431 // Build relay with GRASP-01 validation
410 // Clone Arc for the write policy so both relay and policy can access the database 432 // Clone Arc for the write policy so both relay and policy can access the database