rsub 0.1.0

A high-performance message broker with QUIC transport and pub/sub messaging patterns
Documentation
use std::sync::Arc;

use clap::{Arg, Command};
use rcgen::{CertificateParams, DnType};
use rsub::{
    common::tls::generate_self_signed_cert,
    server::{config, server::RsubServer},
};

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .init();

    let matches = Command::new("rsub")
        .version("0.1.0")
        .about("High performance streaming server")
        .author("Samespace <open-source@samespace.com>")
        .subcommand(
            Command::new("start").about("Starts the server").arg(
                Arg::new("config")
                    .short('c')
                    .long("config")
                    .value_name("FILE")
                    .help("Sets the config file")
                    .default_value("config.yaml"),
            ),
        )
        .subcommand(
            Command::new("cert-gen")
                .about("Generates a self-signed certificate")
                .arg(
                    Arg::new("cert_file")
                        .short('c')
                        .long("cert")
                        .value_name("FILE")
                        .help("Sets the certificate file")
                        .default_value("cert.pem"),
                )
                .arg(
                    Arg::new("key_file")
                        .short('k')
                        .long("key")
                        .value_name("FILE")
                        .help("Sets the key file")
                        .default_value("key.pem"),
                )
                .arg(
                    Arg::new("days")
                        .short('d')
                        .long("days")
                        .value_name("DAYS")
                        .help("Sets the number of days the certificate is valid for")
                        .default_value("365"),
                )
                .arg(
                    Arg::new("country")
                        .short('o')
                        .long("country")
                        .value_name("COUNTRY")
                        .help("Sets the country")
                        .default_value("US"),
                )
                .arg(
                    Arg::new("state")
                        .short('s')
                        .long("state")
                        .value_name("STATE")
                        .help("Sets the state")
                        .default_value("California"),
                ),
        )
        .get_matches();

    match matches.subcommand() {
        Some(("start", start_matches)) => {
            let config_file = start_matches.get_one::<String>("config").unwrap();
            let config = config::read_config_file(config_file.to_string()).unwrap();
            RsubServer::start(Arc::new(config)).await.unwrap();
        }
        Some(("cert-gen", cert_matches)) => {
            let cert_file = cert_matches.get_one::<String>("cert_file").unwrap();
            let key_file = cert_matches.get_one::<String>("key_file").unwrap();
            let days = cert_matches
                .get_one::<String>("days")
                .unwrap()
                .parse::<u64>()
                .unwrap();
            let country = cert_matches.get_one::<String>("country").unwrap();
            let state = cert_matches.get_one::<String>("state").unwrap();

            let mut params = CertificateParams::default();
            params.not_after = time::OffsetDateTime::now_utc() + time::Duration::days(days as i64);
            params
                .distinguished_name
                .push(DnType::CountryName, country.to_string());
            params
                .distinguished_name
                .push(DnType::StateOrProvinceName, state.to_string());

            generate_self_signed_cert(key_file, cert_file, Some(params)).unwrap();
            println!("Self-signed certificate generated successfully.");
        }
        _ => {
            let config = config::read_config_file("config.yaml".to_string()).unwrap();
            RsubServer::start(Arc::new(config)).await.unwrap();
        }
    }
}