beteran-api-kgon-server-ser.../src/main.rs

139 lines
3.5 KiB
Rust
Raw Normal View History

2022-08-05 04:23:17 +00:00
//!
//!
2022-08-16 17:00:26 +00:00
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
2022-08-05 04:23:17 +00:00
2022-08-16 17:00:26 +00:00
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
2022-08-05 04:23:17 +00:00
use std::env;
2022-08-16 17:00:26 +00:00
mod api;
2022-08-05 04:23:17 +00:00
mod compositions;
2022-08-17 17:45:00 +00:00
mod core;
2022-08-19 03:20:45 +00:00
mod events;
2022-08-05 04:23:17 +00:00
mod repositories;
2022-08-16 17:00:26 +00:00
mod schedulers;
2022-08-05 04:23:17 +00:00
mod services;
2022-08-16 17:00:26 +00:00
diesel_migrations::embed_migrations!();
2022-08-05 04:23:17 +00:00
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2022-08-16 17:00:26 +00:00
let url_db = match env::var_os("URL_DATABASE") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
2022-08-05 04:23:17 +00:00
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(),
};
2022-08-17 17:45:00 +00:00
let k_url = match env::var_os("K_URL") {
2022-08-05 04:23:17 +00:00
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(),
};
2022-08-17 17:45:00 +00:00
let k_secret = match env::var_os("K_SECRET") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
2022-08-05 04:23:17 +00:00
2022-08-17 17:45:00 +00:00
let api_config = core::config::ApiConfig {
k_url,
k_username,
k_secret,
};
2022-08-16 17:00:26 +00:00
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)?;
2022-08-05 04:23:17 +00:00
let server_broker_opts = nats::asynk::Options::new();
let connection_server_broker = server_broker_opts.connect(url_server_broker).await?;
2022-08-17 17:45:00 +00:00
let mut sched = tokio_cron_scheduler::JobScheduler::new().await?;
2022-08-18 22:35:22 +00:00
let member_scheduler = schedulers::member::scheduler::Scheduler::get_instance(
2022-08-17 17:45:00 +00:00
pool.clone(),
sched.clone(),
api_config.clone(),
)?;
2022-08-18 22:35:22 +00:00
member_scheduler.queue().await?;
let balance_scheduler = schedulers::balance::scheduler::Scheduler::get_instance(
pool.clone(),
sched.clone(),
api_config.clone(),
)?;
balance_scheduler.queue().await?;
let game_scheduler = schedulers::game::scheduler::Scheduler::get_instance(
pool.clone(),
sched.clone(),
api_config.clone(),
)?;
game_scheduler.queue().await?;
2022-08-16 17:00:26 +00:00
2022-08-17 17:45:00 +00:00
let _h_scheduler = sched.start().await?;
2022-08-16 17:00:26 +00:00
2022-08-19 07:22:49 +00:00
let vendor_service = services::vendor::service::Service::new(
2022-08-19 03:20:45 +00:00
connection_server_broker.clone(),
queue_server_broker.clone(),
2022-08-19 07:22:49 +00:00
pool.clone(),
2022-08-19 03:20:45 +00:00
);
2022-08-19 07:22:49 +00:00
let game_service = services::game::service::Service::new(
2022-08-19 03:20:45 +00:00
connection_server_broker.clone(),
queue_server_broker.clone(),
2022-08-19 07:22:49 +00:00
pool.clone(),
2022-08-19 03:20:45 +00:00
);
2022-08-19 07:22:49 +00:00
let member_service = services::member::service::Service::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
);
let member_account_service = services::member_account::service::Service::new(
2022-08-19 03:20:45 +00:00
connection_server_broker.clone(),
queue_server_broker.clone(),
);
let betting_service = services::betting::service::Service::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
);
let member_event_handler = events::member::event::EventHandler::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
pool.clone(),
api_config.clone(),
);
2022-08-05 04:23:17 +00:00
println!("Server service [beteran-api-kgon-server-service] is started");
2022-08-16 17:00:26 +00:00
futures::try_join!(
2022-08-19 07:22:49 +00:00
vendor_service.subscribe(),
game_service.subscribe(),
2022-08-18 04:27:11 +00:00
// member_service.subscribe(),
// member_account_service.subscribe(),
// betting_service.subscribe(),
2022-08-19 03:20:45 +00:00
member_event_handler.subscribe(),
2022-08-16 17:00:26 +00:00
)?;
sched.shutdown().await?;
2022-08-05 04:23:17 +00:00
Ok(())
}