use http::{Request, Response};
use http_body_util::BodyExt;
use tonic::body::Body as TonicBody;
use tonic::service::Routes;
use tonic::transport::Channel;
use tower::ServiceExt;
use crate::{BoxError, ResBody};
#[derive(Clone)]
pub enum Backend {
InProcess(Routes),
Upstream(Channel),
}
impl Backend {
pub async fn call(&self, req: Request<TonicBody>) -> Result<Response<ResBody>, BoxError> {
match self {
Backend::InProcess(routes) => {
let resp = routes.clone().oneshot(req).await.unwrap_or_else(|e| match e {});
Ok(resp.map(|b| b.map_err(Into::into).boxed_unsync()))
}
Backend::Upstream(channel) => {
let resp = channel.clone().oneshot(req).await.map_err(|e| Box::new(e) as BoxError)?;
Ok(resp.map(|b| b.map_err(Into::into).boxed_unsync()))
}
}
}
pub fn is_upstream(&self) -> bool {
matches!(self, Backend::Upstream(_))
}
}