Skip to content

Add x_restate_server header #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bytes::Bytes;
use futures::future::BoxFuture;
use futures::{FutureExt, TryStreamExt};
use http::header::CONTENT_TYPE;
use http::{response, Request, Response};
use http::{response, HeaderName, HeaderValue, Request, Response};
use http_body_util::{BodyExt, Either, Full};
use hyper::body::{Body, Frame, Incoming};
use hyper::service::Service;
Expand All @@ -17,7 +17,12 @@ use std::ops::Deref;
use std::pin::Pin;
use std::task::{ready, Context, Poll};
use tokio::sync::mpsc;
use tracing::warn;
use tracing::{debug, warn};

#[allow(clippy::declare_interior_mutable_const)]
const X_RESTATE_SERVER: HeaderName = HeaderName::from_static("x-restate-server");
const X_RESTATE_SERVER_VALUE: HeaderValue =
HeaderValue::from_static(concat!("restate-sdk-rust/", env!("CARGO_PKG_VERSION")));

/// Wraps [`Endpoint`] to implement hyper [`Service`].
#[derive(Clone)]
Expand All @@ -39,10 +44,11 @@ impl Service<Request<Incoming>> for HyperEndpoint {
let endpoint_response = match self.0.resolve(parts.uri.path(), parts.headers) {
Ok(res) => res,
Err(err) => {
// TODO log this
debug!("Error when trying to handle incoming request: {err}");
return ready(Ok(Response::builder()
.status(err.status_code())
.header(CONTENT_TYPE, "text/plain")
.header(X_RESTATE_SERVER, X_RESTATE_SERVER_VALUE)
.body(Either::Left(Full::new(Bytes::from(err.to_string()))))
.expect("Headers should be valid")));
}
Expand Down Expand Up @@ -80,7 +86,9 @@ impl Service<Request<Incoming>> for HyperEndpoint {
}

fn response_builder_from_response_head(response_head: ResponseHead) -> response::Builder {
let mut response_builder = Response::builder().status(response_head.status_code);
let mut response_builder = Response::builder()
.status(response_head.status_code)
.header(X_RESTATE_SERVER, X_RESTATE_SERVER_VALUE);

for header in response_head.headers {
response_builder = response_builder.header(header.key.deref(), header.value.deref());
Expand Down