Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit 26b6f6a

Browse files
committed
different route based on api keys
1 parent befbd84 commit 26b6f6a

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

src/server.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,25 @@ impl Server {
5555
}
5656

5757
pub async fn listen(&self, cancellation_token: CancellationToken) {
58-
let router = Router::new()
59-
.route("/healthz", get(healthz_handler))
60-
.route("/ws", any(websocket_handler))
61-
.route("/ws/{api_key}", any(websocket_handler_with_key))
62-
.with_state(ServerState {
63-
registry: self.registry.clone(),
64-
rate_limiter: self.rate_limiter.clone(),
65-
metrics: self.metrics.clone(),
66-
ip_addr_http_header: self.ip_addr_http_header.clone(),
67-
api_keys: self.api_keys.clone(),
68-
});
58+
let server_state = ServerState {
59+
registry: self.registry.clone(),
60+
rate_limiter: self.rate_limiter.clone(),
61+
metrics: self.metrics.clone(),
62+
ip_addr_http_header: self.ip_addr_http_header.clone(),
63+
api_keys: self.api_keys.clone(),
64+
};
65+
66+
let router = if self.api_keys.is_empty() {
67+
Router::new()
68+
.route("/healthz", get(healthz_handler))
69+
.route("/ws", any(websocket_handler))
70+
.with_state(server_state)
71+
} else {
72+
Router::new()
73+
.route("/healthz", get(healthz_handler))
74+
.route("/ws/{api_key}", any(websocket_handler_with_key))
75+
.with_state(server_state)
76+
};
6977

7078
let listener = tokio::net::TcpListener::bind(self.listen_addr)
7179
.await
@@ -76,16 +84,6 @@ impl Server {
7684
address = listener.local_addr().unwrap().to_string()
7785
);
7886

79-
if self.api_keys.is_empty() {
80-
info!(message = "API key authentication is disabled");
81-
} else {
82-
info!(
83-
message = "API key authentication is enabled",
84-
key_count = self.api_keys.len()
85-
);
86-
info!(message = "API keys", keys = ?self.api_keys);
87-
}
88-
8987
axum::serve(
9088
listener,
9189
router.into_make_service_with_connect_info::<SocketAddr>(),
@@ -106,17 +104,6 @@ async fn websocket_handler(
106104
ConnectInfo(addr): ConnectInfo<SocketAddr>,
107105
headers: HeaderMap,
108106
) -> Response {
109-
// If API keys are required but none provided in URL path, reject
110-
if !state.api_keys.is_empty() {
111-
state.metrics.unauthorized_requests.increment(1);
112-
return Response::builder()
113-
.status(StatusCode::UNAUTHORIZED)
114-
.body(Body::from(
115-
json!({"message": "API key required"}).to_string(),
116-
))
117-
.unwrap();
118-
}
119-
120107
handle_websocket_connection(state, ws, addr, headers, None)
121108
}
122109

@@ -128,7 +115,7 @@ async fn websocket_handler_with_key(
128115
Path(api_key): Path<String>,
129116
) -> Response {
130117
// If API keys are required, validate the provided key
131-
if !state.api_keys.is_empty() && !state.api_keys.contains(&api_key) {
118+
if !state.api_keys.contains(&api_key) {
132119
state.metrics.unauthorized_requests.increment(1);
133120
return Response::builder()
134121
.status(StatusCode::UNAUTHORIZED)

0 commit comments

Comments
 (0)