samjeston / openhub Public
Sam Jeston Working vertical 9aa3c45 · History
57 lines· 1.5 KB ·Rust
Raw
1mod config;
2mod db;
3mod git;
4mod highlight;
5mod icons;
6mod markdown;
7mod ssh;
8mod state;
9mod sync;
10mod validate;
11mod web;
12
13use anyhow::Context;
14use clap::Parser;
15use tracing_subscriber::EnvFilter;
16
17use crate::config::Config;
18use crate::state::AppState;
19
20#[tokio::main]
21async 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.
48fn 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}