| 8d2caf12 Andy McCall ↶ |
1 |
|
| d23464c0 Andy McCall ↶ |
2 |
|
| e57765de Andy McCall ↶ |
3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| d23464c0 Andy McCall ↶ |
7 |
|
| 8 |
pub mod api; |
| ea85398a Andy McCall ↶ |
9 |
pub mod apt; |
| e71846aa Andy McCall ↶ |
10 |
pub mod assistants; |
| 19bb31ff Andy McCall ↶ |
11 |
pub mod auth; |
| 86a69b53 Andy McCall ↶ |
12 |
pub mod autoland; |
| d7d18246 Andy McCall ↶ |
13 |
pub mod backups; |
| 33d98147 Andy McCall |
14 |
pub mod bus; |
| d23464c0 Andy McCall ↶ |
15 |
pub mod config; |
| 554ccacf Andy McCall ↶ |
16 |
pub mod coverage; |
| d23464c0 Andy McCall ↶ |
17 |
pub mod db; |
| f677d627 Andy McCall ↶ |
18 |
pub mod diffusion; |
| b64e2537 Andy McCall ↶ |
19 |
pub mod events; |
| 42fa9554 Andy McCall ↶ |
20 |
pub mod flow; |
| d74ce22f Andy McCall ↶ |
21 |
pub mod headers; |
| b64e2537 Andy McCall ↶ |
22 |
pub mod hooks; |
| 92a797b2 Andy McCall ↶ |
23 |
pub mod images; |
| e97500b7 Andy McCall ↶ |
24 |
pub mod insights; |
| 0b203bbf Andy McCall ↶ |
25 |
pub mod junit; |
| 026a76d2 Andy McCall ↶ |
26 |
pub mod keyless; |
| d23464c0 Andy McCall ↶ |
27 |
pub mod keys; |
| e57765de Andy McCall ↶ |
28 |
pub mod leader; |
| 2a83f530 Andy McCall ↶ |
29 |
pub mod levels; |
| 5e6d6484 Andy McCall ↶ |
30 |
pub mod live; |
| 34beb1a7 Andy McCall ↶ |
31 |
pub mod mail; |
| 8a3334b7 Andy McCall ↶ |
32 |
pub mod maintenance; |
| 8a48dd83 Andy McCall ↶ |
33 |
pub mod metrics; |
| 3f4b0ea6 Andy McCall ↶ |
34 |
pub mod mirror; |
| 9a777fec Andy McCall ↶ |
35 |
pub mod net; |
| 18e381ea Andy McCall ↶ |
36 |
pub mod notify; |
| d1727c57 Andy McCall ↶ |
37 |
pub mod oidc; |
| d23464c0 Andy McCall ↶ |
38 |
pub mod protocol; |
| 076c7a7c Andy McCall ↶ |
39 |
pub mod provenance; |
| 0ee5c3fb Andy McCall ↶ |
40 |
pub mod push_protection; |
| d23464c0 Andy McCall ↶ |
41 |
pub mod render; |
| a36bab6b Andy McCall ↶ |
42 |
pub mod reports; |
| d23464c0 Andy McCall ↶ |
43 |
pub mod repos; |
| 4ac01a73 Andy McCall ↶ |
44 |
pub mod secrets; |
| c8684d44 Andy McCall |
45 |
pub mod setup; |
| c92a9949 Andy McCall ↶ |
46 |
pub mod shutdown; |
| 83940d53 Andy McCall ↶ |
47 |
pub mod signin; |
| 89e89836 Andy McCall ↶ |
48 |
pub mod social; |
| 1f78655a Andy McCall ↶ |
49 |
pub mod storage; |
| a46d15e7 Andy McCall ↶ |
50 |
pub mod swarm; |
| 8dcaeeb8 Andy McCall ↶ |
51 |
pub mod terraform; |
| aa0bfbd1 Andy McCall ↶ |
52 |
pub mod throttle; |
| 965b950f Andy McCall ↶ |
53 |
pub mod tlog; |
| 2a83f530 Andy McCall ↶ |
54 |
pub mod variables; |
| d23464c0 Andy McCall ↶ |
55 |
pub mod web; |
| 66fd9d6b Andy McCall ↶ |
56 |
pub mod wiki; |
| d23464c0 Andy McCall ↶ |
57 |
|
| 58 |
use crate::config::Config; |
| 59 |
use crate::repos::Repos; |
| 60 |
use std::sync::Arc; |
| 61 |
|
| 62 |
|
| 63 |
pub struct App { |
| 64 |
pub config: Config, |
| 65 |
pub db: db::Pool, |
| 66 |
pub repos: Arc<Repos>, |
| 66fd9d6b Andy McCall ↶ |
67 |
|
| 68 |
pub wikis: Arc<Repos>, |
| d23464c0 Andy McCall ↶ |
69 |
pub server: Arc<oxy_server::Server>, |
| 70 |
pub hub_key: keys::HubKey, |
| 19bb31ff Andy McCall ↶ |
71 |
pub cookie_key: auth::CookieKey, |
| e71846aa Andy McCall ↶ |
72 |
|
| 73 |
pub assistant_key: assistants::SecretKey, |
| 4ac01a73 Andy McCall ↶ |
74 |
|
| 75 |
pub secret_key: assistants::SecretKey, |
| d1727c57 Andy McCall ↶ |
76 |
|
| 77 |
pub oidc: tokio::sync::OnceCell<oidc::Issuer>, |
| 7d2eb590 Andy McCall ↶ |
78 |
|
| 79 |
|
| 80 |
pub assistant_gate: std::sync::Mutex<assistants::Gate>, |
| aa0bfbd1 Andy McCall ↶ |
81 |
|
| 82 |
pub throttle: std::sync::Mutex<throttle::Throttle>, |
| 33d98147 Andy McCall |
83 |
|
| 84 |
|
| 85 |
pub bus: bus::Bus, |
| 5e6d6484 Andy McCall ↶ |
86 |
|
| 87 |
pub rooms: Arc<live::Rooms>, |
| 3556d08e Andy McCall ↶ |
88 |
|
| 89 |
|
| 90 |
pub mail: std::sync::RwLock<config::Email>, |
| a46d15e7 Andy McCall ↶ |
91 |
|
| 92 |
pub swarm: swarm::Tracker, |
| d23464c0 Andy McCall ↶ |
93 |
pub started: std::time::Instant, |
| 965b950f Andy McCall ↶ |
94 |
|
| 95 |
pub tlog: Arc<tlog::Log>, |
| e57765de Andy McCall ↶ |
96 |
|
| 97 |
pub roles: config::Roles, |
| 98 |
|
| 99 |
pub leader: leader::Leader, |
| d23464c0 Andy McCall ↶ |
100 |
} |
| 101 |
|
| d1727c57 Andy McCall ↶ |
102 |
impl App { |
| 92a797b2 Andy McCall ↶ |
103 |
|
| 104 |
pub fn images(&self) -> images::Store { |
| 105 |
images::Store::new(&self.config.data) |
| 106 |
} |
| 107 |
|
| d1727c57 Andy McCall ↶ |
108 |
|
| 109 |
pub async fn oidc(&self) -> anyhow::Result<&oidc::Issuer> { |
| 110 |
let keys = self.config.keys_dir(); |
| 111 |
self.oidc.get_or_try_init(|| async move { tokio::task::spawn_blocking(move || oidc::Issuer::load_or_create(&keys)).await? }).await |
| 112 |
} |
| 113 |
} |
| 114 |
|
| d23464c0 Andy McCall ↶ |
115 |
pub type Shared = Arc<App>; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
pub async fn build(config: Config) -> anyhow::Result<Shared> { |
| e57765de Andy McCall ↶ |
120 |
let roles = config.roles()?; |
| d23464c0 Andy McCall ↶ |
121 |
std::fs::create_dir_all(config.repos_dir())?; |
| 3b1bc27f Andy McCall |
122 |
let db = db::open(&config).await?; |
| d23464c0 Andy McCall ↶ |
123 |
let repos = Arc::new(Repos::new(config.repos_dir())); |
| 66fd9d6b Andy McCall ↶ |
124 |
let wikis = Arc::new(Repos::new(config.data.join("wikis"))); |
| d23464c0 Andy McCall ↶ |
125 |
let hub_key = keys::ensure_identity(&config.keys_dir(), &config.site.name, config.base_url())?; |
| 19bb31ff Andy McCall ↶ |
126 |
let cookie_key = auth::CookieKey::load_or_create(&config.keys_dir())?; |
| e71846aa Andy McCall ↶ |
127 |
let assistant_key = assistants::SecretKey::load_or_create(&config.keys_dir())?; |
| 4ac01a73 Andy McCall ↶ |
128 |
let secret_key = assistants::SecretKey::load_or_create_named(&config.keys_dir(), "secrets.key")?; |
| 66fd9d6b Andy McCall ↶ |
129 |
let backend = protocol::HubBackend::new( |
| 130 |
repos.clone(), |
| 131 |
wikis.clone(), |
| 132 |
db.clone(), |
| 133 |
config.auth.push_tokens.clone(), |
| 134 |
tokio::runtime::Handle::current(), |
| 3eb491e9 Andy McCall ↶ |
135 |
) |
| 0ee5c3fb Andy McCall ↶ |
136 |
.with_acceptor(hub_key.identity.clone(), keys::load_secret(&config.keys_dir())?.1) |
| 137 |
.with_scan_budget(push_protection::Budget { |
| 138 |
bytes: config.limits.scan_limit.0, |
| 139 |
file: config.limits.scan_file_limit.0, |
| 140 |
time: std::time::Duration::from_millis(config.limits.scan_time_ms), |
| 141 |
}); |
| 18e381ea Andy McCall ↶ |
142 |
let slot = backend.app_slot(); |
| d23464c0 Andy McCall ↶ |
143 |
let server = oxy_server::Server::new(backend); |
| 3556d08e Andy McCall ↶ |
144 |
let mail = mail::load(&db, &config.email).await; |
| e57765de Andy McCall ↶ |
145 |
|
| 146 |
let (tlog_dir, origin) = (config.data.join("tlog"), tlog::origin_of(config.base_url())); |
| 147 |
let tlog = Arc::new(if roles.web { tlog::Log::open(&tlog_dir, &origin)? } else { tlog::Log::open_reader(&tlog_dir, &origin)? }); |
| 4f40eed0 Andy McCall ↶ |
148 |
tlog.configure(&config.tlog)?; |
| b64e2537 Andy McCall ↶ |
149 |
let app = Arc::new(App { |
| 3556d08e Andy McCall ↶ |
150 |
mail: std::sync::RwLock::new(mail), |
| b64e2537 Andy McCall ↶ |
151 |
config, |
| 152 |
db, |
| 153 |
repos, |
| 66fd9d6b Andy McCall ↶ |
154 |
wikis, |
| b64e2537 Andy McCall ↶ |
155 |
server, |
| 156 |
hub_key, |
| 157 |
cookie_key, |
| e71846aa Andy McCall ↶ |
158 |
assistant_key, |
| 4ac01a73 Andy McCall ↶ |
159 |
secret_key, |
| d1727c57 Andy McCall ↶ |
160 |
oidc: tokio::sync::OnceCell::new(), |
| 7d2eb590 Andy McCall ↶ |
161 |
assistant_gate: std::sync::Mutex::new(assistants::Gate::default()), |
| aa0bfbd1 Andy McCall ↶ |
162 |
throttle: std::sync::Mutex::new(throttle::Throttle::default()), |
| 33d98147 Andy McCall |
163 |
bus: bus::Bus::default(), |
| 5e6d6484 Andy McCall ↶ |
164 |
rooms: Arc::new(live::Rooms::default()), |
| a46d15e7 Andy McCall ↶ |
165 |
swarm: Default::default(), |
| b64e2537 Andy McCall ↶ |
166 |
started: std::time::Instant::now(), |
| 965b950f Andy McCall ↶ |
167 |
tlog, |
| e57765de Andy McCall ↶ |
168 |
roles, |
| 169 |
leader: leader::Leader::default(), |
| b64e2537 Andy McCall ↶ |
170 |
}); |
| 18e381ea Andy McCall ↶ |
171 |
*slot.lock().unwrap() = Some(Arc::downgrade(&app)); |
| 33d98147 Andy McCall |
172 |
bus::start(&app).await?; |
| 1f78655a Andy McCall ↶ |
173 |
storage::load(&app).await?; |
| 554ccacf Andy McCall ↶ |
174 |
coverage::load(&app).await?; |
| 36e5a9c1 Andy McCall ↶ |
175 |
junit::load(&app).await?; |
| c8df33a6 Andy McCall ↶ |
176 |
reports::load(&app).await?; |
| e57765de Andy McCall ↶ |
177 |
start_background(&app); |
| 178 |
setup::ensure(&app).await?; |
| 179 |
Ok(app) |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
fn start_background(app: &Shared) { |
| 187 |
if app.roles.web { |
| 188 |
|
| 189 |
leader::run(app, "tlog.checkpointer", tlog::checkpointer); |
| 190 |
} |
| 191 |
if !app.roles.worker { |
| 192 |
return; |
| 193 |
} |
| 194 |
leader::run(app, "hooks.worker", hooks::worker); |
| 195 |
leader::run(app, "flow.reaper", flow::reaper); |
| 196 |
leader::run(app, "flow.scheduler", flow::scheduler); |
| 197 |
leader::run(app, "flow.placement", flow::placement); |
| 198 |
leader::run(app, "maintenance.nightly", maintenance::nightly); |
| 199 |
leader::run(app, "backups.scheduler", backups::scheduler); |
| 200 |
|
| 201 |
|
| 202 |
leader::run(app, "diffusion.backfill", |app| async move { diffusion::backfill(&app).await }); |
| 0e04dfc2 Andy McCall ↶ |
203 |
|
| e57765de Andy McCall ↶ |
204 |
leader::run(app, "diffusion.reconciler", diffusion::reconciler); |
| 205 |
|
| 206 |
|
| 207 |
app.leader.note("images.learn_archs"); |
| 93501da2 Andy McCall ↶ |
208 |
tokio::spawn({ |
| 209 |
let app = app.clone(); |
| 210 |
async move { |
| 211 |
match images::learn_archs(&app).await { |
| 212 |
Ok(n) if n > 0 => tracing::info!(images = n, "read the architecture of kept images"), |
| 213 |
Ok(_) => {} |
| 214 |
Err(e) => tracing::warn!(error = %e, "learning image architectures"), |
| 215 |
} |
| 216 |
} |
| 217 |
}); |
| e57765de Andy McCall ↶ |
218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
pub fn router(app: Shared) -> axum::Router { |
| 223 |
if !app.roles.web { |
| 224 |
return worker_router(app); |
| 225 |
} |
| 226 |
full_router(app) |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
fn worker_router(app: Shared) -> axum::Router { |
| 231 |
use axum::routing::get; |
| 232 |
let hsts = headers::hsts(app.config.base_url(), app.config.server.hsts_max_age); |
| 233 |
axum::Router::new() |
| 234 |
.route("/health", get(web::home::health)) |
| 235 |
.route("/metrics", get(metrics::metrics)) |
| 236 |
.with_state(app) |
| 237 |
.layer(axum::middleware::from_fn_with_state(hsts, headers::security_headers)) |
| d23464c0 Andy McCall ↶ |
238 |
} |
| 239 |
|
| 240 |
|
| e57765de Andy McCall ↶ |
241 |
fn full_router(app: Shared) -> axum::Router { |
| 9a8e3b44 Andy McCall ↶ |
242 |
let https = app_https(&app); |
| 2cbf6f34 Andy McCall ↶ |
243 |
let hsts = headers::hsts(app.config.base_url(), app.config.server.hsts_max_age); |
| bff9106e Andy McCall ↶ |
244 |
web::router(app.clone()) |
| 245 |
.layer(axum::middleware::from_fn_with_state(app, agent_requests)) |
| 246 |
.layer(axum::middleware::from_fn(private_pages)) |
| 247 |
.layer(axum::middleware::from_fn(slow_requests)) |
| 9a8e3b44 Andy McCall ↶ |
248 |
.layer(axum::middleware::from_fn_with_state(https, https_only)) |
| 2cbf6f34 Andy McCall ↶ |
249 |
.layer(axum::middleware::from_fn_with_state(hsts, headers::security_headers)) |
| d23464c0 Andy McCall ↶ |
250 |
} |
| 251 |
|
| 9a8e3b44 Andy McCall ↶ |
252 |
|
| 253 |
fn app_https(app: &Shared) -> Option<String> { |
| 254 |
let base = app.config.base_url(); |
| 255 |
base.starts_with("https://").then(|| base.trim_end_matches('/').to_string()) |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
fn forwarded_scheme(headers: &axum::http::HeaderMap) -> Option<String> { |
| 262 |
if let Some(p) = headers.get("x-forwarded-proto").and_then(|v| v.to_str().ok()) { |
| 263 |
return p.split(',').next().map(|s| s.trim().to_ascii_lowercase()); |
| 264 |
} |
| 265 |
let visitor = headers.get("cf-visitor").and_then(|v| v.to_str().ok())?; |
| 266 |
serde_json::from_str::<serde_json::Value>(visitor).ok()?["scheme"].as_str().map(str::to_ascii_lowercase) |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
async fn https_only( |
| 275 |
axum::extract::State(https): axum::extract::State<Option<String>>, |
| 276 |
req: axum::extract::Request, |
| 277 |
next: axum::middleware::Next, |
| 278 |
) -> axum::response::Response { |
| 279 |
use axum::response::IntoResponse; |
| 280 |
let Some(base) = https else { return next.run(req).await }; |
| 281 |
if forwarded_scheme(req.headers()).as_deref() != Some("http") { |
| 282 |
return next.run(req).await; |
| 283 |
} |
| 284 |
let to = format!("{base}{}", req.uri().path_and_query().map(|p| p.as_str()).unwrap_or("/")); |
| 285 |
if matches!(*req.method(), axum::http::Method::GET | axum::http::Method::HEAD) { |
| 286 |
return axum::response::Redirect::permanent(&to).into_response(); |
| 287 |
} |
| 288 |
(axum::http::StatusCode::BAD_REQUEST, format!("This site takes requests over HTTPS only: send it to {to}\n")).into_response() |
| 289 |
} |
| 290 |
|
| bff9106e Andy McCall ↶ |
291 |
|
| 292 |
|
| 293 |
|
| 294 |
async fn agent_requests( |
| 295 |
axum::extract::State(app): axum::extract::State<Shared>, |
| 296 |
req: axum::extract::Request, |
| 297 |
next: axum::middleware::Next, |
| 298 |
) -> axum::response::Response { |
| 299 |
let token = req |
| 300 |
.headers() |
| 301 |
.get(axum::http::header::AUTHORIZATION) |
| 302 |
.and_then(|v| v.to_str().ok()) |
| 303 |
.and_then(|v| v.strip_prefix("Bearer ")) |
| d4a0043f Andy McCall ↶ |
304 |
.map(|t| t.trim().to_string()); |
| bff9106e Andy McCall ↶ |
305 |
let agent = match token { |
| d4a0043f Andy McCall ↶ |
306 |
|
| 307 |
Some(t) if t.starts_with(keyless::GRANT_TOKEN_PREFIX) => db::accounts::agent_grant_by_token(&app.db, &auth::token_hash(&t)) |
| 308 |
.await |
| 309 |
.ok() |
| 310 |
.flatten() |
| 311 |
.filter(|g| g.live(db::now_ms())) |
| 312 |
.map(|g| g.agent), |
| 313 |
Some(t) => db::accounts::token_by_hash(&app.db, &auth::token_hash(&t)).await.ok().flatten().filter(|t| t.agent).map(|t| t.name), |
| bff9106e Andy McCall ↶ |
314 |
None => None, |
| 315 |
}; |
| 316 |
notify::AGENT.scope(agent, next.run(req)).await |
| 317 |
} |
| 318 |
|
| 39442b00 Andy McCall ↶ |
319 |
|
| 320 |
const SLOW: std::time::Duration = std::time::Duration::from_secs(2); |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
async fn slow_requests(req: axum::extract::Request, next: axum::middleware::Next) -> axum::response::Response { |
| 325 |
let (method, path) = (req.method().clone(), req.uri().path().to_string()); |
| 326 |
let start = std::time::Instant::now(); |
| 327 |
let res = next.run(req).await; |
| 328 |
let took = start.elapsed(); |
| b9b74d99 Andy McCall ↶ |
329 |
metrics::observe(method.as_str(), &path, res.status().as_u16(), took.as_secs_f64()); |
| 39442b00 Andy McCall ↶ |
330 |
if took >= SLOW { |
| 331 |
tracing::warn!("slow request: {method} {path} took {:.1} s ({})", took.as_secs_f64(), res.status().as_u16()); |
| 332 |
} |
| 333 |
res |
| 334 |
} |
| 335 |
|
| 33bd0bd4 Andy McCall ↶ |
336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
async fn private_pages(req: axum::extract::Request, next: axum::middleware::Next) -> axum::response::Response { |
| 342 |
use axum::http::header; |
| 343 |
let signed_in = req |
| 344 |
.headers() |
| 345 |
.get_all(header::COOKIE) |
| 346 |
.iter() |
| 347 |
.filter_map(|v| v.to_str().ok()) |
| 348 |
.any(|c| c.split(';').any(|kv| kv.trim_start().starts_with("oxyhub="))); |
| 349 |
let mut res = next.run(req).await; |
| 350 |
let html = res.headers().get(header::CONTENT_TYPE).and_then(|v| v.to_str().ok()).is_some_and(|t| t.starts_with("text/html")); |
| 351 |
if signed_in && html && !res.headers().contains_key(header::CACHE_CONTROL) { |
| 352 |
res.headers_mut().insert(header::CACHE_CONTROL, header::HeaderValue::from_static("private, no-store")); |
| 353 |
} |
| 354 |
res |
| 355 |
} |
| 356 |
|
| d23464c0 Andy McCall ↶ |
357 |
|
| 358 |
pub async fn serve(app: Shared) -> anyhow::Result<()> { |
| 359 |
let router = router(app.clone()); |
| 360 |
let http = app.config.bind_addr()?; |
| 361 |
let https = app.config.tls_bind_addr()?; |
| e57765de Andy McCall ↶ |
362 |
if http.is_none() && https.is_none() && app.roles.web { |
| d23464c0 Andy McCall ↶ |
363 |
anyhow::bail!("nothing to bind: set server.bind or server.tls_bind"); |
| 364 |
} |
| e57765de Andy McCall ↶ |
365 |
tracing::info!("roles: {}", app.roles.names().join(", ")); |
| c92a9949 Andy McCall ↶ |
366 |
|
| 367 |
let signal = shutdown::on_signal()?; |
| d23464c0 Andy McCall ↶ |
368 |
let mut tasks = tokio::task::JoinSet::new(); |
| 369 |
if let Some(addr) = http { |
| c92a9949 Andy McCall ↶ |
370 |
let listener = match shutdown::systemd_listener() { |
| 371 |
Some(l) => { |
| 372 |
tracing::info!("http on the socket systemd holds ({})", l.local_addr().map(|a| a.to_string()).unwrap_or_default()); |
| 373 |
tokio::net::TcpListener::from_std(l)? |
| 374 |
} |
| 375 |
None => { |
| 376 |
tracing::info!("http on {addr}"); |
| 377 |
tokio::net::TcpListener::bind(addr).await? |
| 378 |
} |
| 379 |
}; |
| d23464c0 Andy McCall ↶ |
380 |
let r = router.clone(); |
| 60f11b71 Andy McCall ↶ |
381 |
tasks.spawn(async move { |
| c92a9949 Andy McCall ↶ |
382 |
axum::serve(listener, r.into_make_service_with_connect_info::<std::net::SocketAddr>()) |
| 383 |
.with_graceful_shutdown(shutdown::stopping()) |
| 384 |
.await |
| 385 |
.map_err(anyhow::Error::from) |
| 60f11b71 Andy McCall ↶ |
386 |
}); |
| d23464c0 Andy McCall ↶ |
387 |
} |
| 388 |
if let Some(addr) = https { |
| 389 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 390 |
let tls = axum_server::tls_rustls::RustlsConfig::from_pem_file(app.config.tls_cert(), app.config.tls_key()) |
| 391 |
.await |
| 392 |
.map_err(|e| anyhow::anyhow!("loading TLS files: {e}"))?; |
| 393 |
tracing::info!("https on {addr}"); |
| 394 |
let r = router.clone(); |
| c92a9949 Andy McCall ↶ |
395 |
let handle = axum_server::Handle::new(); |
| 396 |
let draining = handle.clone(); |
| 397 |
tokio::spawn(async move { |
| 398 |
shutdown::stopping().await; |
| 399 |
draining.graceful_shutdown(Some(shutdown::DRAIN)); |
| 400 |
}); |
| 60f11b71 Andy McCall ↶ |
401 |
tasks.spawn(async move { |
| 402 |
axum_server::bind_rustls(addr, tls) |
| c92a9949 Andy McCall ↶ |
403 |
.handle(handle) |
| 60f11b71 Andy McCall ↶ |
404 |
.serve(r.into_make_service_with_connect_info::<std::net::SocketAddr>()) |
| 405 |
.await |
| 406 |
.map_err(anyhow::Error::from) |
| 407 |
}); |
| d23464c0 Andy McCall ↶ |
408 |
} |
| 409 |
tokio::select! { |
| e57765de Andy McCall ↶ |
410 |
|
| 411 |
r = tasks.join_next(), if !tasks.is_empty() => { r.transpose()?.transpose()?; Ok(()) } |
| c92a9949 Andy McCall ↶ |
412 |
_ = signal => { |
| 413 |
|
| 414 |
|
| 415 |
let drained = tokio::time::timeout(shutdown::DRAIN, async { while tasks.join_next().await.is_some() {} }).await; |
| 416 |
if drained.is_err() { |
| 417 |
tracing::warn!("requests still running after {} s: stopping anyway", shutdown::DRAIN.as_secs()); |
| 418 |
} |
| 419 |
tracing::info!("stopped"); |
| 420 |
Ok(()) |
| 421 |
} |
| d23464c0 Andy McCall ↶ |
422 |
} |
| 423 |
} |