You've already forked AstralRinth
forked from didirus/AstralRinth
Add launcher analytics (#661)
* Add more analytics * finish hydra move * Finish websocket flow * add minecraft account flow * Finish playtime vals + payout automation
This commit is contained in:
114
src/main.rs
114
src/main.rs
@@ -1,23 +1,26 @@
|
||||
use crate::file_hosting::S3Host;
|
||||
use crate::queue::analytics::AnalyticsQueue;
|
||||
use crate::queue::download::DownloadQueue;
|
||||
use crate::queue::payouts::PayoutsQueue;
|
||||
use crate::queue::payouts::{process_payout, PayoutsQueue};
|
||||
use crate::queue::session::AuthQueue;
|
||||
use crate::queue::socket::ActiveSockets;
|
||||
use crate::ratelimit::errors::ARError;
|
||||
use crate::ratelimit::memory::{MemoryStore, MemoryStoreActor};
|
||||
use crate::ratelimit::middleware::RateLimiter;
|
||||
use crate::util::cors::default_cors;
|
||||
use crate::util::env::{parse_strings_from_var, parse_var};
|
||||
use actix_cors::Cors;
|
||||
use actix_files::Files;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use chrono::{DateTime, Utc};
|
||||
use deadpool_redis::{Config, Runtime};
|
||||
use env_logger::Env;
|
||||
use log::{error, info, warn};
|
||||
use search::indexing::index_projects;
|
||||
use search::indexing::IndexingSettings;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::{Mutex, RwLock};
|
||||
|
||||
mod auth;
|
||||
mod clickhouse;
|
||||
mod database;
|
||||
mod file_hosting;
|
||||
mod models;
|
||||
@@ -121,8 +124,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let search_config_ref = search_config_ref.clone();
|
||||
async move {
|
||||
info!("Indexing local database");
|
||||
let settings = IndexingSettings { index_local: true };
|
||||
let result = index_projects(pool_ref, settings, &search_config_ref).await;
|
||||
let result = index_projects(pool_ref, &search_config_ref).await;
|
||||
if let Err(e) = result {
|
||||
warn!("Local project indexing failed: {:?}", e);
|
||||
}
|
||||
@@ -280,11 +282,75 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
info!("Initializing clickhouse connection");
|
||||
let clickhouse = clickhouse::init_client().await.unwrap();
|
||||
|
||||
let reader = Arc::new(queue::maxmind::MaxMindIndexer::new().await.unwrap());
|
||||
{
|
||||
let reader_ref = reader.clone();
|
||||
scheduler.run(std::time::Duration::from_secs(60 * 60 * 24), move || {
|
||||
let reader_ref = reader_ref.clone();
|
||||
|
||||
async move {
|
||||
info!("Downloading MaxMind GeoLite2 country database");
|
||||
let result = reader_ref.index().await;
|
||||
if let Err(e) = result {
|
||||
warn!(
|
||||
"Downloading MaxMind GeoLite2 country database failed: {:?}",
|
||||
e
|
||||
);
|
||||
}
|
||||
info!("Done downloading MaxMind GeoLite2 country database");
|
||||
}
|
||||
});
|
||||
}
|
||||
info!("Downloading MaxMind GeoLite2 country database");
|
||||
|
||||
let analytics_queue = Arc::new(AnalyticsQueue::new());
|
||||
{
|
||||
let client_ref = clickhouse.clone();
|
||||
let analytics_queue_ref = analytics_queue.clone();
|
||||
scheduler.run(std::time::Duration::from_secs(60 * 5), move || {
|
||||
let client_ref = client_ref.clone();
|
||||
let analytics_queue_ref = analytics_queue_ref.clone();
|
||||
|
||||
async move {
|
||||
info!("Indexing analytics queue");
|
||||
let result = analytics_queue_ref.index(client_ref).await;
|
||||
if let Err(e) = result {
|
||||
warn!("Indexing analytics queue failed: {:?}", e);
|
||||
}
|
||||
info!("Done indexing analytics queue");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let pool_ref = pool.clone();
|
||||
let redis_ref = redis_pool.clone();
|
||||
let client_ref = clickhouse.clone();
|
||||
scheduler.run(std::time::Duration::from_secs(60 * 60 * 6), move || {
|
||||
let pool_ref = pool_ref.clone();
|
||||
let redis_ref = redis_ref.clone();
|
||||
let client_ref = client_ref.clone();
|
||||
|
||||
async move {
|
||||
info!("Done running payouts");
|
||||
let result = process_payout(&pool_ref, &redis_ref, &client_ref).await;
|
||||
if let Err(e) = result {
|
||||
warn!("Payouts run failed: {:?}", e);
|
||||
}
|
||||
info!("Done running payouts");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let ip_salt = Pepper {
|
||||
pepper: models::ids::Base62Id(models::ids::random_base62(11)).to_string(),
|
||||
};
|
||||
|
||||
let payouts_queue = web::Data::new(Mutex::new(PayoutsQueue::new()));
|
||||
let active_sockets = web::Data::new(RwLock::new(ActiveSockets::default()));
|
||||
|
||||
let store = MemoryStore::new();
|
||||
|
||||
@@ -294,14 +360,6 @@ async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(actix_web::middleware::Compress::default())
|
||||
.wrap(
|
||||
Cors::default()
|
||||
.allow_any_origin()
|
||||
.allow_any_header()
|
||||
.allow_any_method()
|
||||
.max_age(3600)
|
||||
.send_wildcard(),
|
||||
)
|
||||
.wrap(
|
||||
RateLimiter::new(MemoryStoreActor::from(store.clone()).start())
|
||||
.with_identifier(|req| {
|
||||
@@ -323,6 +381,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.with_max_requests(300)
|
||||
.with_ignore_key(dotenvy::var("RATE_LIMIT_IGNORE_KEY").ok()),
|
||||
)
|
||||
.wrap(sentry_actix::Sentry::new())
|
||||
.app_data(
|
||||
web::FormConfig::default().error_handler(|err, _req| {
|
||||
routes::ApiError::Validation(err.to_string()).into()
|
||||
@@ -351,11 +410,15 @@ async fn main() -> std::io::Result<()> {
|
||||
.app_data(session_queue.clone())
|
||||
.app_data(payouts_queue.clone())
|
||||
.app_data(web::Data::new(ip_salt.clone()))
|
||||
.wrap(sentry_actix::Sentry::new())
|
||||
.app_data(web::Data::new(analytics_queue.clone()))
|
||||
.app_data(web::Data::new(clickhouse.clone()))
|
||||
.app_data(web::Data::new(reader.clone()))
|
||||
.app_data(active_sockets.clone())
|
||||
.configure(routes::root_config)
|
||||
.configure(routes::v2::config)
|
||||
.configure(routes::v3::config)
|
||||
.default_service(web::get().to(routes::not_found))
|
||||
.service(Files::new("/", "assets/"))
|
||||
.default_service(web::get().wrap(default_cors()).to(routes::not_found))
|
||||
})
|
||||
.bind(dotenvy::var("BIND_ADDR").unwrap())?
|
||||
.run()
|
||||
@@ -431,9 +494,6 @@ fn check_env_vars() -> bool {
|
||||
failed |= true;
|
||||
}
|
||||
|
||||
failed |= check_var::<String>("ARIADNE_ADMIN_KEY");
|
||||
failed |= check_var::<String>("ARIADNE_URL");
|
||||
|
||||
failed |= check_var::<String>("PAYPAL_API_URL");
|
||||
failed |= check_var::<String>("PAYPAL_CLIENT_ID");
|
||||
failed |= check_var::<String>("PAYPAL_CLIENT_SECRET");
|
||||
@@ -462,5 +522,21 @@ fn check_env_vars() -> bool {
|
||||
failed |= check_var::<String>("BEEHIIV_PUBLICATION_ID");
|
||||
failed |= check_var::<String>("BEEHIIV_API_KEY");
|
||||
|
||||
if parse_strings_from_var("ANALYTICS_ALLOWED_ORIGINS").is_none() {
|
||||
warn!(
|
||||
"Variable `ANALYTICS_ALLOWED_ORIGINS` missing in dotenv or not a json array of strings"
|
||||
);
|
||||
failed |= true;
|
||||
}
|
||||
|
||||
failed |= check_var::<String>("CLICKHOUSE_URL");
|
||||
failed |= check_var::<String>("CLICKHOUSE_USER");
|
||||
failed |= check_var::<String>("CLICKHOUSE_PASSWORD");
|
||||
failed |= check_var::<String>("CLICKHOUSE_DATABASE");
|
||||
|
||||
failed |= check_var::<String>("MAXMIND_LICENSE_KEY");
|
||||
|
||||
failed |= check_var::<u64>("PAYOUTS_BUDGET");
|
||||
|
||||
failed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user