diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 75 |
1 files changed, 52 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs index 6769cf3..bf3aefb 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -3,8 +3,8 @@ use std::{path::PathBuf, sync::Arc}; | |||
| 3 | 3 | ||
| 4 | use anyhow::Result; | 4 | use anyhow::Result; |
| 5 | use tokio::signal; | 5 | use tokio::signal; |
| 6 | use tracing::{error, info, warn, Level}; | 6 | use tracing::{error, info, warn}; |
| 7 | use tracing_subscriber::FmtSubscriber; | 7 | use tracing_subscriber::{EnvFilter, FmtSubscriber}; |
| 8 | 8 | ||
| 9 | use ngit_grasp::{ | 9 | use ngit_grasp::{ |
| 10 | config::{Config, DatabaseBackend}, | 10 | config::{Config, DatabaseBackend}, |
| @@ -17,16 +17,16 @@ use ngit_grasp::{ | |||
| 17 | 17 | ||
| 18 | #[tokio::main] | 18 | #[tokio::main] |
| 19 | async fn main() -> Result<()> { | 19 | async fn main() -> Result<()> { |
| 20 | // Initialize tracing | 20 | // Load configuration first (priority: CLI flags > env vars > .env file > defaults) |
| 21 | let config = Config::load()?; | ||
| 22 | |||
| 23 | // Initialize tracing with configured log level | ||
| 21 | let subscriber = FmtSubscriber::builder() | 24 | let subscriber = FmtSubscriber::builder() |
| 22 | .with_max_level(Level::DEBUG) | 25 | .with_env_filter(EnvFilter::new(&config.log_level)) |
| 23 | .finish(); | 26 | .finish(); |
| 24 | tracing::subscriber::set_global_default(subscriber)?; | 27 | tracing::subscriber::set_global_default(subscriber)?; |
| 25 | 28 | ||
| 26 | info!("Starting ngit-grasp with nostr-relay-builder..."); | 29 | info!("Starting ngit-grasp with log level: {}", config.log_level); |
| 27 | |||
| 28 | // Load configuration (priority: CLI flags > env vars > .env file > defaults) | ||
| 29 | let config = Config::load()?; | ||
| 30 | 30 | ||
| 31 | // Validate configuration and fail fast on fatal errors | 31 | // Validate configuration and fail fast on fatal errors |
| 32 | // Recoverable issues (e.g., malformed whitelist entries) are logged as warnings | 32 | // Recoverable issues (e.g., malformed whitelist entries) are logged as warnings |
| @@ -189,8 +189,8 @@ async fn main() -> Result<()> { | |||
| 189 | )); | 189 | )); |
| 190 | 190 | ||
| 191 | // Create throttle manager for rate limiting remote git servers | 191 | // Create throttle manager for rate limiting remote git servers |
| 192 | // Default: 5 concurrent requests per domain, 30 requests per minute per domain | 192 | // Default: 5 concurrent requests per domain, 60 requests per minute per domain |
| 193 | let throttle_manager = Arc::new(ThrottleManager::new(5, 30)); | 193 | let throttle_manager = Arc::new(ThrottleManager::new(5, 60)); |
| 194 | throttle_manager.set_context(sync_ctx.clone()); | 194 | throttle_manager.set_context(sync_ctx.clone()); |
| 195 | throttle_manager.set_git_naughty_list(git_naughty_list.clone()); | 195 | throttle_manager.set_git_naughty_list(git_naughty_list.clone()); |
| 196 | 196 | ||
| @@ -212,20 +212,49 @@ async fn main() -> Result<()> { | |||
| 212 | let http_write_policy = Arc::new(relay_with_db.write_policy.clone()); | 212 | let http_write_policy = Arc::new(relay_with_db.write_policy.clone()); |
| 213 | 213 | ||
| 214 | // Run server until shutdown signal, then cleanup | 214 | // Run server until shutdown signal, then cleanup |
| 215 | tokio::select! { | 215 | #[cfg(unix)] |
| 216 | result = http::run_server( | 216 | { |
| 217 | config, | 217 | use tokio::signal::unix::{signal, SignalKind}; |
| 218 | relay_with_db.relay, | 218 | let mut sigterm = signal(SignalKind::terminate())?; |
| 219 | relay_with_db.database, | 219 | |
| 220 | metrics, | 220 | tokio::select! { |
| 221 | purgatory, | 221 | result = http::run_server( |
| 222 | http_write_policy, | 222 | config, |
| 223 | http_rejected_index, | 223 | relay_with_db.relay, |
| 224 | ) => { | 224 | relay_with_db.database, |
| 225 | result? | 225 | metrics, |
| 226 | purgatory, | ||
| 227 | http_write_policy, | ||
| 228 | http_rejected_index, | ||
| 229 | ) => { | ||
| 230 | result? | ||
| 231 | } | ||
| 232 | _ = signal::ctrl_c() => { | ||
| 233 | info!("Received SIGINT (Ctrl+C), cleaning up..."); | ||
| 234 | } | ||
| 235 | _ = sigterm.recv() => { | ||
| 236 | info!("Received SIGTERM, cleaning up..."); | ||
| 237 | } | ||
| 226 | } | 238 | } |
| 227 | _ = signal::ctrl_c() => { | 239 | } |
| 228 | info!("Received shutdown signal, cleaning up..."); | 240 | |
| 241 | #[cfg(not(unix))] | ||
| 242 | { | ||
| 243 | tokio::select! { | ||
| 244 | result = http::run_server( | ||
| 245 | config, | ||
| 246 | relay_with_db.relay, | ||
| 247 | relay_with_db.database, | ||
| 248 | metrics, | ||
| 249 | purgatory, | ||
| 250 | http_write_policy, | ||
| 251 | http_rejected_index, | ||
| 252 | ) => { | ||
| 253 | result? | ||
| 254 | } | ||
| 255 | _ = signal::ctrl_c() => { | ||
| 256 | info!("Received SIGINT (Ctrl+C), cleaning up..."); | ||
| 257 | } | ||
| 229 | } | 258 | } |
| 230 | } | 259 | } |
| 231 | 260 | ||