diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-21 04:04:05 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-21 04:04:05 +0000 |
| commit | 2e799fa7ec57d284c643df8b8dc54471470f5c59 (patch) | |
| tree | 0d8affa3e742d34e8d5a251b4ae50678c10a10d5 | |
| parent | 25b206c00766579f2322619f4716ca999f9b92ec (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.
| -rw-r--r-- | .env.example | 6 | ||||
| -rw-r--r-- | docs/reference/configuration.md | 41 | ||||
| -rw-r--r-- | src/config.rs | 42 | ||||
| -rw-r--r-- | src/nostr/builder.rs | 40 |
4 files changed, 120 insertions, 9 deletions
diff --git a/.env.example b/.env.example index 291d00a..0a27bbb 100644 --- a/.env.example +++ b/.env.example | |||
| @@ -14,6 +14,12 @@ NGIT_RELAY_DESCRIPTION=A GRASP-compliant Git relay with Nostr authorization | |||
| 14 | NGIT_GIT_DATA_PATH=./data/git | 14 | NGIT_GIT_DATA_PATH=./data/git |
| 15 | NGIT_RELAY_DATA_PATH=./data/relay | 15 | NGIT_RELAY_DATA_PATH=./data/relay |
| 16 | 16 | ||
| 17 | # Database backend (memory, nostrdb, lmdb) | ||
| 18 | # - memory: In-memory database (default, fastest, no persistence) | ||
| 19 | # - nostrdb: NostrDB backend (persistent, optimized for Nostr) [Not yet implemented] | ||
| 20 | # - lmdb: LMDB backend (persistent, general purpose) [Not yet implemented] | ||
| 21 | NGIT_DATABASE_BACKEND=memory | ||
| 22 | |||
| 17 | # Server configuration | 23 | # Server configuration |
| 18 | NGIT_BIND_ADDRESS=127.0.0.1:8080 | 24 | NGIT_BIND_ADDRESS=127.0.0.1:8080 |
| 19 | 25 | ||
diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index fc7bbe0..e2ec9aa 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md | |||
| @@ -224,6 +224,47 @@ NGIT_RELAY_DATA_PATH=/mnt/ssd/relay-data | |||
| 224 | 224 | ||
| 225 | --- | 225 | --- |
| 226 | 226 | ||
| 227 | #### `NGIT_DATABASE_BACKEND` | ||
| 228 | |||
| 229 | **Description:** Database backend type for storing Nostr events | ||
| 230 | **Type:** String (enum: memory, nostrdb, lmdb) | ||
| 231 | **Default:** `memory` | ||
| 232 | **Required:** No | ||
| 233 | |||
| 234 | **Valid Values:** | ||
| 235 | - `memory` - In-memory database (default, fastest, no persistence) | ||
| 236 | - `nostrdb` - NostrDB backend (persistent, optimized for Nostr) [Not yet implemented] | ||
| 237 | - `lmdb` - LMDB backend (persistent, general purpose) [Not yet implemented] | ||
| 238 | |||
| 239 | **Examples:** | ||
| 240 | ```bash | ||
| 241 | # Development (default, no persistence) | ||
| 242 | NGIT_DATABASE_BACKEND=memory | ||
| 243 | |||
| 244 | # Production with NostrDB (when implemented) | ||
| 245 | NGIT_DATABASE_BACKEND=nostrdb | ||
| 246 | |||
| 247 | # Production with LMDB (when implemented) | ||
| 248 | NGIT_DATABASE_BACKEND=lmdb | ||
| 249 | ``` | ||
| 250 | |||
| 251 | **Comparison:** | ||
| 252 | |||
| 253 | | Backend | Persistence | Performance | Use Case | | ||
| 254 | |---------|-------------|-------------|----------| | ||
| 255 | | memory | No | Fastest | Development, testing | | ||
| 256 | | nostrdb | Yes | High | Production (Nostr-optimized) | | ||
| 257 | | lmdb | Yes | High | Production (general purpose) | | ||
| 258 | |||
| 259 | **Notes:** | ||
| 260 | - `memory` backend loses all data on restart | ||
| 261 | - NostrDB and LMDB backends will use `NGIT_RELAY_DATA_PATH` for storage | ||
| 262 | - NostrDB and LMDB are planned features, not yet available | ||
| 263 | - Default `memory` backend suitable for development and testing only | ||
| 264 | - Production deployments should use persistent backends when available | ||
| 265 | |||
| 266 | --- | ||
| 267 | |||
| 227 | ### Logging Configuration | 268 | ### Logging Configuration |
| 228 | 269 | ||
| 229 | #### `RUST_LOG` | 270 | #### `RUST_LOG` |
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}; | |||
| 2 | use serde::{Deserialize, Serialize}; | 2 | use serde::{Deserialize, Serialize}; |
| 3 | use std::env; | 3 | use std::env; |
| 4 | 4 | ||
| 5 | /// Database backend type for the relay | ||
| 6 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] | ||
| 7 | #[serde(rename_all = "lowercase")] | ||
| 8 | pub 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 | |||
| 17 | impl Default for DatabaseBackend { | ||
| 18 | fn default() -> Self { | ||
| 19 | Self::Memory | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | impl 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)] |
| 6 | pub struct Config { | 40 | pub 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 | ||
| 16 | impl Config { | 51 | impl 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}; | |||
| 11 | use nostr::{EventId, Filter, Kind, PublicKey}; | 11 | use nostr::{EventId, Filter, Kind, PublicKey}; |
| 12 | use nostr_relay_builder::prelude::*; | 12 | use nostr_relay_builder::prelude::*; |
| 13 | 13 | ||
| 14 | use crate::config::Config; | 14 | use crate::config::{Config, DatabaseBackend}; |
| 15 | use crate::nostr::events::{ | 15 | use 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 |