beteran-api-kgon-server-ser.../src/main.rs
2022-08-16 17:00:26 +00:00

107 lines
2.7 KiB
Rust

//!
//!
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use std::env;
mod api;
mod compositions;
mod repositories;
mod schedulers;
mod services;
diesel_migrations::embed_migrations!();
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url_db = match env::var_os("URL_DATABASE") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let url_server_broker = match env::var_os("URL_BROKER") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let queue_server_broker = match env::var_os("QUEUE_BROKER") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let k_secret = match env::var_os("K_SECRET") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let k_username = match env::var_os("K_USERNAME") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let mut sched = tokio_cron_scheduler::JobScheduler::new().await?;
let manager = ConnectionManager::<PgConnection>::new(url_db);
let pool = Pool::builder()
.max_size(4)
.test_on_check_out(true)
.build(manager)?;
let conn = pool.get()?;
embedded_migrations::run(&conn)?;
let server_broker_opts = nats::asynk::Options::new();
let connection_server_broker = server_broker_opts.connect(url_server_broker).await?;
let member_service = services::member::service::Service::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
);
let member_account_service = services::member_account::service::Service::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
);
let game_service = services::game::service::Service::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
);
let betting_service = services::betting::service::Service::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
);
let vendor_scheduler = schedulers::vendor::scheduler::Scheduler::new(pool.clone(), sched.clone());
vendor_scheduler.queue().await;
let start = sched.start().await;
if start.is_err() {
panic!("Error starting scheduler");
}
println!("Server service [beteran-api-kgon-server-service] is started");
futures::try_join!(
member_service.subscribe(),
member_account_service.subscribe(),
game_service.subscribe(),
betting_service.subscribe(),
)?;
sched.shutdown().await?;
Ok(())
}