Skip to content

Commit 89a9e79

Browse files
aram356claude
andcommitted
Add X-TS-Version and X-TS-Env response headers for staging identification
Expose FASTLY_SERVICE_VERSION as X-TS-Version on every response, and set X-TS-Env: staging when FASTLY_IS_STAGING=1. This makes it trivial to tell staging from production via curl or browser DevTools. Adds constants for both the Fastly env vars and the header names. Closes #325 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 775dc72 commit 89a9e79

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

crates/common/src/constants.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ pub const HEADER_X_REQUEST_ID: HeaderName = HeaderName::from_static("x-request-i
1919
pub const HEADER_X_COMPRESS_HINT: HeaderName = HeaderName::from_static("x-compress-hint");
2020
pub const HEADER_X_DEBUG_FASTLY_POP: HeaderName = HeaderName::from_static("x-debug-fastly-pop");
2121

22+
// Staging / version identification headers
23+
pub const HEADER_X_TS_VERSION: HeaderName = HeaderName::from_static("x-ts-version");
24+
pub const HEADER_X_TS_ENV: HeaderName = HeaderName::from_static("x-ts-env");
25+
26+
// Fastly environment variables
27+
pub const ENV_FASTLY_SERVICE_VERSION: &str = "FASTLY_SERVICE_VERSION";
28+
pub const ENV_FASTLY_IS_STAGING: &str = "FASTLY_IS_STAGING";
29+
2230
// Common standard header names used across modules
2331
pub const HEADER_USER_AGENT: HeaderName = HeaderName::from_static("user-agent");
2432
pub const HEADER_ACCEPT: HeaderName = HeaderName::from_static("accept");

crates/fastly/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use log_fastly::Logger;
66
use trusted_server_common::auction::endpoints::handle_auction;
77
use trusted_server_common::auction::{build_orchestrator, AuctionOrchestrator};
88
use trusted_server_common::auth::enforce_basic_auth;
9+
use trusted_server_common::constants::{
10+
ENV_FASTLY_IS_STAGING, ENV_FASTLY_SERVICE_VERSION, HEADER_X_TS_ENV, HEADER_X_TS_VERSION,
11+
};
912
use trusted_server_common::error::TrustedServerError;
1013
use trusted_server_common::integrations::IntegrationRegistry;
1114
use trusted_server_common::proxy::{
@@ -63,7 +66,7 @@ async fn route_request(
6366
) -> Result<Response, Error> {
6467
log::info!(
6568
"FASTLY_SERVICE_VERSION: {}",
66-
::std::env::var("FASTLY_SERVICE_VERSION").unwrap_or_else(|_| String::new())
69+
::std::env::var(ENV_FASTLY_SERVICE_VERSION).unwrap_or_else(|_| String::new())
6770
);
6871

6972
if let Some(response) = enforce_basic_auth(settings, &req) {
@@ -132,6 +135,13 @@ async fn route_request(
132135
// Convert any errors to HTTP error responses
133136
let mut response = result.unwrap_or_else(|e| to_error_response(&e));
134137

138+
if let Ok(v) = ::std::env::var(ENV_FASTLY_SERVICE_VERSION) {
139+
response.set_header(HEADER_X_TS_VERSION, v);
140+
}
141+
if ::std::env::var(ENV_FASTLY_IS_STAGING).as_deref() == Ok("1") {
142+
response.set_header(HEADER_X_TS_ENV, "staging");
143+
}
144+
135145
for (key, value) in &settings.response_headers {
136146
response.set_header(key, value);
137147
}

0 commit comments

Comments
 (0)