Sam Jeston
History
57 lines·
1.5 KB
·Rust
| 1 | mod config; |
| 2 | mod db; |
| 3 | mod git; |
| 4 | mod highlight; |
| 5 | mod icons; |
| 6 | mod markdown; |
| 7 | mod ssh; |
| 8 | mod state; |
| 9 | mod sync; |
| 10 | mod validate; |
| 11 | mod web; |
| 12 | |
| 13 | use anyhow::Context; |
| 14 | use clap::Parser; |
| 15 | use tracing_subscriber::EnvFilter; |
| 16 | |
| 17 | use crate::config::Config; |
| 18 | use crate::state::AppState; |
| 19 | |
| 20 | #[tokio::main] |
| 21 | async fn main() -> anyhow::Result<()> { |
| 22 | tracing_subscriber::fmt() |
| 23 | .with_env_filter( |
| 24 | EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("openhub=info")), |
| 25 | ) |
| 26 | .init(); |
| 27 | |
| 28 | let mut config = Config::parse(); |
| 29 | config.normalize(); |
| 30 | std::fs::create_dir_all(config.repos_dir()) |
| 31 | .with_context(|| format!("creating data directory {}", config.data_dir.display()))?; |
| 32 | check_git(&config.git_bin)?; |
| 33 | |
| 34 | let db = db::connect(&config.db_path()).await?; |
| 35 | let state = AppState::new(config, db); |
| 36 | |
| 37 | tokio::select! { |
| 38 | res = web::serve(state.clone()) => res.context("web server stopped"), |
| 39 | res = ssh::serve(state.clone()) => res.context("SSH server stopped"), |
| 40 | _ = tokio::signal::ctrl_c() => { |
| 41 | tracing::info!("shutting down"); |
| 42 | Ok(()) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Fetches and pushes are served by the system git, so make sure it exists. |
| 48 | fn check_git(git_bin: &str) -> anyhow::Result<()> { |
| 49 | let output = std::process::Command::new(git_bin) |
| 50 | .arg("--version") |
| 51 | .output() |
| 52 | .with_context(|| { |
| 53 | format!("could not run `{git_bin}`; Openhub requires git to be installed") |
| 54 | })?; |
| 55 | tracing::info!("using {}", String::from_utf8_lossy(&output.stdout).trim()); |
| 56 | Ok(()) |
| 57 | } |