Skip to content

Commit 47cf4ca

Browse files
committed
fix: add user-agent response
1 parent c5b8d59 commit 47cf4ca

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

api/src/interactions/mod.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use axum::routing::post;
66
use axum::{Json, middleware};
77
use ed25519_dalek::{PUBLIC_KEY_LENGTH, Verifier, VerifyingKey};
88
use hex::FromHex;
9-
use http::{HeaderMap, StatusCode};
9+
use http::{HeaderMap, HeaderValue, StatusCode};
1010
use http_body_util::BodyExt;
1111
use lambda_http::tracing::error;
1212
use once_cell::sync::Lazy;
@@ -32,6 +32,7 @@ pub fn router() -> axum::Router<AppState> {
3232
axum::Router::new()
3333
.route("/", post(post_interactions))
3434
.route_layer(middleware::from_fn(pubkey_middleware))
35+
.route_layer(middleware::from_fn(user_agent_response_middleware))
3536
.route("/register", post(register_commands))
3637
}
3738

@@ -75,6 +76,25 @@ pub async fn pubkey_middleware(request: Request, next: Next) -> Result<Response,
7576
Ok(next.run(new_request).await)
7677
}
7778

79+
pub async fn user_agent_response_middleware(
80+
request: Request,
81+
next: Next,
82+
) -> Result<Response, StatusCode> {
83+
let mut response = next.run(request).await;
84+
85+
let user_agent = response
86+
.headers()
87+
.get("User-Agent")
88+
.and_then(|v| v.to_str().ok())
89+
.unwrap_or("Unknown");
90+
91+
if response.headers().get("User-Agent").is_none() {
92+
let full_version = env!("CARGO_PKG_VERSION");
93+
response.headers_mut().insert("User-Agent", format!("KoalaBot/{} (+{})",full_version, get_url()).parse().unwrap());
94+
}
95+
96+
Ok(response)}
97+
7898
async fn post_interactions(
7999
State(_app_state): State<AppState>,
80100
Json(interaction): Json<Interaction>,
@@ -128,7 +148,7 @@ async fn support() -> Result<Json<Value>, StatusCode> {
128148
}
129149

130150
fn get_url() -> String {
131-
match std::env::var("DEPLOYMENT_ENV").expect("DEPLOYMENT_ENV must be set").as_str() {
151+
match std::env::var("DEPLOYMENT_ENV").unwrap_or("prod".into()).as_str() {
132152
"prod" => "https://koalabot.uk".to_owned(),
133153
env => format!("https://{env}.koalabot.uk").to_owned()
134154
}

0 commit comments

Comments
 (0)