OXYGENHUB
# de505df5cf8d oxygenhub / crates / hub / src / lib.rs
3.0 KiB · Rust · last changed Milestone 0.2: accounts and access 1 month ago source blame history raw
d23464c0 Andy McCall ↶ 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;
19bb31ff Andy McCall ↶ 7
pub mod auth;
d23464c0 Andy McCall ↶ 8
pub mod config;
9
pub mod db;
10
pub mod keys;
11
pub mod protocol;
12
pub mod render;
13
pub mod repos;
14
pub mod web;
15
16
use crate::config::Config;
17
use crate::repos::Repos;
18
use std::sync::Arc;
19
20
/// Everything a request handler can reach.
21
pub struct App {
22
    pub config: Config,
23
    pub db: db::Pool,
24
    pub repos: Arc<Repos>,
25
    pub server: Arc<oxy_server::Server>,
26
    pub hub_key: keys::HubKey,
19bb31ff Andy McCall ↶ 27
    pub cookie_key: auth::CookieKey,
d23464c0 Andy McCall ↶ 28
    pub started: std::time::Instant,
29
}
30
31
pub type Shared = Arc<App>;
32
33
/// Open the data directory and build the application state. `init` must
34
/// have run once; `serve` and the tests call this.
35
pub async fn build(config: Config) -> anyhow::Result<Shared> {
36
    std::fs::create_dir_all(config.repos_dir())?;
37
    let db = db::open(&config.db_path()).await?;
38
    let repos = Arc::new(Repos::new(config.repos_dir()));
39
    let hub_key = keys::ensure_identity(&config.keys_dir(), &config.site.name, config.base_url())?;
19bb31ff Andy McCall ↶ 40
    let cookie_key = auth::CookieKey::load_or_create(&config.keys_dir())?;
d23464c0 Andy McCall ↶ 41
    let backend = protocol::HubBackend::new(repos.clone(), db.clone(), config.auth.push_tokens.clone(), tokio::runtime::Handle::current());
42
    let server = oxy_server::Server::new(backend);
19bb31ff Andy McCall ↶ 43
    Ok(Arc::new(App { config, db, repos, server, hub_key, cookie_key, started: std::time::Instant::now() }))
d23464c0 Andy McCall ↶ 44
}
45
46
/// The full router: pages, API, protocol and static assets.
47
pub fn router(app: Shared) -> axum::Router {
48
    web::router(app)
49
}
50
51
/// Serve on the configured addresses until the process is told to stop.
52
pub async fn serve(app: Shared) -> anyhow::Result<()> {
53
    let router = router(app.clone());
54
    let http = app.config.bind_addr()?;
55
    let https = app.config.tls_bind_addr()?;
56
    if http.is_none() && https.is_none() {
57
        anyhow::bail!("nothing to bind: set server.bind or server.tls_bind");
58
    }
59
    let mut tasks = tokio::task::JoinSet::new();
60
    if let Some(addr) = http {
61
        let listener = tokio::net::TcpListener::bind(addr).await?;
62
        tracing::info!("http on {addr}");
63
        let r = router.clone();
64
        tasks.spawn(async move { axum::serve(listener, r).await.map_err(anyhow::Error::from) });
65
    }
66
    if let Some(addr) = https {
67
        let _ = rustls::crypto::ring::default_provider().install_default();
68
        let tls = axum_server::tls_rustls::RustlsConfig::from_pem_file(app.config.tls_cert(), app.config.tls_key())
69
            .await
70
            .map_err(|e| anyhow::anyhow!("loading TLS files: {e}"))?;
71
        tracing::info!("https on {addr}");
72
        let r = router.clone();
73
        tasks.spawn(async move { axum_server::bind_rustls(addr, tls).serve(r.into_make_service()).await.map_err(anyhow::Error::from) });
74
    }
75
    tokio::select! {
76
        r = tasks.join_next() => { r.transpose()?.transpose()?; Ok(()) }
77
        _ = tokio::signal::ctrl_c() => { tracing::info!("stopping"); Ok(()) }
78
    }
79
}