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