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