Skip to content
Snippets Groups Projects
Select Git revision
  • e06f5c8f52487cd5d1ac20874a62439daf25fd95
  • main default protected
2 results

eventhandler.rs

Blame
  • eventhandler.rs 2.54 KiB
    use std::process::exit;
    use std::env::args;
    use std::collections::HashMap;
    use std::path::PathBuf;
    
    use reqwest;
    pub use log::{info, warn, debug, error};
    
    use sms_gateway::{
        ErrorResponse,
        PACKAGE_NAME,
        CONFIG
    };
    
    /// Static variable holding the read configuration
    const BINARY_NAME: &str = env!("CARGO_BIN_NAME");
    
    
    
    fn help() {
        info!("Error: Insufficient number of arguments! Expected two or three arguments.");
        exit(1);
    }
    
    
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        info!("Started {} {}", PACKAGE_NAME, BINARY_NAME);
    
        let args: Vec<String> = args().collect();
    
        match args.len() {
            2 | 3 => {
                let kind = args[1].to_ascii_lowercase();
                let path = PathBuf::from(&args[2]);
    
                let content = match std::fs::read_to_string(&path) {
                    Ok(s) => s,
                    Err(e) => {
                        error!("Could not read {} SMS from path \"{}\": {}", kind, path.to_string_lossy(), e);
                        exit(1);
                    }
                };
    
                match &kind[..] {
                    "received" => {
                        info!("Received sms event of type \"{}\": {}", kind, content);
                    },
                    "sent" => {
                        info!("Received sms event of type \"{}\": {}", kind, content);
                    },
                    "failed" => {
                        info!("Received sms event of type \"{}\": {}", kind, content);
                    },
                    _ => {
                        error!("Error: Unhandled event type: {}", &kind[..]);
                        exit(1);
                    }
                }
    
                let url = format!("http://{host}:{port}/eventhandler", host=CONFIG.server.host, port=CONFIG.server.port);
                info!("Attempting to send Event to url: \"{}\"", url);
    
                let mut map = HashMap::new();
                map.insert("kind", kind);
                map.insert("sms", content.replace("\\n", "\n"));
    
                debug!("Json Payload to be sent: {:#?}", &map);
    
                let client = reqwest::blocking::Client::new();