| 1 | use std::io::{Write as _, stdout}; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | use anyhow::Context as _; |
| 5 | use clap::{Parser, Subcommand}; |
| 6 | use tokio::signal::ctrl_c; |
| 7 | #[cfg(unix)] |
| 8 | use tokio::signal::unix::{SignalKind, signal}; |
| 9 | use vineyard::config::Config; |
| 10 | use vineyard::server::spawn; |
| 11 | |
| 12 | #[derive(Parser)] |
| 13 | #[command(version, about = "a persistent nostr relay for git forges")] |
| 14 | struct Cli { |
| 15 | #[command(subcommand)] |
| 16 | command: Command, |
| 17 | } |
| 18 | |
| 19 | #[derive(Subcommand)] |
| 20 | enum Command { |
| 21 | Serve { |
| 22 | #[arg(long, default_value = "vineyard.toml")] |
| 23 | config: PathBuf, |
| 24 | }, |
| 25 | CheckConfig { |
| 26 | #[arg(long, default_value = "vineyard.toml")] |
| 27 | config: PathBuf, |
| 28 | }, |
| 29 | } |
| 30 | |
| 31 | #[tokio::main(worker_threads = 2)] |
| 32 | async fn main() -> anyhow::Result<()> { |
| 33 | tracing_subscriber::fmt() |
| 34 | .with_env_filter( |
| 35 | tracing_subscriber::EnvFilter::try_from_default_env() |
| 36 | .unwrap_or_else(|_| "vineyard=info".into()), |
| 37 | ) |
| 38 | .init(); |
| 39 | match Cli::parse().command { |
| 40 | Command::CheckConfig { config } => { |
| 41 | Config::load(&config)?; |
| 42 | writeln!(stdout().lock(), "configuration is valid")?; |
| 43 | } |
| 44 | Command::Serve { config } => { |
| 45 | let relay = spawn(Config::load(&config)?).await?; |
| 46 | #[cfg(unix)] |
| 47 | { |
| 48 | let mut terminate = |
| 49 | signal(SignalKind::terminate()).context("installing termination handler")?; |
| 50 | tokio::select! { |
| 51 | result = ctrl_c() => result?, |
| 52 | _ = terminate.recv() => {}, |
| 53 | } |
| 54 | } |
| 55 | #[cfg(not(unix))] |
| 56 | ctrl_c().await?; |
| 57 | relay.shutdown().await?; |
| 58 | } |
| 59 | } |
| 60 | Ok(()) |
| 61 | } |