Skip to content

Commit eca6dff

Browse files
committed
refactor: [#255] extract tracker config types into submodules
- Convert config.rs into folder module (config/) - Extract TrackerCoreConfig → config/core.rs (2 tests) - Extract UdpTrackerConfig → config/udp.rs (3 tests) - Extract HttpTrackerConfig → config/http.rs (3 tests) - Extract HttpApiConfig → config/http_api.rs (3 tests) - Extract HealthCheckApiConfig → config/health_check_api.rs (3 tests) - Keep TrackerConfig in config/mod.rs (3 integration tests) - Improve code organization and maintainability - All 1582 tests passing
1 parent ab84bbe commit eca6dff

6 files changed

Lines changed: 292 additions & 65 deletions

File tree

src/domain/tracker/config/core.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Core tracker configuration
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
use crate::domain::tracker::DatabaseConfig;
6+
7+
/// Core tracker configuration options
8+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9+
pub struct TrackerCoreConfig {
10+
/// Database configuration (`SQLite`, `MySQL`, etc.)
11+
pub database: DatabaseConfig,
12+
13+
/// Tracker mode: true for private tracker, false for public
14+
pub private: bool,
15+
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use super::*;
20+
use crate::domain::tracker::{DatabaseConfig, SqliteConfig};
21+
22+
#[test]
23+
fn it_should_create_core_config() {
24+
let core = TrackerCoreConfig {
25+
database: DatabaseConfig::Sqlite(SqliteConfig {
26+
database_name: "tracker.db".to_string(),
27+
}),
28+
private: true,
29+
};
30+
31+
assert_eq!(core.database.database_name(), "tracker.db");
32+
assert!(core.private);
33+
}
34+
35+
#[test]
36+
fn it_should_serialize_core_config() {
37+
let core = TrackerCoreConfig {
38+
database: DatabaseConfig::Sqlite(SqliteConfig {
39+
database_name: "test.db".to_string(),
40+
}),
41+
private: false,
42+
};
43+
44+
let json = serde_json::to_value(&core).unwrap();
45+
assert_eq!(json["private"], false);
46+
}
47+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//! Health Check API configuration
2+
3+
use std::net::SocketAddr;
4+
5+
use serde::{Deserialize, Serialize};
6+
7+
/// Health Check API configuration
8+
///
9+
/// The Health Check API is a minimal HTTP endpoint used by Docker and container
10+
/// orchestration tools to verify service health. It's separate from the main HTTP API.
11+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
12+
pub struct HealthCheckApiConfig {
13+
/// Bind address (e.g., "127.0.0.1:1313")
14+
///
15+
/// Conventionally uses port 1313, though this is configurable
16+
#[serde(
17+
serialize_with = "crate::domain::tracker::config::serialize_socket_addr",
18+
deserialize_with = "crate::domain::tracker::config::deserialize_socket_addr"
19+
)]
20+
pub bind_address: SocketAddr,
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn it_should_create_health_check_api_config() {
29+
let config = HealthCheckApiConfig {
30+
bind_address: "127.0.0.1:1313".parse().unwrap(),
31+
};
32+
33+
assert_eq!(
34+
config.bind_address,
35+
"127.0.0.1:1313".parse::<SocketAddr>().unwrap()
36+
);
37+
}
38+
39+
#[test]
40+
fn it_should_serialize_health_check_api_config() {
41+
let config = HealthCheckApiConfig {
42+
bind_address: "127.0.0.1:1313".parse().unwrap(),
43+
};
44+
45+
let json = serde_json::to_value(&config).unwrap();
46+
assert_eq!(json["bind_address"], "127.0.0.1:1313");
47+
}
48+
49+
#[test]
50+
fn it_should_deserialize_health_check_api_config() {
51+
let json = r#"{"bind_address": "127.0.0.1:1313"}"#;
52+
let config: HealthCheckApiConfig = serde_json::from_str(json).unwrap();
53+
54+
assert_eq!(
55+
config.bind_address,
56+
"127.0.0.1:1313".parse::<SocketAddr>().unwrap()
57+
);
58+
}
59+
}

src/domain/tracker/config/http.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! HTTP tracker configuration
2+
3+
use std::net::SocketAddr;
4+
5+
use serde::{Deserialize, Serialize};
6+
7+
/// HTTP tracker bind configuration
8+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9+
pub struct HttpTrackerConfig {
10+
/// Bind address (e.g., "0.0.0.0:7070")
11+
#[serde(
12+
serialize_with = "crate::domain::tracker::config::serialize_socket_addr",
13+
deserialize_with = "crate::domain::tracker::config::deserialize_socket_addr"
14+
)]
15+
pub bind_address: SocketAddr,
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::*;
21+
22+
#[test]
23+
fn it_should_create_http_tracker_config() {
24+
let config = HttpTrackerConfig {
25+
bind_address: "0.0.0.0:7070".parse().unwrap(),
26+
};
27+
28+
assert_eq!(
29+
config.bind_address,
30+
"0.0.0.0:7070".parse::<SocketAddr>().unwrap()
31+
);
32+
}
33+
34+
#[test]
35+
fn it_should_serialize_http_tracker_config() {
36+
let json = serde_json::to_value(&HttpTrackerConfig {
37+
bind_address: "0.0.0.0:7070".parse().unwrap(),
38+
})
39+
.unwrap();
40+
41+
assert_eq!(json["bind_address"], "0.0.0.0:7070");
42+
}
43+
44+
#[test]
45+
fn it_should_deserialize_http_tracker_config() {
46+
let json = r#"{"bind_address": "0.0.0.0:7070"}"#;
47+
let config: HttpTrackerConfig = serde_json::from_str(json).unwrap();
48+
49+
assert_eq!(
50+
config.bind_address,
51+
"0.0.0.0:7070".parse::<SocketAddr>().unwrap()
52+
);
53+
}
54+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//! HTTP API configuration
2+
3+
use std::net::SocketAddr;
4+
5+
use serde::{Deserialize, Serialize};
6+
7+
use crate::shared::ApiToken;
8+
9+
/// HTTP API configuration
10+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
11+
pub struct HttpApiConfig {
12+
/// Bind address (e.g., "0.0.0.0:1212")
13+
#[serde(
14+
serialize_with = "crate::domain::tracker::config::serialize_socket_addr",
15+
deserialize_with = "crate::domain::tracker::config::deserialize_socket_addr"
16+
)]
17+
pub bind_address: SocketAddr,
18+
19+
/// Admin access token for HTTP API authentication
20+
pub admin_token: ApiToken,
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn it_should_create_http_api_config() {
29+
let config = HttpApiConfig {
30+
bind_address: "0.0.0.0:1212".parse().unwrap(),
31+
admin_token: "test_token".to_string().into(),
32+
};
33+
34+
assert_eq!(
35+
config.bind_address,
36+
"0.0.0.0:1212".parse::<SocketAddr>().unwrap()
37+
);
38+
assert_eq!(config.admin_token.expose_secret(), "test_token");
39+
}
40+
41+
#[test]
42+
fn it_should_serialize_http_api_config() {
43+
let config = HttpApiConfig {
44+
bind_address: "0.0.0.0:1212".parse().unwrap(),
45+
admin_token: "token123".to_string().into(),
46+
};
47+
48+
let json = serde_json::to_value(&config).unwrap();
49+
assert_eq!(json["bind_address"], "0.0.0.0:1212");
50+
assert_eq!(json["admin_token"], "token123");
51+
}
52+
53+
#[test]
54+
fn it_should_deserialize_http_api_config() {
55+
let json = r#"{"bind_address": "0.0.0.0:1212", "admin_token": "MyToken"}"#;
56+
let config: HttpApiConfig = serde_json::from_str(json).unwrap();
57+
58+
assert_eq!(
59+
config.bind_address,
60+
"0.0.0.0:1212".parse::<SocketAddr>().unwrap()
61+
);
62+
assert_eq!(config.admin_token.expose_secret(), "MyToken");
63+
}
64+
}
Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@ use std::net::SocketAddr;
77

88
use serde::{Deserialize, Serialize};
99

10+
mod core;
11+
mod health_check_api;
12+
mod http;
13+
mod http_api;
14+
mod udp;
15+
16+
pub use core::TrackerCoreConfig;
17+
pub use health_check_api::HealthCheckApiConfig;
18+
pub use http::HttpTrackerConfig;
19+
pub use http_api::HttpApiConfig;
20+
pub use udp::UdpTrackerConfig;
21+
1022
use super::{DatabaseConfig, SqliteConfig};
11-
use crate::shared::ApiToken;
1223

1324
/// Tracker deployment configuration
1425
///
@@ -63,68 +74,6 @@ pub struct TrackerConfig {
6374
pub health_check_api: HealthCheckApiConfig,
6475
}
6576

