use std::net::SocketAddr;
use std::sync::Arc;
use usenet_dl::UsenetDownloader;
use usenet_dl::api::start_api_server;
use usenet_dl::config::{ApiConfig, Config, DownloadConfig, ServerConfig, ServerIntegrationConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = ServerConfig {
host: "news.example.com".to_string(),
port: 563,
tls: true,
username: Some("your_username".to_string()),
password: Some("your_password".to_string()),
connections: 10,
priority: 0,
pipeline_depth: 10,
};
let api_config = ApiConfig {
bind_address: "127.0.0.1:6789".parse::<SocketAddr>().unwrap(),
api_key: None, cors_enabled: true,
cors_origins: vec!["*".to_string()],
swagger_ui: true,
..Default::default()
};
let config = Config {
servers: vec![server],
download: DownloadConfig {
download_dir: "downloads".into(),
temp_dir: "temp".into(),
..Default::default()
},
server: ServerIntegrationConfig { api: api_config },
..Default::default()
};
let downloader = Arc::new(UsenetDownloader::new(config.clone()).await?);
let config_arc = Arc::new(config);
println!("🚀 Starting usenet-dl REST API server");
println!("📖 Swagger UI: http://localhost:6789/swagger-ui");
println!("📡 API Base: http://localhost:6789/api/v1");
println!("🔄 Events stream: http://localhost:6789/api/v1/events");
println!();
println!("Example commands:");
println!(" # Add download from URL");
println!(" curl -X POST http://localhost:6789/api/v1/downloads/url \\");
println!(" -H 'Content-Type: application/json' \\");
println!(
" -d '{{\"url\": \"https://example.com/file.nzb\", \"options\": {{\"category\": \"movies\"}}}}'"
);
println!();
println!(" # List all downloads");
println!(" curl http://localhost:6789/api/v1/downloads");
println!();
println!(" # Stream events (Server-Sent Events)");
println!(" curl -N http://localhost:6789/api/v1/events");
start_api_server(downloader, config_arc).await?;
Ok(())
}