This commit is contained in:
Timur Gordon
2025-08-01 00:01:08 +02:00
parent 32c2cbe0cc
commit 8ed40ce99c
57 changed files with 2047 additions and 4113 deletions

View File

@@ -1,7 +1,5 @@
use hero_websocket_server::{ServerBuilder, TlsConfigError};
use hero_websocket_server::{ServerBuilder, ServerConfig};
use clap::Parser;
use dotenv::dotenv;
use log::info;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
@@ -39,14 +37,62 @@ struct Args {
#[clap(long, help = "Enable webhook handling")]
webhooks: bool,
#[clap(long, value_parser, help = "Worker ID for the server")]
worker_id: String,
#[clap(short, long, value_parser, help = "Path to configuration file")]
config: Option<String>,
#[clap(long, help = "Generate a sample configuration file")]
generate_config: bool,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let args = Args::parse();
// Handle config file generation
if args.generate_config {
let sample_config = ServerConfig::create_sample();
let config_path = "config.json";
match sample_config.to_file(config_path) {
Ok(_) => {
println!("✅ Sample configuration file generated: {}", config_path);
println!("📝 Edit the file to customize your server configuration.");
return Ok(());
}
Err(e) => {
eprintln!("❌ Failed to generate config file: {}", e);
std::process::exit(1);
}
}
}
// Load configuration from file if provided, otherwise use CLI args
let config = if let Some(config_path) = &args.config {
match ServerConfig::from_file(config_path) {
Ok(config) => {
println!("📄 Loaded configuration from: {}", config_path);
config
}
Err(e) => {
eprintln!("❌ Failed to load config file {}: {}", config_path, e);
std::process::exit(1);
}
}
} else {
// Create config from CLI arguments
ServerConfig {
host: args.host.clone(),
port: args.port,
redis_url: args.redis_url.clone(),
auth: args.auth,
tls: args.tls,
cert: args.cert.clone(),
key: args.key.clone(),
tls_port: args.tls_port,
webhooks: args.webhooks,
circles: std::collections::HashMap::new(), // Empty circles when using CLI
}
};
// Configure logging based on verbosity level
let log_config = match args.verbose {
0 => {
@@ -78,39 +124,14 @@ async fn main() -> std::io::Result<()> {
env_logger::init();
}
// Validate TLS configuration
if args.tls && (args.cert.is_none() || args.key.is_none()) {
eprintln!("Error: TLS is enabled but certificate or key path is missing");
eprintln!("Use --cert and --key to specify certificate and key files");
// Validate configuration
if let Err(e) = config.validate() {
eprintln!("❌ Configuration validation failed: {}", e);
std::process::exit(1);
}
let mut builder = ServerBuilder::new()
.host(args.host.clone())
.port(args.port)
.redis_url(args.redis_url.clone())
.worker_id(args.worker_id.clone());
if args.auth {
builder = builder.with_auth();
}
if args.tls {
if let (Some(cert), Some(key)) = (args.cert.clone(), args.key.clone()) {
builder = builder.with_tls(cert, key);
} else {
eprintln!("Error: TLS is enabled but --cert or --key is missing.");
std::process::exit(1);
}
}
if let Some(tls_port) = args.tls_port {
builder = builder.with_tls_port(tls_port);
}
if args.webhooks {
builder = builder.with_webhooks();
}
// Build server from configuration
let builder = ServerBuilder::new().from_config(config.clone());
let server = match builder.build() {
Ok(server) => server,
@@ -122,27 +143,36 @@ async fn main() -> std::io::Result<()> {
println!("🚀 Starting Circles WebSocket Server");
println!("📋 Configuration:");
println!(" Host: {}", args.host);
println!(" Port: {}", args.port);
if let Some(tls_port) = args.tls_port {
println!(" Host: {}", config.host);
println!(" Port: {}", config.port);
println!(" Redis URL: {}", config.redis_url);
if let Some(tls_port) = config.tls_port {
println!(" TLS Port: {}", tls_port);
}
println!(" Authentication: {}", if args.auth { "ENABLED" } else { "DISABLED" });
println!(" TLS/WSS: {}", if args.tls { "ENABLED" } else { "DISABLED" });
println!(" Webhooks: {}", if args.webhooks { "ENABLED" } else { "DISABLED" });
println!(" Authentication: {}", if config.auth { "ENABLED" } else { "DISABLED" });
println!(" TLS/WSS: {}", if config.tls { "ENABLED" } else { "DISABLED" });
println!(" Webhooks: {}", if config.webhooks { "ENABLED" } else { "DISABLED" });
println!(" Circles configured: {}", config.circles.len());
if args.tls {
if let (Some(cert), Some(key)) = (&args.cert, &args.key) {
if config.tls {
if let (Some(cert), Some(key)) = (&config.cert, &config.key) {
println!(" Certificate: {}", cert);
println!(" Private Key: {}", key);
}
}
if args.webhooks {
if config.webhooks {
println!(" Webhook secrets loaded from environment variables:");
println!(" - STRIPE_WEBHOOK_SECRET");
println!(" - IDENFY_WEBHOOK_SECRET");
}
if config.auth && !config.circles.is_empty() {
println!(" Configured circles:");
for (circle_name, members) in &config.circles {
println!(" - {}: {} members", circle_name, members.len());
}
}
println!();
let (server_task, _server_handle) = server.spawn_circle_server()?;