| 8d2caf12 Andy McCall ↶ |
1 |
|
| d23464c0 Andy McCall ↶ |
2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
pub mod api; |
| ea85398a Andy McCall ↶ |
7 |
pub mod apt; |
| 19bb31ff Andy McCall ↶ |
8 |
pub mod auth; |
| d23464c0 Andy McCall ↶ |
9 |
pub mod config; |
| 10 |
pub mod db; |
| b64e2537 Andy McCall ↶ |
11 |
pub mod events; |
| 42fa9554 Andy McCall ↶ |
12 |
pub mod flow; |
| b64e2537 Andy McCall ↶ |
13 |
pub mod hooks; |
| d23464c0 Andy McCall ↶ |
14 |
pub mod keys; |
| 5e6d6484 Andy McCall ↶ |
15 |
pub mod live; |
| 34beb1a7 Andy McCall ↶ |
16 |
pub mod mail; |
| 8a3334b7 Andy McCall ↶ |
17 |
pub mod maintenance; |
| 3f4b0ea6 Andy McCall ↶ |
18 |
pub mod mirror; |
| 18e381ea Andy McCall ↶ |
19 |
pub mod notify; |
| d23464c0 Andy McCall ↶ |
20 |
pub mod protocol; |
| 21 |
pub mod render; |
| 22 |
pub mod repos; |
| 23 |
pub mod web; |
| 24 |
|
| 25 |
use crate::config::Config; |
| 26 |
use crate::repos::Repos; |
| 27 |
use std::sync::Arc; |
| 28 |
|
| 29 |
|
| 30 |
pub struct App { |
| 31 |
pub config: Config, |
| 32 |
pub db: db::Pool, |
| 33 |
pub repos: Arc<Repos>, |
| 34 |
pub server: Arc<oxy_server::Server>, |
| 35 |
pub hub_key: keys::HubKey, |
| 19bb31ff Andy McCall ↶ |
36 |
pub cookie_key: auth::CookieKey, |
| b64e2537 Andy McCall ↶ |
37 |
pub events: events::Events, |
| 5e6d6484 Andy McCall ↶ |
38 |
|
| 39 |
pub rooms: Arc<live::Rooms>, |
| d23464c0 Andy McCall ↶ |
40 |
pub started: std::time::Instant, |
| 41 |
} |
| 42 |
|
| 43 |
pub type Shared = Arc<App>; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub async fn build(config: Config) -> anyhow::Result<Shared> { |
| 48 |
std::fs::create_dir_all(config.repos_dir())?; |
| 49 |
let db = db::open(&config.db_path()).await?; |
| 50 |
let repos = Arc::new(Repos::new(config.repos_dir())); |
| 51 |
let hub_key = keys::ensure_identity(&config.keys_dir(), &config.site.name, config.base_url())?; |
| 19bb31ff Andy McCall ↶ |
52 |
let cookie_key = auth::CookieKey::load_or_create(&config.keys_dir())?; |
| d23464c0 Andy McCall ↶ |
53 |
let backend = protocol::HubBackend::new(repos.clone(), db.clone(), config.auth.push_tokens.clone(), tokio::runtime::Handle::current()); |
| 18e381ea Andy McCall ↶ |
54 |
let slot = backend.app_slot(); |
| d23464c0 Andy McCall ↶ |
55 |
let server = oxy_server::Server::new(backend); |
| b64e2537 Andy McCall ↶ |
56 |
let app = Arc::new(App { |
| 57 |
config, |
| 58 |
db, |
| 59 |
repos, |
| 60 |
server, |
| 61 |
hub_key, |
| 62 |
cookie_key, |
| 63 |
events: events::Events::default(), |
| 5e6d6484 Andy McCall ↶ |
64 |
rooms: Arc::new(live::Rooms::default()), |
| b64e2537 Andy McCall ↶ |
65 |
started: std::time::Instant::now(), |
| 66 |
}); |
| 18e381ea Andy McCall ↶ |
67 |
*slot.lock().unwrap() = Some(Arc::downgrade(&app)); |
| b64e2537 Andy McCall ↶ |
68 |
tokio::spawn(hooks::worker(app.clone())); |
| 070a7b64 Andy McCall ↶ |
69 |
tokio::spawn(flow::reaper(app.clone())); |
| 348f8c4b Andy McCall ↶ |
70 |
tokio::spawn(flow::scheduler(app.clone())); |
| 8a3334b7 Andy McCall ↶ |
71 |
tokio::spawn(maintenance::nightly(app.clone())); |
| 18e381ea Andy McCall ↶ |
72 |
Ok(app) |
| d23464c0 Andy McCall ↶ |
73 |
} |
| 74 |
|
| 75 |
|
| 76 |
pub fn router(app: Shared) -> axum::Router { |
| 77 |
web::router(app) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
pub async fn serve(app: Shared) -> anyhow::Result<()> { |
| 82 |
let router = router(app.clone()); |
| 83 |
let http = app.config.bind_addr()?; |
| 84 |
let https = app.config.tls_bind_addr()?; |
| 85 |
if http.is_none() && https.is_none() { |
| 86 |
anyhow::bail!("nothing to bind: set server.bind or server.tls_bind"); |
| 87 |
} |
| 88 |
let mut tasks = tokio::task::JoinSet::new(); |
| 89 |
if let Some(addr) = http { |
| 90 |
let listener = tokio::net::TcpListener::bind(addr).await?; |
| 91 |
tracing::info!("http on {addr}"); |
| 92 |
let r = router.clone(); |
| 60f11b71 Andy McCall ↶ |
93 |
tasks.spawn(async move { |
| 94 |
axum::serve(listener, r.into_make_service_with_connect_info::<std::net::SocketAddr>()).await.map_err(anyhow::Error::from) |
| 95 |
}); |
| d23464c0 Andy McCall ↶ |
96 |
} |
| 97 |
if let Some(addr) = https { |
| 98 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 99 |
let tls = axum_server::tls_rustls::RustlsConfig::from_pem_file(app.config.tls_cert(), app.config.tls_key()) |
| 100 |
.await |
| 101 |
.map_err(|e| anyhow::anyhow!("loading TLS files: {e}"))?; |
| 102 |
tracing::info!("https on {addr}"); |
| 103 |
let r = router.clone(); |
| 60f11b71 Andy McCall ↶ |
104 |
tasks.spawn(async move { |
| 105 |
axum_server::bind_rustls(addr, tls) |
| 106 |
.serve(r.into_make_service_with_connect_info::<std::net::SocketAddr>()) |
| 107 |
.await |
| 108 |
.map_err(anyhow::Error::from) |
| 109 |
}); |
| d23464c0 Andy McCall ↶ |
110 |
} |
| 111 |
tokio::select! { |
| 112 |
r = tasks.join_next() => { r.transpose()?.transpose()?; Ok(()) } |
| 113 |
_ = tokio::signal::ctrl_c() => { tracing::info!("stopping"); Ok(()) } |
| 114 |
} |
| 115 |
} |