refactor handler_main
This commit is contained in:
parent
a412eb08d6
commit
2705db6b91
2 changed files with 27 additions and 33 deletions
|
|
@ -180,9 +180,5 @@ pub async fn entrypoint(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// returns the first error as the representative error
|
// returns the first error as the representative error
|
||||||
if let Some(e) = errs.next() {
|
errs.next().map_or(Ok(()), |e| Err(e))
|
||||||
return Err(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,9 +107,11 @@ where
|
||||||
let backend_app = match self.app_manager.apps.get(&server_name) {
|
let backend_app = match self.app_manager.apps.get(&server_name) {
|
||||||
Some(backend_app) => backend_app,
|
Some(backend_app) => backend_app,
|
||||||
None => {
|
None => {
|
||||||
let Some(default_server_name) = &self.app_manager.default_server_name else {
|
let default_server_name = self
|
||||||
return Err(HttpError::NoMatchingBackendApp);
|
.app_manager
|
||||||
};
|
.default_server_name
|
||||||
|
.as_ref()
|
||||||
|
.ok_or(HttpError::NoMatchingBackendApp)?;
|
||||||
debug!("Serving by default app");
|
debug!("Serving by default app");
|
||||||
self.app_manager.apps.get(default_server_name).unwrap()
|
self.app_manager.apps.get(default_server_name).unwrap()
|
||||||
}
|
}
|
||||||
|
|
@ -131,9 +133,7 @@ where
|
||||||
// Find reverse proxy for given path and choose one of upstream host
|
// Find reverse proxy for given path and choose one of upstream host
|
||||||
// Longest prefix match
|
// Longest prefix match
|
||||||
let path = req.uri().path();
|
let path = req.uri().path();
|
||||||
let Some(upstream_candidates) = backend_app.path_manager.get(path) else {
|
let upstream_candidates = backend_app.path_manager.get(path).ok_or(HttpError::NoUpstreamCandidates)?;
|
||||||
return Err(HttpError::NoUpstreamCandidates);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Upgrade in request header
|
// Upgrade in request header
|
||||||
let upgrade_in_request = extract_upgrade(req.headers());
|
let upgrade_in_request = extract_upgrade(req.headers());
|
||||||
|
|
@ -147,19 +147,17 @@ where
|
||||||
let req_on_upgrade = hyper::upgrade::on(&mut req);
|
let req_on_upgrade = hyper::upgrade::on(&mut req);
|
||||||
|
|
||||||
// Build request from destination information
|
// Build request from destination information
|
||||||
let _context = match self.generate_request_forwarded(
|
let _context = self
|
||||||
&client_addr,
|
.generate_request_forwarded(
|
||||||
&listen_addr,
|
&client_addr,
|
||||||
&mut req,
|
&listen_addr,
|
||||||
&upgrade_in_request,
|
&mut req,
|
||||||
upstream_candidates,
|
&upgrade_in_request,
|
||||||
tls_enabled,
|
upstream_candidates,
|
||||||
) {
|
tls_enabled,
|
||||||
Err(e) => {
|
)
|
||||||
return Err(HttpError::FailedToGenerateUpstreamRequest(e.to_string()));
|
.map_err(|e| HttpError::FailedToGenerateUpstreamRequest(e.to_string()))?;
|
||||||
}
|
|
||||||
Ok(v) => v,
|
|
||||||
};
|
|
||||||
debug!(
|
debug!(
|
||||||
"Request to be forwarded: [uri {}, method: {}, version {:?}, headers {:?}]",
|
"Request to be forwarded: [uri {}, method: {}, version {:?}, headers {:?}]",
|
||||||
req.uri(),
|
req.uri(),
|
||||||
|
|
@ -173,12 +171,12 @@ where
|
||||||
|
|
||||||
//////////////
|
//////////////
|
||||||
// Forward request to a chosen backend
|
// Forward request to a chosen backend
|
||||||
let mut res_backend = match self.forwarder.request(req).await {
|
let mut res_backend = self
|
||||||
Ok(v) => v,
|
.forwarder
|
||||||
Err(e) => {
|
.request(req)
|
||||||
return Err(HttpError::FailedToGetResponseFromBackend(e.to_string()));
|
.await
|
||||||
}
|
.map_err(|e| HttpError::FailedToGetResponseFromBackend(e.to_string()))?;
|
||||||
};
|
|
||||||
//////////////
|
//////////////
|
||||||
// Process reverse proxy context generated during the forwarding request generation.
|
// Process reverse proxy context generated during the forwarding request generation.
|
||||||
#[cfg(feature = "sticky-cookie")]
|
#[cfg(feature = "sticky-cookie")]
|
||||||
|
|
@ -191,9 +189,9 @@ where
|
||||||
|
|
||||||
if res_backend.status() != StatusCode::SWITCHING_PROTOCOLS {
|
if res_backend.status() != StatusCode::SWITCHING_PROTOCOLS {
|
||||||
// Generate response to client
|
// Generate response to client
|
||||||
if let Err(e) = self.generate_response_forwarded(&mut res_backend, backend_app) {
|
self
|
||||||
return Err(HttpError::FailedToGenerateDownstreamResponse(e.to_string()));
|
.generate_response_forwarded(&mut res_backend, backend_app)
|
||||||
}
|
.map_err(|e| HttpError::FailedToGenerateDownstreamResponse(e.to_string()))?;
|
||||||
return Ok(res_backend);
|
return Ok(res_backend);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue