Replace custom logging with tracing crate and RUST_LOG env var

- Remove custom logging module and init_logging function
- Update main.rs to use tracing_subscriber with EnvFilter
- Remove log_level from global config structure
- Update documentation and tests to use RUST_LOG
- Format long lines in config.rs and test files for better readability
This commit is contained in:
Jeena 2026-01-22 05:25:46 +00:00
parent 50a4d9bc75
commit 55fe47b172
15 changed files with 787 additions and 459 deletions

View file

@ -15,10 +15,7 @@ fn test_concurrent_requests_multiple_hosts() {
for host in &hosts {
let root_dir = temp_dir.path().join(host.replace(".", "_"));
std::fs::create_dir(&root_dir).unwrap();
std::fs::write(
root_dir.join("index.gmi"),
format!("Welcome to {}", host),
).unwrap();
std::fs::write(root_dir.join("index.gmi"), format!("Welcome to {}", host)).unwrap();
host_roots.push(root_dir);
}
@ -26,23 +23,28 @@ fn test_concurrent_requests_multiple_hosts() {
let config_path = temp_dir.path().join("config.toml");
let port = 1969 + (std::process::id() % 1000) as u16;
let mut config_content = format!(r#"
let mut config_content = format!(
r#"
bind_host = "127.0.0.1"
port = {}
"#, port);
"#,
port
);
for (i, host) in hosts.iter().enumerate() {
config_content.push_str(&format!(r#"
config_content.push_str(&format!(
r#"
["{}"]
root = "{}"
cert = "{}"
key = "{}"
"#,
host,
host_roots[i].display(),
temp_dir.path().join("cert.pem").display(),
temp_dir.path().join("key.pem").display()));
host,
host_roots[i].display(),
temp_dir.path().join("cert.pem").display(),
temp_dir.path().join("key.pem").display()
));
}
std::fs::write(&config_path, config_content).unwrap();
@ -65,9 +67,20 @@ key = "{}"
let port_clone = Arc::clone(&port_arc);
let handle = thread::spawn(move || {
let response = make_gemini_request("127.0.0.1", *port_clone, &format!("gemini://{}/", host));
assert!(response.starts_with("20"), "Request {} failed: {}", i, response);
assert!(response.contains(&format!("Welcome to {}", host)), "Wrong content for request {}: {}", i, response);
let response =
make_gemini_request("127.0.0.1", *port_clone, &format!("gemini://{}/", host));
assert!(
response.starts_with("20"),
"Request {} failed: {}",
i,
response
);
assert!(
response.contains(&format!("Welcome to {}", host)),
"Wrong content for request {}: {}",
i,
response
);
response
});
@ -97,7 +110,8 @@ fn test_mixed_valid_invalid_hostnames() {
// Create config
let config_path = temp_dir.path().join("config.toml");
let port = 1970 + (std::process::id() % 1000) as u16;
let config_content = format!(r#"
let config_content = format!(
r#"
bind_host = "127.0.0.1"
port = {}
@ -106,10 +120,11 @@ root = "{}"
cert = "{}"
key = "{}"
"#,
port,
root_dir.display(),
temp_dir.path().join("cert.pem").display(),
temp_dir.path().join("key.pem").display());
port,
root_dir.display(),
temp_dir.path().join("cert.pem").display(),
temp_dir.path().join("key.pem").display()
);
std::fs::write(&config_path, config_content).unwrap();
// Start server with TLS
@ -123,8 +138,16 @@ key = "{}"
// Test valid hostname
let valid_response = make_gemini_request("127.0.0.1", port, "gemini://valid.com/");
assert!(valid_response.starts_with("20"), "Valid host should work: {}", valid_response);
assert!(valid_response.contains("Valid site content"), "Should serve correct content: {}", valid_response);
assert!(
valid_response.starts_with("20"),
"Valid host should work: {}",
valid_response
);
assert!(
valid_response.contains("Valid site content"),
"Should serve correct content: {}",
valid_response
);
// Test various invalid hostnames
let invalid_hosts = vec![
@ -135,8 +158,14 @@ key = "{}"
];
for invalid_host in invalid_hosts {
let response = make_gemini_request("127.0.0.1", port, &format!("gemini://{}/", invalid_host));
assert!(response.starts_with("53"), "Invalid host '{}' should return 53, got: {}", invalid_host, response);
let response =
make_gemini_request("127.0.0.1", port, &format!("gemini://{}/", invalid_host));
assert!(
response.starts_with("53"),
"Invalid host '{}' should return 53, got: {}",
invalid_host,
response
);
}
server_process.kill().unwrap();
@ -153,7 +182,8 @@ fn test_load_performance_basic() {
let config_path = temp_dir.path().join("config.toml");
let port = 1971 + (std::process::id() % 1000) as u16;
let config_content = format!(r#"
let config_content = format!(
r#"
bind_host = "127.0.0.1"
port = {}
@ -162,10 +192,11 @@ root = "{}"
cert = "{}"
key = "{}"
"#,
port,
root_dir.display(),
temp_dir.path().join("cert.pem").display(),
temp_dir.path().join("key.pem").display());
port,
root_dir.display(),
temp_dir.path().join("cert.pem").display(),
temp_dir.path().join("key.pem").display()
);
std::fs::write(&config_path, config_content).unwrap();
// Start server with TLS
@ -183,17 +214,30 @@ key = "{}"
for i in 0..NUM_REQUESTS {
let response = make_gemini_request("127.0.0.1", port, "gemini://perf.com/");
assert!(response.starts_with("20"), "Request {} failed: {}", i, response);
assert!(
response.starts_with("20"),
"Request {} failed: {}",
i,
response
);
}
let elapsed = start.elapsed();
let avg_time = elapsed.as_millis() as f64 / NUM_REQUESTS as f64;
println!("Processed {} requests in {:?} (avg: {:.2}ms per request)",
NUM_REQUESTS, elapsed, avg_time);
tracing::debug!(
"Processed {} requests in {:?} (avg: {:.2}ms per request)",
NUM_REQUESTS,
elapsed,
avg_time
);
// Basic performance check - should be reasonably fast
assert!(avg_time < 100.0, "Average request time too slow: {:.2}ms", avg_time);
assert!(
avg_time < 100.0,
"Average request time too slow: {:.2}ms",
avg_time
);
server_process.kill().unwrap();
}
@ -222,7 +266,8 @@ fn test_full_request_lifecycle() {
let cert_path = temp_dir.path().join("cert.pem");
let key_path = temp_dir.path().join("key.pem");
let config_content = format!(r#"
let config_content = format!(
r#"
bind_host = "127.0.0.1"
port = {}
@ -231,10 +276,11 @@ root = "{}"
cert = "{}"
key = "{}"
"#,
port,
root_dir.display(),
cert_path.display(),
key_path.display());
port,
root_dir.display(),
cert_path.display(),
key_path.display()
);
std::fs::write(&config_path, config_content).unwrap();
// Start server with TLS
@ -248,27 +294,64 @@ key = "{}"
// Test root index
let root_response = make_gemini_request("127.0.0.1", port, "gemini://lifecycle.com/");
assert!(root_response.starts_with("20"), "Root request failed: {}", root_response);
assert!(root_response.contains("Main site content"), "Wrong root content: {}", root_response);
assert!(
root_response.starts_with("20"),
"Root request failed: {}",
root_response
);
assert!(
root_response.contains("Main site content"),
"Wrong root content: {}",
root_response
);
// Test explicit index
let index_response = make_gemini_request("127.0.0.1", port, "gemini://lifecycle.com/index.gmi");
assert!(index_response.starts_with("20"), "Index request failed: {}", index_response);
assert!(index_response.contains("Main site content"), "Wrong index content: {}", index_response);
assert!(
index_response.starts_with("20"),
"Index request failed: {}",
index_response
);
assert!(
index_response.contains("Main site content"),
"Wrong index content: {}",
index_response
);
// Test subdirectory index
let blog_response = make_gemini_request("127.0.0.1", port, "gemini://lifecycle.com/blog/");
assert!(blog_response.starts_with("20"), "Blog request failed: {}", blog_response);
assert!(blog_response.contains("Blog index content"), "Wrong blog content: {}", blog_response);
assert!(
blog_response.starts_with("20"),
"Blog request failed: {}",
blog_response
);
assert!(
blog_response.contains("Blog index content"),
"Wrong blog content: {}",
blog_response
);
// Test individual file
let about_response = make_gemini_request("127.0.0.1", port, "gemini://lifecycle.com/about.gmi");
assert!(about_response.starts_with("20"), "About request failed: {}", about_response);
assert!(about_response.contains("About page content"), "Wrong about content: {}", about_response);
assert!(
about_response.starts_with("20"),
"About request failed: {}",
about_response
);
assert!(
about_response.contains("About page content"),
"Wrong about content: {}",
about_response
);
// Test not found
let notfound_response = make_gemini_request("127.0.0.1", port, "gemini://lifecycle.com/nonexistent.gmi");
assert!(notfound_response.starts_with("51"), "Not found should return 51: {}", notfound_response);
let notfound_response =
make_gemini_request("127.0.0.1", port, "gemini://lifecycle.com/nonexistent.gmi");
assert!(
notfound_response.starts_with("51"),
"Not found should return 51: {}",
notfound_response
);
server_process.kill().unwrap();
}
@ -295,4 +378,3 @@ fn make_gemini_request(host: &str, port: u16, url: &str) -> String {
Err(e) => format!("Error: Failed to run Python client: {}", e),
}
}