| 8d2caf12 Andy McCall ↶ |
1 |
|
| d23464c0 Andy McCall ↶ |
2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
pub mod api; |
| ea85398a Andy McCall ↶ |
7 |
pub mod apt; |
| e71846aa Andy McCall ↶ |
8 |
pub mod assistants; |
| 19bb31ff Andy McCall ↶ |
9 |
pub mod auth; |
| d7d18246 Andy McCall ↶ |
10 |
pub mod backups; |
| d23464c0 Andy McCall ↶ |
11 |
pub mod config; |
| 554ccacf Andy McCall ↶ |
12 |
pub mod coverage; |
| d23464c0 Andy McCall ↶ |
13 |
pub mod db; |
| f677d627 Andy McCall ↶ |
14 |
pub mod diffusion; |
| b64e2537 Andy McCall ↶ |
15 |
pub mod events; |
| 42fa9554 Andy McCall ↶ |
16 |
pub mod flow; |
| b64e2537 Andy McCall ↶ |
17 |
pub mod hooks; |
| 92a797b2 Andy McCall ↶ |
18 |
pub mod images; |
| 0b203bbf Andy McCall ↶ |
19 |
pub mod junit; |
| d23464c0 Andy McCall ↶ |
20 |
pub mod keys; |
| 2a83f530 Andy McCall ↶ |
21 |
pub mod levels; |
| 5e6d6484 Andy McCall ↶ |
22 |
pub mod live; |
| 34beb1a7 Andy McCall ↶ |
23 |
pub mod mail; |
| 8a3334b7 Andy McCall ↶ |
24 |
pub mod maintenance; |
| 3f4b0ea6 Andy McCall ↶ |
25 |
pub mod mirror; |
| 18e381ea Andy McCall ↶ |
26 |
pub mod notify; |
| d1727c57 Andy McCall ↶ |
27 |
pub mod oidc; |
| d23464c0 Andy McCall ↶ |
28 |
pub mod protocol; |
| 29 |
pub mod render; |
| a36bab6b Andy McCall ↶ |
30 |
pub mod reports; |
| d23464c0 Andy McCall ↶ |
31 |
pub mod repos; |
| 4ac01a73 Andy McCall ↶ |
32 |
pub mod secrets; |
| 83940d53 Andy McCall ↶ |
33 |
pub mod signin; |
| 89e89836 Andy McCall ↶ |
34 |
pub mod social; |
| 1f78655a Andy McCall ↶ |
35 |
pub mod storage; |
| a46d15e7 Andy McCall ↶ |
36 |
pub mod swarm; |
| 2a83f530 Andy McCall ↶ |
37 |
pub mod variables; |
| d23464c0 Andy McCall ↶ |
38 |
pub mod web; |
| 66fd9d6b Andy McCall ↶ |
39 |
pub mod wiki; |
| d23464c0 Andy McCall ↶ |
40 |
|
| 41 |
use crate::config::Config; |
| 42 |
use crate::repos::Repos; |
| 43 |
use std::sync::Arc; |
| 44 |
|
| 45 |
|
| 46 |
pub struct App { |
| 47 |
pub config: Config, |
| 48 |
pub db: db::Pool, |
| 49 |
pub repos: Arc<Repos>, |
| 66fd9d6b Andy McCall ↶ |
50 |
|
| 51 |
pub wikis: Arc<Repos>, |
| d23464c0 Andy McCall ↶ |
52 |
pub server: Arc<oxy_server::Server>, |
| 53 |
pub hub_key: keys::HubKey, |
| 19bb31ff Andy McCall ↶ |
54 |
pub cookie_key: auth::CookieKey, |
| e71846aa Andy McCall ↶ |
55 |
|
| 56 |
pub assistant_key: assistants::SecretKey, |
| 4ac01a73 Andy McCall ↶ |
57 |
|
| 58 |
pub secret_key: assistants::SecretKey, |
| d1727c57 Andy McCall ↶ |
59 |
|
| 60 |
pub oidc: tokio::sync::OnceCell<oidc::Issuer>, |
| 7d2eb590 Andy McCall ↶ |
61 |
|
| 62 |
|
| 63 |
pub assistant_gate: std::sync::Mutex<assistants::Gate>, |
| b64e2537 Andy McCall ↶ |
64 |
pub events: events::Events, |
| 5e6d6484 Andy McCall ↶ |
65 |
|
| 66 |
pub rooms: Arc<live::Rooms>, |
| 3556d08e Andy McCall ↶ |
67 |
|
| 68 |
|
| 69 |
pub mail: std::sync::RwLock<config::Email>, |
| a46d15e7 Andy McCall ↶ |
70 |
|
| 71 |
pub swarm: swarm::Tracker, |
| d23464c0 Andy McCall ↶ |
72 |
pub started: std::time::Instant, |
| 73 |
} |
| 74 |
|
| d1727c57 Andy McCall ↶ |
75 |
impl App { |
| 92a797b2 Andy McCall ↶ |
76 |
|
| 77 |
pub fn images(&self) -> images::Store { |
| 78 |
images::Store::new(&self.config.data) |
| 79 |
} |
| 80 |
|
| d1727c57 Andy McCall ↶ |
81 |
|
| 82 |
pub async fn oidc(&self) -> anyhow::Result<&oidc::Issuer> { |
| 83 |
let keys = self.config.keys_dir(); |
| 84 |
self.oidc.get_or_try_init(|| async move { tokio::task::spawn_blocking(move || oidc::Issuer::load_or_create(&keys)).await? }).await |
| 85 |
} |
| 86 |
} |
| 87 |
|
| d23464c0 Andy McCall ↶ |
88 |
pub type Shared = Arc<App>; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pub async fn build(config: Config) -> anyhow::Result<Shared> { |
| 93 |
std::fs::create_dir_all(config.repos_dir())?; |
| 94 |
let db = db::open(&config.db_path()).await?; |
| 95 |
let repos = Arc::new(Repos::new(config.repos_dir())); |
| 66fd9d6b Andy McCall ↶ |
96 |
let wikis = Arc::new(Repos::new(config.data.join("wikis"))); |
| d23464c0 Andy McCall ↶ |
97 |
let hub_key = keys::ensure_identity(&config.keys_dir(), &config.site.name, config.base_url())?; |
| 19bb31ff Andy McCall ↶ |
98 |
let cookie_key = auth::CookieKey::load_or_create(&config.keys_dir())?; |
| e71846aa Andy McCall ↶ |
99 |
let assistant_key = assistants::SecretKey::load_or_create(&config.keys_dir())?; |
| 4ac01a73 Andy McCall ↶ |
100 |
let secret_key = assistants::SecretKey::load_or_create_named(&config.keys_dir(), "secrets.key")?; |
| 66fd9d6b Andy McCall ↶ |
101 |
let backend = protocol::HubBackend::new( |
| 102 |
repos.clone(), |
| 103 |
wikis.clone(), |
| 104 |
db.clone(), |
| 105 |
config.auth.push_tokens.clone(), |
| 106 |
tokio::runtime::Handle::current(), |
| 107 |
); |
| 18e381ea Andy McCall ↶ |
108 |
let slot = backend.app_slot(); |
| d23464c0 Andy McCall ↶ |
109 |
let server = oxy_server::Server::new(backend); |
| 3556d08e Andy McCall ↶ |
110 |
let mail = mail::load(&db, &config.email).await; |
| b64e2537 Andy McCall ↶ |
111 |
let app = Arc::new(App { |
| 3556d08e Andy McCall ↶ |
112 |
mail: std::sync::RwLock::new(mail), |
| b64e2537 Andy McCall ↶ |
113 |
config, |
| 114 |
db, |
| 115 |
repos, |
| 66fd9d6b Andy McCall ↶ |
116 |
wikis, |
| b64e2537 Andy McCall ↶ |
117 |
server, |
| 118 |
hub_key, |
| 119 |
cookie_key, |
| e71846aa Andy McCall ↶ |
120 |
assistant_key, |
| 4ac01a73 Andy McCall ↶ |
121 |
secret_key, |
| d1727c57 Andy McCall ↶ |
122 |
oidc: tokio::sync::OnceCell::new(), |
| 7d2eb590 Andy McCall ↶ |
123 |
assistant_gate: std::sync::Mutex::new(assistants::Gate::default()), |
| b64e2537 Andy McCall ↶ |
124 |
events: events::Events::default(), |
| 5e6d6484 Andy McCall ↶ |
125 |
rooms: Arc::new(live::Rooms::default()), |
| a46d15e7 Andy McCall ↶ |
126 |
swarm: Default::default(), |
| b64e2537 Andy McCall ↶ |
127 |
started: std::time::Instant::now(), |
| 128 |
}); |
| 18e381ea Andy McCall ↶ |
129 |
*slot.lock().unwrap() = Some(Arc::downgrade(&app)); |
| 1f78655a Andy McCall ↶ |
130 |
storage::load(&app).await?; |
| 554ccacf Andy McCall ↶ |
131 |
coverage::load(&app).await?; |
| 36e5a9c1 Andy McCall ↶ |
132 |
junit::load(&app).await?; |
| c8df33a6 Andy McCall ↶ |
133 |
reports::load(&app).await?; |
| b64e2537 Andy McCall ↶ |
134 |
tokio::spawn(hooks::worker(app.clone())); |
| 070a7b64 Andy McCall ↶ |
135 |
tokio::spawn(flow::reaper(app.clone())); |
| 348f8c4b Andy McCall ↶ |
136 |
tokio::spawn(flow::scheduler(app.clone())); |
| 4d2e44d7 Andy McCall ↶ |
137 |
tokio::spawn(flow::placement(app.clone())); |
| 8a3334b7 Andy McCall ↶ |
138 |
tokio::spawn(maintenance::nightly(app.clone())); |
| d7d18246 Andy McCall ↶ |
139 |
tokio::spawn(backups::scheduler(app.clone())); |
| f677d627 Andy McCall ↶ |
140 |
|
| 141 |
tokio::spawn({ |
| 142 |
let app = app.clone(); |
| 143 |
async move { diffusion::backfill(&app).await } |
| 144 |
}); |
| 93501da2 Andy McCall ↶ |
145 |
|
| 146 |
tokio::spawn({ |
| 147 |
let app = app.clone(); |
| 148 |
async move { |
| 149 |
match images::learn_archs(&app).await { |
| 150 |
Ok(n) if n > 0 => tracing::info!(images = n, "read the architecture of kept images"), |
| 151 |
Ok(_) => {} |
| 152 |
Err(e) => tracing::warn!(error = %e, "learning image architectures"), |
| 153 |
} |
| 154 |
} |
| 155 |
}); |
| 18e381ea Andy McCall ↶ |
156 |
Ok(app) |
| d23464c0 Andy McCall ↶ |
157 |
} |
| 158 |
|
| 159 |
|
| 160 |
pub fn router(app: Shared) -> axum::Router { |
| bff9106e Andy McCall ↶ |
161 |
web::router(app.clone()) |
| 162 |
.layer(axum::middleware::from_fn_with_state(app, agent_requests)) |
| 163 |
.layer(axum::middleware::from_fn(private_pages)) |
| 164 |
.layer(axum::middleware::from_fn(slow_requests)) |
| d23464c0 Andy McCall ↶ |
165 |
} |
| 166 |
|
| bff9106e Andy McCall ↶ |
167 |
|
| 168 |
|
| 169 |
|
| 170 |
async fn agent_requests( |
| 171 |
axum::extract::State(app): axum::extract::State<Shared>, |
| 172 |
req: axum::extract::Request, |
| 173 |
next: axum::middleware::Next, |
| 174 |
) -> axum::response::Response { |
| 175 |
let token = req |
| 176 |
.headers() |
| 177 |
.get(axum::http::header::AUTHORIZATION) |
| 178 |
.and_then(|v| v.to_str().ok()) |
| 179 |
.and_then(|v| v.strip_prefix("Bearer ")) |
| 180 |
.map(|t| auth::token_hash(t.trim())); |
| 181 |
let agent = match token { |
| 182 |
Some(hash) => db::accounts::token_by_hash(&app.db, &hash).await.ok().flatten().filter(|t| t.agent).map(|t| t.name), |
| 183 |
None => None, |
| 184 |
}; |
| 185 |
notify::AGENT.scope(agent, next.run(req)).await |
| 186 |
} |
| 187 |
|
| 39442b00 Andy McCall ↶ |
188 |
|
| 189 |
const SLOW: std::time::Duration = std::time::Duration::from_secs(2); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
async fn slow_requests(req: axum::extract::Request, next: axum::middleware::Next) -> axum::response::Response { |
| 194 |
let (method, path) = (req.method().clone(), req.uri().path().to_string()); |
| 195 |
let start = std::time::Instant::now(); |
| 196 |
let res = next.run(req).await; |
| 197 |
let took = start.elapsed(); |
| 198 |
if took >= SLOW { |
| 199 |
tracing::warn!("slow request: {method} {path} took {:.1} s ({})", took.as_secs_f64(), res.status().as_u16()); |
| 200 |
} |
| 201 |
res |
| 202 |
} |
| 203 |
|
| 33bd0bd4 Andy McCall ↶ |
204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
async fn private_pages(req: axum::extract::Request, next: axum::middleware::Next) -> axum::response::Response { |
| 210 |
use axum::http::header; |
| 211 |
let signed_in = req |
| 212 |
.headers() |
| 213 |
.get_all(header::COOKIE) |
| 214 |
.iter() |
| 215 |
.filter_map(|v| v.to_str().ok()) |
| 216 |
.any(|c| c.split(';').any(|kv| kv.trim_start().starts_with("oxyhub="))); |
| 217 |
let mut res = next.run(req).await; |
| 218 |
let html = res.headers().get(header::CONTENT_TYPE).and_then(|v| v.to_str().ok()).is_some_and(|t| t.starts_with("text/html")); |
| 219 |
if signed_in && html && !res.headers().contains_key(header::CACHE_CONTROL) { |
| 220 |
res.headers_mut().insert(header::CACHE_CONTROL, header::HeaderValue::from_static("private, no-store")); |
| 221 |
} |
| 222 |
res |
| 223 |
} |
| 224 |
|
| d23464c0 Andy McCall ↶ |
225 |
|
| 226 |
pub async fn serve(app: Shared) -> anyhow::Result<()> { |
| 227 |
let router = router(app.clone()); |
| 228 |
let http = app.config.bind_addr()?; |
| 229 |
let https = app.config.tls_bind_addr()?; |
| 230 |
if http.is_none() && https.is_none() { |
| 231 |
anyhow::bail!("nothing to bind: set server.bind or server.tls_bind"); |
| 232 |
} |
| 233 |
let mut tasks = tokio::task::JoinSet::new(); |
| 234 |
if let Some(addr) = http { |
| 235 |
let listener = tokio::net::TcpListener::bind(addr).await?; |
| 236 |
tracing::info!("http on {addr}"); |
| 237 |
let r = router.clone(); |
| 60f11b71 Andy McCall ↶ |
238 |
tasks.spawn(async move { |
| 239 |
axum::serve(listener, r.into_make_service_with_connect_info::<std::net::SocketAddr>()).await.map_err(anyhow::Error::from) |
| 240 |
}); |
| d23464c0 Andy McCall ↶ |
241 |
} |
| 242 |
if let Some(addr) = https { |
| 243 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 244 |
let tls = axum_server::tls_rustls::RustlsConfig::from_pem_file(app.config.tls_cert(), app.config.tls_key()) |
| 245 |
.await |
| 246 |
.map_err(|e| anyhow::anyhow!("loading TLS files: {e}"))?; |
| 247 |
tracing::info!("https on {addr}"); |
| 248 |
let r = router.clone(); |
| 60f11b71 Andy McCall ↶ |
249 |
tasks.spawn(async move { |
| 250 |
axum_server::bind_rustls(addr, tls) |
| 251 |
.serve(r.into_make_service_with_connect_info::<std::net::SocketAddr>()) |
| 252 |
.await |
| 253 |
.map_err(anyhow::Error::from) |
| 254 |
}); |
| d23464c0 Andy McCall ↶ |
255 |
} |
| 256 |
tokio::select! { |
| 257 |
r = tasks.join_next() => { r.transpose()?.transpose()?; Ok(()) } |
| 258 |
_ = tokio::signal::ctrl_c() => { tracing::info!("stopping"); Ok(()) } |
| 259 |
} |
| 260 |
} |