OXYGENHUB
# 0b203bbf7cc3 oxygenhub / crates / hub / src / lib.rs
5.9 KiB · Rust · last changed Test reports from a job's junit artifact (part 1) 1 month ago source blame history raw
1
//! OxygenHub: a forge for Oxygen repositories (`docs/spec.md`).
2
//!
3
//! One process: an axum router serving pages, the JSON API and the wire
4
//! protocol; SQLite for hub state; repositories as bare Oxygen stores.
5
6
pub mod api;
7
pub mod apt;
8
pub mod assistants;
9
pub mod auth;
10
pub mod config;
11
pub mod coverage;
12
pub mod db;
13
pub mod events;
14
pub mod flow;
15
pub mod hooks;
16
pub mod junit;
17
pub mod keys;
18
pub mod levels;
19
pub mod live;
20
pub mod mail;
21
pub mod maintenance;
22
pub mod mirror;
23
pub mod notify;
24
pub mod oidc;
25
pub mod protocol;
26
pub mod render;
27
pub mod repos;
28
pub mod secrets;
29
pub mod signin;
30
pub mod social;
31
pub mod storage;
32
pub mod variables;
33
pub mod web;
34
pub mod wiki;
35
36
use crate::config::Config;
37
use crate::repos::Repos;
38
use std::sync::Arc;
39
40
/// Everything a request handler can reach.
41
pub struct App {
42
    pub config: Config,
43
    pub db: db::Pool,
44
    pub repos: Arc<Repos>,
45
    /// Wikis, one Oxygen repository per repository (hub spec §6.11).
46
    pub wikis: Arc<Repos>,
47
    pub server: Arc<oxy_server::Server>,
48
    pub hub_key: keys::HubKey,
49
    pub cookie_key: auth::CookieKey,
50
    /// Seals AI assistants' tokens at rest (hub spec §13.4).
51
    pub assistant_key: assistants::SecretKey,
52
    /// Seals Flow secrets at rest (hub spec §13.2, #107).
53
    pub secret_key: assistants::SecretKey,
54
    /// The OIDC issuer for jobs (hub spec §13.5), made on first use.
55
    pub oidc: tokio::sync::OnceCell<oidc::Issuer>,
56
    /// Which assistants are answering which proposals, and recent calls
57
    /// per repository (hub spec §13.4).
58
    pub assistant_gate: std::sync::Mutex<assistants::Gate>,
59
    pub events: events::Events,
60
    /// Who is resolving which conflict together (hub spec §6.10).
61
    pub rooms: Arc<live::Rooms>,
62
    /// Outgoing mail: hub.toml's `[email]`, overridden by what the admin
63
    /// page saved (#101).
64
    pub mail: std::sync::RwLock<config::Email>,
65
    pub started: std::time::Instant,
66
}
67
68
impl App {
69
    /// The OIDC issuer, its key loaded or made the first time it is asked for.
70
    pub async fn oidc(&self) -> anyhow::Result<&oidc::Issuer> {
71
        let keys = self.config.keys_dir();
72
        self.oidc.get_or_try_init(|| async move { tokio::task::spawn_blocking(move || oidc::Issuer::load_or_create(&keys)).await? }).await
73
    }
74
}
75
76
pub type Shared = Arc<App>;
77
78
/// Open the data directory and build the application state. `init` must
79
/// have run once; `serve` and the tests call this.
80
pub async fn build(config: Config) -> anyhow::Result<Shared> {
81
    std::fs::create_dir_all(config.repos_dir())?;
82
    let db = db::open(&config.db_path()).await?;
83
    let repos = Arc::new(Repos::new(config.repos_dir()));
84
    let wikis = Arc::new(Repos::new(config.data.join("wikis")));
85
    let hub_key = keys::ensure_identity(&config.keys_dir(), &config.site.name, config.base_url())?;
86
    let cookie_key = auth::CookieKey::load_or_create(&config.keys_dir())?;
87
    let assistant_key = assistants::SecretKey::load_or_create(&config.keys_dir())?;
88
    let secret_key = assistants::SecretKey::load_or_create_named(&config.keys_dir(), "secrets.key")?;
89
    let backend = protocol::HubBackend::new(
90
        repos.clone(),
91
        wikis.clone(),
92
        db.clone(),
93
        config.auth.push_tokens.clone(),
94
        tokio::runtime::Handle::current(),
95
    );
96
    let slot = backend.app_slot();
97
    let server = oxy_server::Server::new(backend);
98
    let mail = mail::load(&db, &config.email).await;
99
    let app = Arc::new(App {
100
        mail: std::sync::RwLock::new(mail),
101
        config,
102
        db,
103
        repos,
104
        wikis,
105
        server,
106
        hub_key,
107
        cookie_key,
108
        assistant_key,
109
        secret_key,
110
        oidc: tokio::sync::OnceCell::new(),
111
        assistant_gate: std::sync::Mutex::new(assistants::Gate::default()),
112
        events: events::Events::default(),
113
        rooms: Arc::new(live::Rooms::default()),
114
        started: std::time::Instant::now(),
115
    });
116
    *slot.lock().unwrap() = Some(Arc::downgrade(&app));
117
    storage::load(&app).await?;
118
    coverage::load(&app).await?;
119
    tokio::spawn(hooks::worker(app.clone()));
120
    tokio::spawn(flow::reaper(app.clone()));
121
    tokio::spawn(flow::scheduler(app.clone()));
122
    tokio::spawn(flow::placement(app.clone()));
123
    tokio::spawn(maintenance::nightly(app.clone()));
124
    Ok(app)
125
}
126
127
/// The full router: pages, API, protocol and static assets.
128
pub fn router(app: Shared) -> axum::Router {
129
    web::router(app)
130
}
131
132
/// Serve on the configured addresses until the process is told to stop.
133
pub async fn serve(app: Shared) -> anyhow::Result<()> {
134
    let router = router(app.clone());
135
    let http = app.config.bind_addr()?;
136
    let https = app.config.tls_bind_addr()?;
137
    if http.is_none() && https.is_none() {
138
        anyhow::bail!("nothing to bind: set server.bind or server.tls_bind");
139
    }
140
    let mut tasks = tokio::task::JoinSet::new();
141
    if let Some(addr) = http {
142
        let listener = tokio::net::TcpListener::bind(addr).await?;
143
        tracing::info!("http on {addr}");
144
        let r = router.clone();
145
        tasks.spawn(async move {
146
            axum::serve(listener, r.into_make_service_with_connect_info::<std::net::SocketAddr>()).await.map_err(anyhow::Error::from)
147
        });
148
    }
149
    if let Some(addr) = https {
150
        let _ = rustls::crypto::ring::default_provider().install_default();
151
        let tls = axum_server::tls_rustls::RustlsConfig::from_pem_file(app.config.tls_cert(), app.config.tls_key())
152
            .await
153
            .map_err(|e| anyhow::anyhow!("loading TLS files: {e}"))?;
154
        tracing::info!("https on {addr}");
155
        let r = router.clone();
156
        tasks.spawn(async move {
157
            axum_server::bind_rustls(addr, tls)
158
                .serve(r.into_make_service_with_connect_info::<std::net::SocketAddr>())
159
                .await
160
                .map_err(anyhow::Error::from)
161
        });
162
    }
163
    tokio::select! {
164
        r = tasks.join_next() => { r.transpose()?.transpose()?; Ok(()) }
165
        _ = tokio::signal::ctrl_c() => { tracing::info!("stopping"); Ok(()) }
166
    }
167
}