66-
/// Core tracker configuration options
67-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
68-
pub struct TrackerCoreConfig {
69-
/// Database configuration (`SQLite`, `MySQL`, etc.)
70-
pub database: DatabaseConfig,
71-
72-
/// Tracker mode: true for private tracker, false for public
73-
pub private: bool,
74-
}
75-
76-
/// UDP tracker bind configuration
77-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
78-
pub struct UdpTrackerConfig {
79-
/// Bind address (e.g., "0.0.0.0:6868")
80-
#[serde(
81-
serialize_with = "serialize_socket_addr",
82-
deserialize_with = "deserialize_socket_addr"
83-
)]
84-
pub bind_address: SocketAddr,
85-
}
86-
87-
/// HTTP tracker bind configuration
88-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
89-
pub struct HttpTrackerConfig {
90-
/// Bind address (e.g., "0.0.0.0:7070")
91-
#[serde(
92-
serialize_with = "serialize_socket_addr",
93-
deserialize_with = "deserialize_socket_addr"
94-
)]
95-
pub bind_address: SocketAddr,
96-
}
97-
98-
/// HTTP API configuration
99-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
100-
pub struct HttpApiConfig {
101-
/// Bind address (e.g., "0.0.0.0:1212")
102-
#[serde(
103-
serialize_with = "serialize_socket_addr",
104-
deserialize_with = "deserialize_socket_addr"
105-
)]
106-
pub bind_address: SocketAddr,
107-
108-
/// Admin access token for HTTP API authentication
109-
pub admin_token: ApiToken,
110-
}
111-
112-
/// Health Check API configuration
113-
///
114-
/// The Health Check API is a minimal HTTP endpoint used by Docker and container
115-
/// orchestration tools to verify service health. It's separate from the main HTTP API.
116-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
117-
pub struct HealthCheckApiConfig {
118-
/// Bind address (e.g., "127.0.0.1:1313")
119-
///
120-
/// Conventionally uses port 1313, though this is configurable
121-
#[serde(
122-
serialize_with = "serialize_socket_addr",
123-
deserialize_with = "deserialize_socket_addr"
124-
)]
125-
pub bind_address: SocketAddr,
126-
}
127-
12877
impl Default for TrackerConfig {
12978
/// Returns a default tracker configuration suitable for development and testing
13079
///
@@ -161,14 +110,14 @@ impl Default for TrackerConfig {
161110
}
162111
}
163112

164-
fn serialize_socket_addr<S>(addr: &SocketAddr, serializer: S) -> Result<S::Ok, S::Error>
113+
pub(crate) fn serialize_socket_addr<S>(addr: &SocketAddr, serializer: S) -> Result<S::Ok, S::Error>
165114
where
166115
S: serde::Serializer,
167116
{
168117
serializer.serialize_str(&addr.to_string())
169118
}
170119

171-
fn deserialize_socket_addr<'de, D>(deserializer: D) -> Result<SocketAddr, D::Error>
120+
pub(crate) fn deserialize_socket_addr<'de, D>(deserializer: D) -> Result<SocketAddr, D::Error>
172121
where
173122
D: serde::Deserializer<'de>,
174123
{

src/domain/tracker/config/udp.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! UDP tracker configuration
2+
3+
use std::net::SocketAddr;
4+
5+
use serde::{Deserialize, Serialize};
6+
7+
/// UDP tracker bind configuration
8+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9+
pub struct UdpTrackerConfig {
10+
/// Bind address (e.g., "0.0.0.0:6868")
11+
#[serde(
12+
serialize_with = "crate::domain::tracker::config::serialize_socket_addr",
13+
deserialize_with = "crate::domain::tracker::config::deserialize_socket_addr"
14+
)]
15+
pub bind_address: SocketAddr,
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::*;
21+
22+
#[test]
23+
fn it_should_create_udp_tracker_config() {
24+
let config = UdpTrackerConfig {
25+
bind_address: "0.0.0.0:6868".parse().unwrap(),
26+
};
27+
28+
assert_eq!(
29+
config.bind_address,
30+
"0.0.0.0:6868".parse::<SocketAddr>().unwrap()
31+
);
32+
}
33+
34+
#[test]
35+
fn it_should_serialize_udp_tracker_config() {
36+
let config = UdpTrackerConfig {
37+
bind_address: "0.0.0.0:6969".parse().unwrap(),
38+
};
39+
40+
let json = serde_json::to_value(&config).unwrap();
41+
assert_eq!(json["bind_address"], "0.0.0.0:6969");
42+
}
43+
44+
#[test]
45+
fn it_should_deserialize_udp_tracker_config() {
46+
let json = r#"{"bind_address": "0.0.0.0:6969"}"#;
47+
let config: UdpTrackerConfig = serde_json::from_str(json).unwrap();
48+
49+
assert_eq!(
50+
config.bind_address,
51+
"0.0.0.0:6969".parse::<SocketAddr>().unwrap()
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)