SolarEnergy/central_backend/src/main.rs

53 lines
1.9 KiB
Rust

use actix::Actor;
use central_backend::app_config::AppConfig;
use central_backend::crypto::pki;
use central_backend::energy::energy_actor::EnergyActor;
use central_backend::server::servers;
use central_backend::utils::files_utils::create_directory_if_missing;
use futures::future;
use tokio_schedule::{every, Job};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Initialize OpenSSL
openssl_sys::init();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// Initialize storage
create_directory_if_missing(AppConfig::get().pki_path()).unwrap();
create_directory_if_missing(AppConfig::get().devices_config_path()).unwrap();
create_directory_if_missing(AppConfig::get().relays_runtime_stats_storage_path()).unwrap();
create_directory_if_missing(AppConfig::get().energy_consumption_history()).unwrap();
// Initialize PKI
pki::initialize_root_ca().expect("Failed to initialize Root CA!");
pki::initialize_web_ca().expect("Failed to initialize web CA!");
pki::initialize_devices_ca().expect("Failed to initialize devices CA!");
pki::initialize_server_ca().expect("Failed to initialize server certificate!");
// Initialize CRL
pki::refresh_crls().expect("Failed to initialize Root CA!");
let refresh_crl = every(1).hour().perform(|| async {
log::info!("Periodic refresh of CRLs...");
if let Err(e) = pki::refresh_crls() {
log::error!("Failed to perform auto refresh of CRLs! {e}");
}
});
tokio::spawn(refresh_crl);
// Initialize energy actor
let actor = EnergyActor::new()
.await
.expect("Failed to initialize energy actor!")
.start();
let s1 = servers::secure_server(actor);
let s2 = servers::unsecure_server();
future::try_join(s1, s2)
.await
.expect("Failed to start servers!");
Ok(())
}