| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
pub mod api; |
| 7 |
pub mod apt; |
| 8 |
pub mod assistants; |
| 9 |
pub mod auth; |
| 10 |
pub mod config; |
| 11 |
pub mod db; |
| 12 |
pub mod events; |
| 13 |
pub mod flow; |
| 14 |
pub mod hooks; |
| 15 |
pub mod keys; |
| 16 |
pub mod levels; |
| 17 |
pub mod live; |
| 18 |
pub mod mail; |
| 19 |
pub mod maintenance; |
| 20 |
pub mod mirror; |
| 21 |
pub mod notify; |
| 22 |
pub mod oidc; |
| 23 |
pub mod protocol; |
| 24 |
pub mod provenance; |
| 25 |
pub mod render; |
| 26 |
pub mod repos; |
| 27 |
pub mod secrets; |
| 28 |
pub mod variables; |
| 29 |
pub mod web; |
| 30 |
|
| 31 |
use crate::config::Config; |
| 32 |
use crate::repos::Repos; |
| 33 |
use std::sync::Arc; |
| 34 |
|
| 35 |
|
| 36 |
pub struct App { |
| 37 |
pub config: Config, |
| 38 |
pub db: db::Pool, |
| 39 |
pub repos: Arc<Repos>, |
| 40 |
pub server: Arc<oxy_server::Server>, |
| 41 |
pub hub_key: keys::HubKey, |
| 42 |
pub cookie_key: auth::CookieKey, |
| 43 |
|
| 44 |
pub assistant_key: assistants::SecretKey, |
| 45 |
|
| 46 |
pub secret_key: assistants::SecretKey, |
| 47 |
|
| 48 |
pub oidc: tokio::sync::OnceCell<oidc::Issuer>, |
| 49 |
|
| 50 |
|
| 51 |
pub assistant_gate: std::sync::Mutex<assistants::Gate>, |
| 52 |
pub events: events::Events, |
| 53 |
|
| 54 |
pub rooms: Arc<live::Rooms>, |
| 55 |
|
| 56 |
|
| 57 |
pub mail: std::sync::RwLock<config::Email>, |
| 58 |
pub started: std::time::Instant, |
| 59 |
} |
| 60 |
|
| 61 |
impl App { |
| 62 |
|
| 63 |
pub async fn oidc(&self) -> anyhow::Result<&oidc::Issuer> { |
| 64 |
let keys = self.config.keys_dir(); |
| 65 |
self.oidc.get_or_try_init(|| async move { tokio::task::spawn_blocking(move || oidc::Issuer::load_or_create(&keys)).await? }).await |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
pub type Shared = Arc<App>; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub async fn build(config: Config) -> anyhow::Result<Shared> { |
| 74 |
std::fs::create_dir_all(config.repos_dir())?; |
| 75 |
let db = db::open(&config.db_path()).await?; |
| 76 |
let repos = Arc::new(Repos::new(config.repos_dir())); |
| 77 |
let hub_key = keys::ensure_identity(&config.keys_dir(), &config.site.name, config.base_url())?; |
| 78 |
let cookie_key = auth::CookieKey::load_or_create(&config.keys_dir())?; |
| 79 |
let assistant_key = assistants::SecretKey::load_or_create(&config.keys_dir())?; |
| 80 |
let secret_key = assistants::SecretKey::load_or_create_named(&config.keys_dir(), "secrets.key")?; |
| 81 |
let backend = protocol::HubBackend::new(repos.clone(), db.clone(), config.auth.push_tokens.clone(), tokio::runtime::Handle::current()); |
| 82 |
let slot = backend.app_slot(); |
| 83 |
let server = oxy_server::Server::new(backend); |
| 84 |
let mail = mail::load(&db, &config.email).await; |
| 85 |
let app = Arc::new(App { |
| 86 |
mail: std::sync::RwLock::new(mail), |
| 87 |
config, |
| 88 |
db, |
| 89 |
repos, |
| 90 |
server, |
| 91 |
hub_key, |
| 92 |
cookie_key, |
| 93 |
assistant_key, |
| 94 |
secret_key, |
| 95 |
oidc: tokio::sync::OnceCell::new(), |
| 96 |
assistant_gate: std::sync::Mutex::new(assistants::Gate::default()), |
| 97 |
events: events::Events::default(), |
| 98 |
rooms: Arc::new(live::Rooms::default()), |
| 99 |
started: std::time::Instant::now(), |
| 100 |
}); |
| 101 |
*slot.lock().unwrap() = Some(Arc::downgrade(&app)); |
| 102 |
tokio::spawn(hooks::worker(app.clone())); |
| 103 |
tokio::spawn(flow::reaper(app.clone())); |
| 104 |
tokio::spawn(flow::scheduler(app.clone())); |
| 105 |
tokio::spawn(maintenance::nightly(app.clone())); |
| 106 |
Ok(app) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
pub fn router(app: Shared) -> axum::Router { |
| 111 |
web::router(app) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
pub async fn serve(app: Shared) -> anyhow::Result<()> { |
| 116 |
let router = router(app.clone()); |
| 117 |
let http = app.config.bind_addr()?; |
| 118 |
let https = app.config.tls_bind_addr()?; |
| 119 |
if http.is_none() && https.is_none() { |
| 120 |
anyhow::bail!("nothing to bind: set server.bind or server.tls_bind"); |
| 121 |
} |
| 122 |
let mut tasks = tokio::task::JoinSet::new(); |
| 123 |
if let Some(addr) = http { |
| 124 |
let listener = tokio::net::TcpListener::bind(addr).await?; |
| 125 |
tracing::info!("http on {addr}"); |
| 126 |
let r = router.clone(); |
| 127 |
tasks.spawn(async move { |
| 128 |
axum::serve(listener, r.into_make_service_with_connect_info::<std::net::SocketAddr>()).await.map_err(anyhow::Error::from) |
| 129 |
}); |
| 130 |
} |
| 131 |
if let Some(addr) = https { |
| 132 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 133 |
let tls = axum_server::tls_rustls::RustlsConfig::from_pem_file(app.config.tls_cert(), app.config.tls_key()) |
| 134 |
.await |
| 135 |
.map_err(|e| anyhow::anyhow!("loading TLS files: {e}"))?; |
| 136 |
tracing::info!("https on {addr}"); |
| 137 |
let r = router.clone(); |
| 138 |
tasks.spawn(async move { |
| 139 |
axum_server::bind_rustls(addr, tls) |
| 140 |
.serve(r.into_make_service_with_connect_info::<std::net::SocketAddr>()) |
| 141 |
.await |
| 142 |
.map_err(anyhow::Error::from) |
| 143 |
}); |
| 144 |
} |
| 145 |
tokio::select! { |
| 146 |
r = tasks.join_next() => { r.transpose()?.transpose()?; Ok(()) } |
| 147 |
_ = tokio::signal::ctrl_c() => { tracing::info!("stopping"); Ok(()) } |
| 148 |
} |
| 149 |
} |