-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathendpoint_builder.rs
More file actions
448 lines (379 loc) · 15.6 KB
/
Copy pathendpoint_builder.rs
File metadata and controls
448 lines (379 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Service endpoint builder utilities
//!
//! This module provides utilities for constructing `ServiceEndpoint` objects
//! from domain configuration types and runtime deployment data (like instance IPs).
//!
//! These builders are used by command handlers that need to create endpoints for
//! external validation, testing, or other operations that require service URLs.
//!
//! ## Design Rationale
//!
//! These functions exist in the application layer (not domain) because:
//! - They combine domain configuration with runtime deployment state
//! - Domain configuration types shouldn't know about deployment IPs
//! - Multiple command handlers may need to build endpoints
//! - They translate domain types → infrastructure types (`ServiceEndpoint`)
use std::net::{IpAddr, SocketAddr};
use crate::domain::tracker::config::{HttpApiConfig, HttpTrackerConfig, TrackerConfig};
use crate::shared::ServiceEndpoint;
/// Build a `ServiceEndpoint` for the HTTP API from configuration and instance IP
///
/// Creates either an HTTP or HTTPS endpoint depending on whether TLS is enabled
/// in the configuration. For TLS endpoints, the domain is used with the instance
/// IP for local resolution (no DNS dependency).
///
/// # Arguments
///
/// * `instance_ip` - The IP address of the deployed instance
/// * `config` - The HTTP API configuration containing port and TLS settings
///
/// # Returns
///
/// A `ServiceEndpoint` configured for the HTTP API health check endpoint.
///
/// # Panics
///
/// Panics if the configuration produces an invalid URL (this should never happen
/// with valid configuration types from the domain layer).
#[must_use]
pub fn build_api_endpoint(instance_ip: IpAddr, config: &HttpApiConfig) -> ServiceEndpoint {
let port = config.bind_address().port();
let path = "/api/health_check";
let socket_addr = SocketAddr::new(instance_ip, port);
if let Some(domain) = config.tls_domain() {
ServiceEndpoint::https(domain, path, instance_ip)
.expect("Valid TLS domain should produce valid HTTPS URL")
} else {
ServiceEndpoint::http(socket_addr, path)
.expect("Valid socket address should produce valid HTTP URL")
}
}
/// Build a `ServiceEndpoint` for an HTTP Tracker from configuration and instance IP
///
/// Creates either an HTTP or HTTPS endpoint depending on whether TLS is enabled
/// in the configuration. For TLS endpoints, the domain is used with the instance
/// IP for local resolution (no DNS dependency).
///
/// # Arguments
///
/// * `instance_ip` - The IP address of the deployed instance
/// * `config` - The HTTP Tracker configuration containing port and TLS settings
///
/// # Returns
///
/// A `ServiceEndpoint` configured for the HTTP Tracker health check endpoint.
///
/// # Panics
///
/// Panics if the configuration produces an invalid URL (this should never happen
/// with valid configuration types from the domain layer).
#[must_use]
pub fn build_http_tracker_endpoint(
instance_ip: IpAddr,
config: &HttpTrackerConfig,
) -> ServiceEndpoint {
let port = config.bind_address().port();
let path = "/health_check";
let socket_addr = SocketAddr::new(instance_ip, port);
if let Some(domain) = config.tls_domain() {
ServiceEndpoint::https(domain, path, instance_ip)
.expect("Valid TLS domain should produce valid HTTPS URL")
} else {
ServiceEndpoint::http(socket_addr, path)
.expect("Valid socket address should produce valid HTTP URL")
}
}
/// Build all tracker service endpoints from configuration and instance IP
///
/// This is a convenience function that builds both the HTTP API endpoint and
/// all HTTP Tracker endpoints in a single call. It's the recommended way to
/// construct endpoints when you need all tracker services.
///
/// # Arguments
///
/// * `instance_ip` - The IP address of the deployed instance
/// * `tracker_config` - The complete tracker configuration
///
/// # Returns
///
/// A tuple containing:
/// - The HTTP API `ServiceEndpoint`
/// - A vector of HTTP Tracker `ServiceEndpoint`s (one per configured tracker)
///
/// # Panics
///
/// Panics if any configuration produces an invalid URL (this should never happen
/// with valid configuration types from the domain layer).
#[must_use]
pub fn build_all_tracker_endpoints(
instance_ip: IpAddr,
tracker_config: &TrackerConfig,
) -> (ServiceEndpoint, Vec<ServiceEndpoint>) {
let api_endpoint = build_api_endpoint(instance_ip, tracker_config.http_api());
let http_tracker_endpoints = tracker_config
.http_trackers()
.iter()
.map(|config| build_http_tracker_endpoint(instance_ip, config))
.collect();
(api_endpoint, http_tracker_endpoints)
}
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr};
use super::*;
use crate::domain::tracker::config::{
DatabaseConfig, HealthCheckApiConfig, HttpApiConfig, HttpTrackerConfig, SqliteConfig,
TrackerConfig, TrackerCoreConfig, UdpTrackerConfig,
};
use crate::shared::{ApiToken, DomainName};
// Test fixtures
fn test_ip() -> IpAddr {
IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))
}
fn http_api_config_without_tls() -> HttpApiConfig {
HttpApiConfig::new(
"0.0.0.0:1212".parse().unwrap(),
ApiToken::from("test_token".to_string()),
None,
false,
)
.expect("valid config")
}
fn http_api_config_with_tls() -> HttpApiConfig {
HttpApiConfig::new(
"0.0.0.0:1212".parse().unwrap(),
ApiToken::from("test_token".to_string()),
Some(DomainName::new("api.tracker.local").unwrap()),
true,
)
.expect("valid config")
}
fn http_tracker_config_without_tls() -> HttpTrackerConfig {
HttpTrackerConfig::new("0.0.0.0:7070".parse().unwrap(), None, false).expect("valid config")
}
fn http_tracker_config_with_tls() -> HttpTrackerConfig {
HttpTrackerConfig::new(
"0.0.0.0:7070".parse().unwrap(),
Some(DomainName::new("tracker.example.com").unwrap()),
true,
)
.expect("valid config")
}
fn tracker_config_with_one_http_tracker() -> TrackerConfig {
TrackerConfig::new(
TrackerCoreConfig::new(
DatabaseConfig::Sqlite(SqliteConfig::new("tracker.db").unwrap()),
false,
),
vec![UdpTrackerConfig::new("0.0.0.0:6969".parse().unwrap(), None).unwrap()],
vec![http_tracker_config_without_tls()],
http_api_config_without_tls(),
HealthCheckApiConfig::new("127.0.0.1:1313".parse().unwrap(), None, false).unwrap(),
)
.expect("valid config")
}
fn tracker_config_with_multiple_http_trackers() -> TrackerConfig {
TrackerConfig::new(
TrackerCoreConfig::new(
DatabaseConfig::Sqlite(SqliteConfig::new("tracker.db").unwrap()),
false,
),
vec![],
vec![
HttpTrackerConfig::new("0.0.0.0:7070".parse().unwrap(), None, false)
.expect("valid config"),
HttpTrackerConfig::new("0.0.0.0:8080".parse().unwrap(), None, false)
.expect("valid config"),
HttpTrackerConfig::new(
"0.0.0.0:9090".parse().unwrap(),
Some(DomainName::new("tracker.example.com").unwrap()),
true,
)
.expect("valid config"),
],
http_api_config_with_tls(),
HealthCheckApiConfig::new("127.0.0.1:1313".parse().unwrap(), None, false).unwrap(),
)
.expect("valid config")
}
// Tests for build_api_endpoint
#[test]
fn it_should_build_http_api_endpoint_when_tls_is_disabled() {
let config = http_api_config_without_tls();
let endpoint = build_api_endpoint(test_ip(), &config);
assert!(!endpoint.uses_tls());
assert_eq!(endpoint.server_ip(), test_ip());
assert_eq!(endpoint.port(), 1212);
assert_eq!(
endpoint.url().as_str(),
"http://10.0.0.1:1212/api/health_check" // DevSkim: ignore DS137138
);
}
#[test]
fn it_should_build_https_api_endpoint_when_tls_is_enabled() {
let config = http_api_config_with_tls();
let endpoint = build_api_endpoint(test_ip(), &config);
assert!(endpoint.uses_tls());
assert_eq!(endpoint.server_ip(), test_ip());
assert_eq!(endpoint.port(), 443);
assert_eq!(endpoint.domain(), Some("api.tracker.local"));
assert_eq!(
endpoint.url().as_str(),
"https://api.tracker.local/api/health_check"
);
}
#[test]
fn it_should_use_correct_path_when_building_api_endpoint() {
let config = http_api_config_without_tls();
let endpoint = build_api_endpoint(test_ip(), &config);
assert_eq!(endpoint.url().path(), "/api/health_check");
}
#[test]
fn it_should_extract_port_from_config_when_building_api_endpoint() {
let config = HttpApiConfig::new(
"0.0.0.0:9999".parse().unwrap(),
ApiToken::from("test".to_string()),
None,
false,
)
.expect("valid config");
let endpoint = build_api_endpoint(test_ip(), &config);
assert_eq!(endpoint.port(), 9999);
}
#[test]
fn it_should_use_instance_ip_when_building_api_endpoint() {
let different_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100));
let config = http_api_config_without_tls();
let endpoint = build_api_endpoint(different_ip, &config);
assert_eq!(endpoint.server_ip(), different_ip);
}
// Tests for build_http_tracker_endpoint
#[test]
fn it_should_build_http_tracker_endpoint_when_tls_is_disabled() {
let config = http_tracker_config_without_tls();
let endpoint = build_http_tracker_endpoint(test_ip(), &config);
assert!(!endpoint.uses_tls());
assert_eq!(endpoint.server_ip(), test_ip());
assert_eq!(endpoint.port(), 7070);
assert_eq!(
endpoint.url().as_str(),
"http://10.0.0.1:7070/health_check" // DevSkim: ignore DS137138
);
}
#[test]
fn it_should_build_https_tracker_endpoint_when_tls_is_enabled() {
let config = http_tracker_config_with_tls();
let endpoint = build_http_tracker_endpoint(test_ip(), &config);
assert!(endpoint.uses_tls());
assert_eq!(endpoint.server_ip(), test_ip());
assert_eq!(endpoint.port(), 443);
assert_eq!(endpoint.domain(), Some("tracker.example.com"));
assert_eq!(
endpoint.url().as_str(),
"https://tracker.example.com/health_check"
);
}
#[test]
fn it_should_use_correct_path_when_building_tracker_endpoint() {
let config = http_tracker_config_without_tls();
let endpoint = build_http_tracker_endpoint(test_ip(), &config);
assert_eq!(endpoint.url().path(), "/health_check");
}
#[test]
fn it_should_extract_port_from_config_when_building_tracker_endpoint() {
let config = HttpTrackerConfig::new("0.0.0.0:8888".parse().unwrap(), None, false)
.expect("valid config");
let endpoint = build_http_tracker_endpoint(test_ip(), &config);
assert_eq!(endpoint.port(), 8888);
}
#[test]
fn it_should_use_instance_ip_when_building_tracker_endpoint() {
let different_ip = IpAddr::V4(Ipv4Addr::new(172, 16, 0, 50));
let config = http_tracker_config_without_tls();
let endpoint = build_http_tracker_endpoint(different_ip, &config);
assert_eq!(endpoint.server_ip(), different_ip);
}
// Tests for build_all_tracker_endpoints
#[test]
fn it_should_build_api_and_tracker_endpoints_when_given_tracker_config() {
let config = tracker_config_with_one_http_tracker();
let (api_endpoint, tracker_endpoints) = build_all_tracker_endpoints(test_ip(), &config);
// Verify API endpoint
assert!(!api_endpoint.uses_tls());
assert_eq!(api_endpoint.port(), 1212);
assert_eq!(api_endpoint.url().path(), "/api/health_check");
// Verify tracker endpoints
assert_eq!(tracker_endpoints.len(), 1);
assert!(!tracker_endpoints[0].uses_tls());
assert_eq!(tracker_endpoints[0].port(), 7070);
assert_eq!(tracker_endpoints[0].url().path(), "/health_check");
}
#[test]
fn it_should_build_multiple_tracker_endpoints_when_multiple_trackers_configured() {
let config = tracker_config_with_multiple_http_trackers();
let (_api_endpoint, tracker_endpoints) = build_all_tracker_endpoints(test_ip(), &config);
assert_eq!(tracker_endpoints.len(), 3);
// First tracker (HTTP on 7070)
assert!(!tracker_endpoints[0].uses_tls());
assert_eq!(tracker_endpoints[0].port(), 7070);
// Second tracker (HTTP on 8080)
assert!(!tracker_endpoints[1].uses_tls());
assert_eq!(tracker_endpoints[1].port(), 8080);
// Third tracker (HTTPS on 9090 -> 443)
assert!(tracker_endpoints[2].uses_tls());
assert_eq!(tracker_endpoints[2].port(), 443);
assert_eq!(tracker_endpoints[2].domain(), Some("tracker.example.com"));
}
#[test]
fn it_should_build_tls_api_endpoint_when_tracker_config_has_tls_enabled() {
let config = tracker_config_with_multiple_http_trackers();
let (api_endpoint, _tracker_endpoints) = build_all_tracker_endpoints(test_ip(), &config);
assert!(api_endpoint.uses_tls());
assert_eq!(api_endpoint.domain(), Some("api.tracker.local"));
assert_eq!(
api_endpoint.url().as_str(),
"https://api.tracker.local/api/health_check"
);
}
#[test]
fn it_should_return_empty_tracker_list_when_no_http_trackers_configured() {
let config = TrackerConfig::new(
TrackerCoreConfig::new(
DatabaseConfig::Sqlite(SqliteConfig::new("tracker.db").unwrap()),
false,
),
vec![UdpTrackerConfig::new("0.0.0.0:6969".parse().unwrap(), None).unwrap()],
vec![], // No HTTP trackers
http_api_config_without_tls(),
HealthCheckApiConfig::new("127.0.0.1:1313".parse().unwrap(), None, false).unwrap(),
)
.expect("valid config");
let (_api_endpoint, tracker_endpoints) = build_all_tracker_endpoints(test_ip(), &config);
assert!(tracker_endpoints.is_empty());
}
#[test]
fn it_should_use_same_instance_ip_for_all_endpoints_when_building_all() {
let config = tracker_config_with_multiple_http_trackers();
let specific_ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 42));
let (api_endpoint, tracker_endpoints) = build_all_tracker_endpoints(specific_ip, &config);
// All endpoints should use the same instance IP
assert_eq!(api_endpoint.server_ip(), specific_ip);
for endpoint in &tracker_endpoints {
assert_eq!(endpoint.server_ip(), specific_ip);
}
}
#[test]
fn it_should_preserve_individual_port_configurations_when_building_all() {
let config = tracker_config_with_multiple_http_trackers();
let (_api_endpoint, tracker_endpoints) = build_all_tracker_endpoints(test_ip(), &config);
// Each tracker should preserve its configured port (or use 443 for HTTPS)
let ports: Vec<u16> = tracker_endpoints
.iter()
.map(ServiceEndpoint::port)
.collect();
// First two are HTTP (use configured ports), third is HTTPS (uses 443)
assert_eq!(ports[0], 7070);
assert_eq!(ports[1], 8080);
assert_eq!(ports[2], 443); // HTTPS default port
}
}