SolarEnergy/central_backend/src/main.rs

53 lines
1.9 KiB
Rust
Raw Normal View History

2024-06-29 09:45:39 +00:00
use actix::Actor;
2024-06-27 16:55:09 +00:00
use central_backend::app_config::AppConfig;
2024-06-28 15:21:40 +00:00
use central_backend::crypto::pki;
2024-06-29 09:45:39 +00:00
use central_backend::energy::energy_actor::EnergyActor;
2024-06-29 12:43:56 +00:00
use central_backend::server::servers;
2024-06-27 16:55:09 +00:00
use central_backend::utils::files_utils::create_directory_if_missing;
2024-06-28 20:00:20 +00:00
use futures::future;
use tokio_schedule::{every, Job};
2024-06-27 16:55:09 +00:00
2024-06-28 20:00:20 +00:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2024-06-28 17:19:17 +00:00
// Initialize OpenSSL
openssl_sys::init();
2024-06-27 16:55:09 +00:00
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// Initialize storage
2024-06-28 15:21:40 +00:00
create_directory_if_missing(AppConfig::get().pki_path()).unwrap();
2024-07-01 19:10:45 +00:00
create_directory_if_missing(AppConfig::get().devices_config_path()).unwrap();
2024-09-17 20:31:51 +00:00
create_directory_if_missing(AppConfig::get().relays_runtime_stats_storage_path()).unwrap();
create_directory_if_missing(AppConfig::get().energy_consumption_history()).unwrap();
2024-06-27 16:55:09 +00:00
// Initialize PKI
pki::initialize_root_ca().expect("Failed to initialize Root CA!");
2024-06-27 23:05:02 +00:00
pki::initialize_web_ca().expect("Failed to initialize web CA!");
pki::initialize_devices_ca().expect("Failed to initialize devices CA!");
2024-06-28 19:34:18 +00:00
pki::initialize_server_ca().expect("Failed to initialize server certificate!");
2024-06-28 17:19:17 +00:00
// Initialize CRL
2024-06-28 17:43:33 +00:00
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);
2024-07-17 16:31:57 +00:00
2024-06-29 09:45:39 +00:00
// Initialize energy actor
let actor = EnergyActor::new()
.await
.expect("Failed to initialize energy actor!")
.start();
2024-06-29 12:43:56 +00:00
let s1 = servers::secure_server(actor);
let s2 = servers::unsecure_server();
2024-06-28 20:00:20 +00:00
future::try_join(s1, s2)
.await
.expect("Failed to start servers!");
Ok(())
2024-06-27 16:55:09 +00:00
}