| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | | pub mod witness; |
| 15 | |
|
| 16 | | use axum::body::Bytes; |
| 17 | | use axum::extract::{Path, State}; |
| 18 | | use axum::http::{header, HeaderMap, Method, StatusCode}; |
| 19 | | use axum::response::{IntoResponse, Response}; |
| 20 | | use axum::routing::any; |
| 21 | | use axum::Router; |
| 22 | | use oxy_core::protocol::{wire, CONTENT_TYPE}; |
| 23 | | use oxy_core::repo::Repo; |
| 24 | | use std::collections::HashMap; |
| 25 | | use std::path::PathBuf; |
| 26 | | use std::sync::{Arc, Mutex}; |
| 27 | |
|
| 28 | | #[derive(Clone, Debug)] |
| 29 | | pub struct ServerConfig { |
| 30 | | |
| 31 | | pub root: PathBuf, |
| 32 | | |
| 33 | | pub tokens: Vec<String>, |
| 34 | | |
| 35 | | pub tls: Option<TlsFiles>, |
| 36 | | } |
| 37 | |
|
| 38 | | #[derive(Clone, Debug)] |
| 39 | | pub struct TlsFiles { |
| 40 | | pub cert: PathBuf, |
| 41 | | pub key: PathBuf, |
| 42 | | } |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 47 | | pub enum Access { |
| 48 | | Read, |
| 49 | | Push, |
| 50 | | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | | pub trait Backend: Send + Sync + 'static { |
| 56 | | |
| 57 | | |
| 58 | | |
| 59 | | fn open(&self, name: &str) -> Result<Repo, Failure>; |
| 60 | |
|
| 61 | | |
| 62 | | fn authorize(&self, name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure>; |
| 63 | |
|
| 64 | | |
| 65 | | |
| 66 | | |
| 67 | 34× | fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {} |
| 68 | |
|
| 69 | | |
| 70 | | |
| 71 | | |
| 72 | | |
| 73 | | |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | | |
| 78 | | |
| 79 | 34× | fn vet_push( |
| 80 | 34× | &self, |
| 81 | 34× | _name: &str, |
| 82 | 34× | _repo: &Repo, |
| 83 | 34× | _bundle: &oxy_core::protocol::Bundle, |
| 84 | 34× | _updates: &[oxy_core::protocol::RefUpdate], |
| 85 | 34× | _headers: &HeaderMap, |
| 86 | 34× | ) -> Result<(), Failure> { |
| 87 | 34× | Ok(()) |
| 88 | 34× | } |
| 89 | |
|
| 90 | | |
| 91 | | |
| 92 | | |
| 93 | 139× | fn add_log(&self, _name: &str, _refs: &mut oxy_core::protocol::RefsResponse) {} |
| 94 | |
|
| 95 | | |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | |
| 100 | | |
| 101 | 139× | fn adjust_refs( |
| 102 | 139× | &self, |
| 103 | 139× | _name: &str, |
| 104 | 139× | repo: &Repo, |
| 105 | 139× | refs: &mut oxy_core::protocol::RefsResponse, |
| 106 | 139× | headers: &HeaderMap, |
| 107 | 139× | ) { |
| 108 | 139× | tailor_refs(repo, refs, headers); |
| 109 | 139× | } |
| 110 | |
|
| 111 | | |
| 112 | | |
| 113 | | |
| 114 | | |
| 115 | | |
| 116 | 0× | fn vet_tag_withdrawal(&self, _name: &str, _tag: &str) -> Result<(), String> { |
| 117 | 0× | Ok(()) |
| 118 | 0× | } |
| 119 | |
|
| 120 | | |
| 121 | | |
| 122 | 28× | fn default_bookmark(&self, _name: &str) -> Option<String> { |
| 123 | 28× | None |
| 124 | 28× | } |
| 125 | | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | | pub struct Server { |
| 130 | | backend: Box<dyn Backend>, |
| 131 | | locks: Mutex<HashMap<String, Arc<Mutex<()>>>>, |
| 132 | | } |
| 133 | |
|
| 134 | |
|
| 135 | | #[derive(Debug)] |
| 136 | | pub struct Failure(pub StatusCode, pub String); |
| 137 | |
|
| 138 | | impl IntoResponse for Failure { |
| 139 | 9× | fn into_response(self) -> Response { |
| 140 | 9× | (self.0, self.1).into_response() |
| 141 | 9× | } |
| 142 | | } |
| 143 | |
|
| 144 | | impl From<oxy_core::repo::RepoError> for Failure { |
| 145 | 0× | fn from(e: oxy_core::repo::RepoError) -> Failure { |
| 146 | | use oxy_core::repo::RepoError; |
| 147 | | |
| 148 | 0× | let status = match &e { |
| 149 | 0× | RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 150 | 0× | _ => StatusCode::BAD_REQUEST, |
| 151 | | }; |
| 152 | 0× | Failure(status, e.to_string()) |
| 153 | 0× | } |
| 154 | | } |
| 155 | |
|
| 156 | |
|
| 157 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 158 | | pub enum Endpoint { |
| 159 | | Capabilities, |
| 160 | | Refs, |
| 161 | | Negotiate, |
| 162 | | Fetch, |
| 163 | | Objects, |
| 164 | | Checkpoint, |
| 165 | | Shallow, |
| 166 | | Push, |
| 167 | | } |
| 168 | |
|
| 169 | | impl Endpoint { |
| 170 | 428× | pub fn parse(name: &str) -> Option<Endpoint> { |
| 171 | 428× | Some(match name { |
| 172 | 428× | "capabilities" => Endpoint::Capabilities, |
| 173 | 287× | "refs" => Endpoint::Refs, |
| 174 | 148× | "negotiate" => Endpoint::Negotiate, |
| 175 | 95× | "fetch" => Endpoint::Fetch, |
| 176 | 42× | "objects" => Endpoint::Objects, |
| 177 | 41× | "checkpoint" => Endpoint::Checkpoint, |
| 178 | 41× | "shallow" => Endpoint::Shallow, |
| 179 | 41× | "push" => Endpoint::Push, |
| 180 | 0× | _ => return None, |
| 181 | | }) |
| 182 | 428× | } |
| 183 | |
|
| 184 | 3× | pub fn name(self) -> &'static str { |
| 185 | 3× | match self { |
| 186 | 2× | Endpoint::Capabilities => "capabilities", |
| 187 | 0× | Endpoint::Refs => "refs", |
| 188 | 0× | Endpoint::Negotiate => "negotiate", |
| 189 | 0× | Endpoint::Fetch => "fetch", |
| 190 | 0× | Endpoint::Objects => "objects", |
| 191 | 0× | Endpoint::Checkpoint => "checkpoint", |
| 192 | 0× | Endpoint::Shallow => "shallow", |
| 193 | 1× | Endpoint::Push => "push", |
| 194 | | } |
| 195 | 3× | } |
| 196 | |
|
| 197 | | |
| 198 | 428× | pub fn method(self) -> Method { |
| 199 | 428× | match self { |
| 200 | 280× | Endpoint::Capabilities | Endpoint::Refs => Method::GET, |
| 201 | 148× | _ => Method::POST, |
| 202 | | } |
| 203 | 428× | } |
| 204 | |
|
| 205 | 428× | fn access(self) -> Access { |
| 206 | 428× | match self { |
| 207 | 41× | Endpoint::Push => Access::Push, |
| 208 | 387× | _ => Access::Read, |
| 209 | | } |
| 210 | 428× | } |
| 211 | | } |
| 212 | |
|
| 213 | 419× | fn ok(body: Vec<u8>) -> Response { |
| 214 | 419× | ([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response() |
| 215 | 419× | } |
| 216 | |
|
| 217 | 511× | pub fn valid_repo_name(name: &str) -> bool { |
| 218 | 511× | !name.is_empty() |
| 219 | 511× | && !name.starts_with('.') |
| 220 | 510× | && name |
| 221 | 510× | .chars() |
| 222 | 2799× | .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) |
| 223 | 511× | } |
| 224 | |
|
| 225 | | impl Server { |
| 226 | 15× | pub fn new(backend: impl Backend) -> Arc<Server> { |
| 227 | 15× | Arc::new(Server { |
| 228 | 15× | backend: Box::new(backend), |
| 229 | 15× | locks: Mutex::new(HashMap::new()), |
| 230 | 15× | }) |
| 231 | 15× | } |
| 232 | |
|
| 233 | 0× | pub fn backend(&self) -> &dyn Backend { |
| 234 | 0× | self.backend.as_ref() |
| 235 | 0× | } |
| 236 | |
|
| 237 | 35× | fn lock_for(&self, name: &str) -> Arc<Mutex<()>> { |
| 238 | 35× | self.locks |
| 239 | 35× | .lock() |
| 240 | 35× | .unwrap() |
| 241 | 35× | .entry(name.to_string()) |
| 242 | 35× | .or_default() |
| 243 | 35× | .clone() |
| 244 | 35× | } |
| 245 | |
|
| 246 | | |
| 247 | | |
| 248 | 428× | pub async fn handle( |
| 249 | 428× | self: &Arc<Self>, |
| 250 | 428× | name: String, |
| 251 | 428× | endpoint: Endpoint, |
| 252 | 428× | method: &Method, |
| 253 | 428× | headers: &HeaderMap, |
| 254 | 428× | body: Bytes, |
| 255 | 428× | ) -> Response { |
| 256 | 428× | if *method != endpoint.method() { |
| 257 | 0× | return Failure( |
| 258 | 0× | StatusCode::METHOD_NOT_ALLOWED, |
| 259 | 0× | format!("{} is {}", endpoint.name(), endpoint.method()), |
| 260 | 0× | ) |
| 261 | 0× | .into_response(); |
| 262 | 428× | } |
| 263 | 428× | if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) { |
| 264 | 6× | return f.into_response(); |
| 265 | 422× | } |
| 266 | 422× | let claimed = headers |
| 267 | 422× | .get(oxy_core::protocol::REPO_ID_HEADER) |
| 268 | 422× | .and_then(|v| v.to_str().ok()) |
| 269 | 422× | .and_then(hex_id); |
| 270 | 422× | let server = Arc::clone(self); |
| 271 | 422× | let request_headers = headers.clone(); |
| 272 | 422× | let repo = name.clone(); |
| 273 | 422× | let result = tokio::task::spawn_blocking(move || { |
| 274 | 422× | server.respond(&repo, endpoint, body, claimed, &request_headers) |
| 275 | 422× | }) |
| 276 | 422× | .await; |
| 277 | 422× | let failure = match result { |
| 278 | 419× | Ok(Ok(r)) => return r, |
| 279 | 3× | Ok(Err(f)) => f, |
| 280 | 0× | Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), |
| 281 | | }; |
| 282 | | |
| 283 | 3× | let f = Failure( |
| 284 | 3× | failure.0, |
| 285 | 3× | format!("{} of {name}: {}", endpoint.name(), failure.1), |
| 286 | 3× | ); |
| 287 | 3× | eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1); |
| 288 | 3× | f.into_response() |
| 289 | 428× | } |
| 290 | |
|
| 291 | 422× | fn respond( |
| 292 | 422× | &self, |
| 293 | 422× | name: &str, |
| 294 | 422× | endpoint: Endpoint, |
| 295 | 422× | body: Bytes, |
| 296 | 422× | claimed: Option<[u8; 16]>, |
| 297 | 422× | headers: &HeaderMap, |
| 298 | 422× | ) -> Result<Response, Failure> { |
| 299 | 422× | let r = self.backend.open(name)?; |
| 300 | 420× | Ok(match endpoint { |
| 301 | 138× | Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)), |
| 302 | | Endpoint::Refs => { |
| 303 | 139× | let mut refs = r.remote_refs()?; |
| 304 | 139× | if let Some(d) = self.backend.default_bookmark(name) { |
| 305 | 117× | refs.default_bookmark = Some(d); |
| 306 | 117× | } |
| 307 | 139× | self.backend.add_log(name, &mut refs); |
| 308 | 139× | self.backend.adjust_refs(name, &r, &mut refs, headers); |
| 309 | 139× | ok(wire::encode_refs(r.id()?, &refs)) |
| 310 | | } |
| 311 | | Endpoint::Negotiate => { |
| 312 | 53× | let (have, want) = wire::decode_negotiate(&body)?; |
| 313 | 53× | ok(wire::encode_hashes(&r.negotiate(&have, &want)?)) |
| 314 | | } |
| 315 | | Endpoint::Fetch => { |
| 316 | 53× | let (changes, max_blob) = wire::decode_fetch(&body)?; |
| 317 | 53× | ok(wire::encode_bundle( |
| 318 | 53× | &r.bundle_for_filtered(&changes, max_blob)?, |
| 319 | | )) |
| 320 | | } |
| 321 | | Endpoint::Objects => { |
| 322 | 1× | let refs = wire::decode_hashes(&body)?; |
| 323 | 1× | let bundle = oxy_core::protocol::Bundle { |
| 324 | 1× | objects: r.objects_for(&refs)?, |
| 325 | 1× | ..Default::default() |
| 326 | | }; |
| 327 | 1× | ok(wire::encode_bundle(&bundle)) |
| 328 | | } |
| 329 | | Endpoint::Checkpoint => { |
| 330 | 0× | let heads = wire::decode_hashes(&body)?; |
| 331 | 0× | let lock = self.lock_for(name); |
| 332 | 0× | let _guard = lock.lock().unwrap(); |
| 333 | 0× | let bundle = r.checkpoint_bundle_for(&heads)?; |
| 334 | 0× | let _t = oxy_core::timing::phase("serve: encode the checkpoint"); |
| 335 | 0× | ok(wire::encode_bundle(&bundle)) |
| 336 | | } |
| 337 | | Endpoint::Shallow => { |
| 338 | 0× | let (heads, depth) = wire::decode_shallow(&body)?; |
| 339 | 0× | ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?)) |
| 340 | | } |
| 341 | | Endpoint::Push => { |
| 342 | 36× | let (bundle, updates, policy, tags) = wire::decode_push(&body)?; |
| 343 | | |
| 344 | | |
| 345 | 36× | self.backend |
| 346 | 36× | .vet_push(name, &r, &bundle, &updates, headers)?; |
| 347 | 35× | let lock = self.lock_for(name); |
| 348 | 35× | let _guard = lock.lock().unwrap(); |
| 349 | 35× | if r.is_unbound() { |
| 350 | | |
| 351 | 3× | match claimed { |
| 352 | 3× | Some(id) => r.bind_id(id)?, |
| 353 | | None => { |
| 354 | 0× | return Err(Failure( |
| 355 | 0× | StatusCode::BAD_REQUEST, |
| 356 | 0× | format!( |
| 357 | 0× | "this repository is unbound; push with a client that sends {}", |
| 358 | 0× | oxy_core::protocol::REPO_ID_HEADER |
| 359 | 0× | ), |
| 360 | 0× | )) |
| 361 | | } |
| 362 | | } |
| 363 | 32× | } |
| 364 | 35× | let protected = self.backend.default_bookmark(name); |
| 365 | 35× | let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| { |
| 366 | 35× | u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str()) |
| 367 | 35× | }); |
| 368 | 35× | let mut kept_tags = Vec::new(); |
| 369 | 35× | let mut withdrawals_refused = Vec::new(); |
| 370 | 35× | for t in tags { |
| 371 | 3× | match t |
| 372 | 3× | .is_withdrawal() |
| 373 | 3× | .then(|| self.backend.vet_tag_withdrawal(name, &t.name)) |
| 374 | | { |
| 375 | 0× | Some(Err(reason)) => withdrawals_refused.push((t.name, reason)), |
| 376 | 3× | _ => kept_tags.push(t), |
| 377 | | } |
| 378 | | } |
| 379 | 35× | let mut report = r.receive_push_report(bundle, updates, policy, kept_tags)?; |
| 380 | | |
| 381 | | |
| 382 | 35× | if !refused.is_empty() || !withdrawals_refused.is_empty() { |
| 383 | 1× | report |
| 384 | 1× | .results |
| 385 | 1× | .retain(|r| r.name != oxy_core::repo::OBJECTS_RESULT); |
| 386 | 34× | } |
| 387 | 35× | for u in refused { |
| 388 | 1× | report.results.push(oxy_core::protocol::RefResult { |
| 389 | 1× | name: u.name.clone(), |
| 390 | 1× | ok: false, |
| 391 | 1× | reason: format!( |
| 392 | 1× | "{:?} is this repository's default bookmark and cannot be deleted", |
| 393 | 1× | u.name |
| 394 | 1× | ), |
| 395 | 1× | }); |
| 396 | 1× | } |
| 397 | 35× | for (tag, reason) in withdrawals_refused { |
| 398 | 0× | report.results.push(oxy_core::protocol::RefResult { |
| 399 | 0× | name: format!("tags/{tag}"), |
| 400 | 0× | ok: false, |
| 401 | 0× | reason, |
| 402 | 0× | }); |
| 403 | 0× | } |
| 404 | 41× | for r in report.results.iter().filter(|r| !r.ok) { |
| 405 | 10× | eprintln!( |
| 406 | 10× | "oxy-server: push to {name}: {} refused: {}", |
| 407 | 10× | r.name, r.reason |
| 408 | 10× | ); |
| 409 | 10× | } |
| 410 | 35× | self.backend.after_push(name, &report, headers); |
| 411 | 35× | ok(wire::encode_push_response(&report.results)) |
| 412 | | } |
| 413 | | }) |
| 414 | 422× | } |
| 415 | | } |
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | | pub struct DirBackend { |
| 420 | | pub root: PathBuf, |
| 421 | | pub tokens: Vec<String>, |
| 422 | | } |
| 423 | |
|
| 424 | | impl Backend for DirBackend { |
| 425 | 511× | fn open(&self, name: &str) -> Result<Repo, Failure> { |
| 426 | 511× | if !valid_repo_name(name) { |
| 427 | 1× | return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 428 | 510× | } |
| 429 | 510× | let dir = self.root.join(name); |
| 430 | 510× | if !dir.join(".oxy").is_dir() { |
| 431 | 1× | return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 432 | 509× | } |
| 433 | 509× | Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) |
| 434 | 511× | } |
| 435 | |
|
| 436 | 146× | fn default_bookmark(&self, name: &str) -> Option<String> { |
| 437 | 146× | self.open(name).ok().map(|r| r.config.bookmark.clone()) |
| 438 | 146× | } |
| 439 | |
|
| 440 | 369× | fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> { |
| 441 | 369× | if access == Access::Read || self.tokens.is_empty() { |
| 442 | 363× | return Ok(()); |
| 443 | 6× | } |
| 444 | 6× | match bearer_token(headers) { |
| 445 | 5× | Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()), |
| 446 | 4× | _ => Err(Failure( |
| 447 | 4× | StatusCode::UNAUTHORIZED, |
| 448 | 4× | "a valid bearer token is required to push".into(), |
| 449 | 4× | )), |
| 450 | | } |
| 451 | 369× | } |
| 452 | | } |
| 453 | |
|
| 454 | 391× | fn hex_id(h: &str) -> Option<[u8; 16]> { |
| 455 | 391× | let h = h.trim(); |
| 456 | 391× | if h.len() != 32 { |
| 457 | 0× | return None; |
| 458 | 391× | } |
| 459 | 391× | let mut out = [0u8; 16]; |
| 460 | 6256× | for (i, byte) in out.iter_mut().enumerate() { |
| 461 | 6256× | *byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?; |
| 462 | | } |
| 463 | 391× | Some(out) |
| 464 | 391× | } |
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | 139× | pub fn client_features(headers: &HeaderMap) -> Option<&str> { |
| 469 | 139× | headers |
| 470 | 139× | .get(oxy_core::protocol::FEATURES_HEADER) |
| 471 | 139× | .and_then(|v| v.to_str().ok()) |
| 472 | 139× | } |
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | 139× | pub fn tailor_refs(repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) { |
| 477 | 139× | repo.refs_for_client(refs, client_features(headers)); |
| 478 | 139× | } |
| 479 | |
|
| 480 | |
|
| 481 | 19× | pub fn bearer_token(headers: &HeaderMap) -> Option<&str> { |
| 482 | 19× | headers |
| 483 | 19× | .get(header::AUTHORIZATION) |
| 484 | 19× | .and_then(|v| v.to_str().ok()) |
| 485 | 19× | .and_then(|v| v.strip_prefix("Bearer ")) |
| 486 | 19× | .map(str::trim) |
| 487 | 19× | } |
| 488 | |
|
| 489 | 363× | async fn dispatch( |
| 490 | 363× | State(server): State<Arc<Server>>, |
| 491 | 363× | Path((repo, endpoint)): Path<(String, String)>, |
| 492 | 363× | method: Method, |
| 493 | 363× | headers: HeaderMap, |
| 494 | 363× | body: Bytes, |
| 495 | 363× | ) -> Response { |
| 496 | 363× | match Endpoint::parse(&endpoint) { |
| 497 | 363× | Some(e) => server.handle(repo, e, &method, &headers, body).await, |
| 498 | 0× | None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(), |
| 499 | | } |
| 500 | 363× | } |
| 501 | |
|
| 502 | |
|
| 503 | 12× | pub fn router(cfg: ServerConfig) -> Router { |
| 504 | 12× | let server = Server::new(DirBackend { |
| 505 | 12× | root: cfg.root, |
| 506 | 12× | tokens: cfg.tokens, |
| 507 | 12× | }); |
| 508 | 12× | Router::new() |
| 509 | 12× | .route("/{repo}/oxy/v1/{endpoint}", any(dispatch)) |
| 510 | 12× | .with_state(server) |
| 511 | 12× | } |
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | 12× | pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 516 | 12× | listener.set_nonblocking(true)?; |
| 517 | 12× | match cfg.tls.clone() { |
| 518 | 1× | Some(tls) => { |
| 519 | 1× | let _ = rustls::crypto::ring::default_provider().install_default(); |
| 520 | 1× | let rustls_cfg = |
| 521 | 1× | axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key) |
| 522 | 1× | .await |
| 523 | 1× | .map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?; |
| 524 | 1× | axum_server::from_tcp_rustls(listener, rustls_cfg)? |
| 525 | 1× | .serve(router(cfg).into_make_service()) |
| 526 | 1× | .await |
| 527 | | } |
| 528 | | None => { |
| 529 | 11× | let listener = tokio::net::TcpListener::from_std(listener)?; |
| 530 | 11× | axum::serve(listener, router(cfg)).await |
| 531 | | } |
| 532 | | } |
| 533 | 0× | } |
| 534 | |
|
| 535 | |
|
| 536 | 12× | pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 537 | 12× | let rt = tokio::runtime::Builder::new_multi_thread() |
| 538 | 12× | .enable_all() |
| 539 | 12× | .build()?; |
| 540 | 12× | rt.block_on(serve(listener, cfg)) |
| 541 | 12× | } |