Initial commit.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
mod application_context;
|
||||
mod server_mode;
|
||||
|
||||
use crate::application_context::ApplicationContext;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use log::info;
|
||||
use rustls::ServerConfig;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Error};
|
||||
use std::sync::Arc;
|
||||
use surveillant_systeme_exposition::init;
|
||||
use tokio_cron_scheduler::{Job, JobScheduler};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()>{
|
||||
dotenv().ok();
|
||||
unsafe { env::set_var("RUST_LOG", "debug"); }
|
||||
check_all_variables_are_set();
|
||||
env_logger::init();
|
||||
|
||||
let application_context = ApplicationContext::init()?;
|
||||
|
||||
let tls_config = init_tls_configuration()?;
|
||||
let server = HttpServer::new(
|
||||
move || App::new().configure(init)
|
||||
.app_data(web::Data::new(Arc::new(application_context.user_service.clone())))
|
||||
)
|
||||
.bind_rustls_0_23(("0.0.0.0", 19000), tls_config)
|
||||
.expect("Unable to start server");
|
||||
|
||||
server.run().await
|
||||
}
|
||||
|
||||
fn init_tls_configuration() -> Result<ServerConfig, Error> {
|
||||
let mut certs_file = BufReader::new(File::open("./certs/lumiere.crt")?);
|
||||
let mut key_file = BufReader::new(File::open("./certs/lumiere.key")?);
|
||||
|
||||
let tls_certs = rustls_pemfile::certs(&mut certs_file)
|
||||
.collect::<Result<Vec<_>, _>>().expect("Unable to read the TLS certificate…");
|
||||
let tls_key = rustls_pemfile::pkcs8_private_keys(&mut key_file)
|
||||
.next()
|
||||
.unwrap()
|
||||
.expect("Unable to read the private key for TLS configuration…");
|
||||
|
||||
let tls_config = rustls::ServerConfig::builder()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(tls_certs, rustls::pki_types::PrivateKeyDer::Pkcs8(tls_key))
|
||||
.unwrap();
|
||||
|
||||
Ok(tls_config)
|
||||
}
|
||||
|
||||
|
||||
fn check_all_variables_are_set() {
|
||||
info!("Checking all variables exist…");
|
||||
let variables = vec!["JWT_SECRET", "SQLITE_DATABASE_NAME", "BCRYPT_SALT"];
|
||||
variables.iter().for_each(|variable| {
|
||||
env::var(variable).expect(format!("{} should be defined!", variable).as_str());
|
||||
});
|
||||
info!("All variables are set correctly.");
|
||||
}
|
||||
Reference in New Issue
Block a user