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 /src/config.rs | |
| 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.
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 42 |
1 files changed, 42 insertions, 0 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}; | |||
| 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 | } |