2025-03-25 01:10:43 -07:00
|
|
|
use actix_web::middleware::from_fn;
|
2023-10-06 09:57:33 -07:00
|
|
|
use actix_web::{App, HttpServer};
|
2023-10-06 17:58:02 -05:00
|
|
|
use actix_web_prom::PrometheusMetricsBuilder;
|
2025-03-15 09:28:20 -05:00
|
|
|
use clap::Parser;
|
|
|
|
|
use labrinth::background_task::BackgroundTask;
|
2023-10-06 09:57:33 -07:00
|
|
|
use labrinth::database::redis::RedisPool;
|
|
|
|
|
use labrinth::file_hosting::S3Host;
|
2024-01-05 08:20:56 -08:00
|
|
|
use labrinth::search;
|
2025-03-25 01:10:43 -07:00
|
|
|
use labrinth::util::ratelimit::rate_limit_middleware;
|
2023-10-06 09:57:33 -07:00
|
|
|
use labrinth::{check_env_vars, clickhouse, database, file_hosting, queue};
|
|
|
|
|
use std::sync::Arc;
|
2025-03-09 13:01:24 -07:00
|
|
|
use tracing::{error, info};
|
|
|
|
|
use tracing_actix_web::TracingLogger;
|
2020-05-09 22:11:48 -07:00
|
|
|
|
2025-05-05 21:38:10 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
2024-01-27 18:13:52 -05:00
|
|
|
#[global_allocator]
|
2025-03-09 14:41:19 -07:00
|
|
|
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
|
|
|
|
|
|
|
|
|
|
#[allow(non_upper_case_globals)]
|
2025-05-09 07:27:55 -05:00
|
|
|
#[unsafe(export_name = "malloc_conf")]
|
2025-03-09 14:41:19 -07:00
|
|
|
pub static malloc_conf: &[u8] =
|
|
|
|
|
b"prof:true,prof_active:true,lg_prof_sample:19\0";
|
2024-01-27 18:13:52 -05:00
|
|
|
|
2020-11-27 10:57:04 -07:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Pepper {
|
|
|
|
|
pub pepper: String,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-15 09:28:20 -05:00
|
|
|
#[derive(Parser)]
|
|
|
|
|
#[command(version)]
|
|
|
|
|
struct Args {
|
|
|
|
|
/// Don't run regularly scheduled background tasks. This means the tasks should be run
|
|
|
|
|
/// manually with --run-background-task.
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
no_background_tasks: bool,
|
|
|
|
|
|
2025-03-25 01:10:43 -07:00
|
|
|
/// Don't automatically run migrations. This means the migrations should be run via --run-background-task.
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
no_migrations: bool,
|
|
|
|
|
|
2025-03-15 09:28:20 -05:00
|
|
|
/// Run a single background task and then exit. Perfect for cron jobs.
|
|
|
|
|
#[arg(long, value_enum, id = "task")]
|
|
|
|
|
run_background_task: Option<BackgroundTask>,
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 22:11:48 -07:00
|
|
|
#[actix_rt::main]
|
2020-05-10 22:30:28 -07:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2025-03-15 09:28:20 -05:00
|
|
|
let args = Args::parse();
|
|
|
|
|
|
2022-10-22 21:23:31 -07:00
|
|
|
dotenvy::dotenv().ok();
|
2025-03-09 13:01:24 -07:00
|
|
|
console_subscriber::init();
|
2020-05-28 13:28:58 -07:00
|
|
|
|
2020-11-05 09:38:03 -06:00
|
|
|
if check_env_vars() {
|
|
|
|
|
error!("Some environment variables are missing!");
|
|
|
|
|
}
|
2020-07-03 12:44:39 -05:00
|
|
|
|
2022-11-20 19:50:14 -07:00
|
|
|
// DSN is from SENTRY_DSN env variable.
|
|
|
|
|
// Has no effect if not set.
|
2023-02-01 10:08:02 -07:00
|
|
|
let sentry = sentry::init(sentry::ClientOptions {
|
|
|
|
|
release: sentry::release_name!(),
|
|
|
|
|
traces_sample_rate: 0.1,
|
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
2022-11-20 19:50:14 -07:00
|
|
|
if sentry.is_enabled() {
|
|
|
|
|
info!("Enabled Sentry integration");
|
2025-05-09 07:27:55 -05:00
|
|
|
unsafe {
|
|
|
|
|
std::env::set_var("RUST_BACKTRACE", "1");
|
|
|
|
|
}
|
2022-11-20 19:50:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-15 09:28:20 -05:00
|
|
|
if args.run_background_task.is_none() {
|
|
|
|
|
info!(
|
|
|
|
|
"Starting Labrinth on {}",
|
|
|
|
|
dotenvy::var("BIND_ADDR").unwrap()
|
|
|
|
|
);
|
2022-02-21 18:57:40 -08:00
|
|
|
|
2025-03-25 01:10:43 -07:00
|
|
|
if !args.no_migrations {
|
|
|
|
|
database::check_for_migrations()
|
|
|
|
|
.await
|
|
|
|
|
.expect("An error occurred while running migrations.");
|
|
|
|
|
}
|
2025-03-15 08:32:45 -07:00
|
|
|
}
|
2020-08-28 17:48:01 +02:00
|
|
|
|
2020-07-23 22:46:33 +02:00
|
|
|
// Database Connector
|
|
|
|
|
let pool = database::connect()
|
2020-07-03 12:44:39 -05:00
|
|
|
.await
|
|
|
|
|
.expect("Database connection failed");
|
2020-07-16 10:16:35 -07:00
|
|
|
|
2023-07-07 12:20:16 -07:00
|
|
|
// Redis connector
|
2023-10-06 09:57:33 -07:00
|
|
|
let redis_pool = RedisPool::new(None);
|
2023-07-07 12:20:16 -07:00
|
|
|
|
2024-10-18 16:07:35 -07:00
|
|
|
let storage_backend =
|
|
|
|
|
dotenvy::var("STORAGE_BACKEND").unwrap_or_else(|_| "local".to_string());
|
|
|
|
|
|
|
|
|
|
let file_host: Arc<dyn file_hosting::FileHost + Send + Sync> =
|
|
|
|
|
match storage_backend.as_str() {
|
|
|
|
|
"backblaze" => Arc::new(
|
|
|
|
|
file_hosting::BackblazeHost::new(
|
|
|
|
|
&dotenvy::var("BACKBLAZE_KEY_ID").unwrap(),
|
|
|
|
|
&dotenvy::var("BACKBLAZE_KEY").unwrap(),
|
|
|
|
|
&dotenvy::var("BACKBLAZE_BUCKET_ID").unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.await,
|
|
|
|
|
),
|
|
|
|
|
"s3" => Arc::new(
|
|
|
|
|
S3Host::new(
|
|
|
|
|
&dotenvy::var("S3_BUCKET_NAME").unwrap(),
|
|
|
|
|
&dotenvy::var("S3_REGION").unwrap(),
|
|
|
|
|
&dotenvy::var("S3_URL").unwrap(),
|
|
|
|
|
&dotenvy::var("S3_ACCESS_TOKEN").unwrap(),
|
|
|
|
|
&dotenvy::var("S3_SECRET").unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
),
|
|
|
|
|
"local" => Arc::new(file_hosting::MockHost::new()),
|
|
|
|
|
_ => panic!("Invalid storage backend specified. Aborting startup!"),
|
|
|
|
|
};
|
2020-07-16 10:16:35 -07:00
|
|
|
|
2023-08-02 14:43:04 -07:00
|
|
|
info!("Initializing clickhouse connection");
|
2023-10-06 09:57:33 -07:00
|
|
|
let mut clickhouse = clickhouse::init_client().await.unwrap();
|
2020-11-27 10:57:04 -07:00
|
|
|
|
2025-03-15 09:28:20 -05:00
|
|
|
let search_config = search::SearchConfig::new(None);
|
|
|
|
|
|
|
|
|
|
let stripe_client =
|
|
|
|
|
stripe::Client::new(dotenvy::var("STRIPE_API_KEY").unwrap());
|
|
|
|
|
|
|
|
|
|
if let Some(task) = args.run_background_task {
|
|
|
|
|
info!("Running task {task:?} and exiting");
|
|
|
|
|
task.run(pool, redis_pool, search_config, clickhouse, stripe_client)
|
|
|
|
|
.await;
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-18 16:07:35 -07:00
|
|
|
let maxmind_reader =
|
|
|
|
|
Arc::new(queue::maxmind::MaxMindIndexer::new().await.unwrap());
|
2022-11-20 19:50:14 -07:00
|
|
|
|
2023-10-06 17:58:02 -05:00
|
|
|
let prometheus = PrometheusMetricsBuilder::new("labrinth")
|
|
|
|
|
.endpoint("/metrics")
|
2025-03-05 15:40:46 -08:00
|
|
|
.exclude_regex(r"^/api/v1/.*$")
|
|
|
|
|
.exclude_regex(r"^/maven/.*$")
|
2024-12-27 22:44:09 -07:00
|
|
|
.exclude("/_internal/launcher_socket")
|
2025-03-05 15:40:46 -08:00
|
|
|
.mask_unmatched_patterns("UNKNOWN")
|
2023-10-06 17:58:02 -05:00
|
|
|
.build()
|
|
|
|
|
.expect("Failed to create prometheus metrics middleware");
|
|
|
|
|
|
2025-03-07 17:16:45 -08:00
|
|
|
database::register_and_set_metrics(&pool, &prometheus.registry)
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to register database metrics");
|
|
|
|
|
redis_pool
|
|
|
|
|
.register_and_set_metrics(&prometheus.registry)
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to register redis metrics");
|
2025-03-10 16:45:36 -05:00
|
|
|
|
2025-05-05 21:38:10 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
2025-05-15 22:47:29 +02:00
|
|
|
labrinth::routes::debug::jemalloc_memory_stats(&prometheus.registry)
|
2025-03-09 14:41:19 -07:00
|
|
|
.expect("Failed to register jemalloc metrics");
|
2025-03-07 17:16:45 -08:00
|
|
|
|
2024-04-22 13:09:35 -07:00
|
|
|
let labrinth_config = labrinth::app_setup(
|
2023-10-06 09:57:33 -07:00
|
|
|
pool.clone(),
|
|
|
|
|
redis_pool.clone(),
|
2024-01-05 08:20:56 -08:00
|
|
|
search_config.clone(),
|
2023-10-06 09:57:33 -07:00
|
|
|
&mut clickhouse,
|
|
|
|
|
file_host.clone(),
|
|
|
|
|
maxmind_reader.clone(),
|
2025-03-15 09:28:20 -05:00
|
|
|
stripe_client,
|
|
|
|
|
!args.no_background_tasks,
|
2023-10-06 09:57:33 -07:00
|
|
|
);
|
|
|
|
|
|
2024-03-27 15:56:29 -07:00
|
|
|
info!("Starting Actix HTTP server!");
|
|
|
|
|
|
2020-07-23 22:46:33 +02:00
|
|
|
// Init App
|
2020-05-09 22:11:48 -07:00
|
|
|
HttpServer::new(move || {
|
|
|
|
|
App::new()
|
2025-03-09 13:01:24 -07:00
|
|
|
.wrap(TracingLogger::default())
|
2023-10-06 17:58:02 -05:00
|
|
|
.wrap(prometheus.clone())
|
2025-03-25 01:10:43 -07:00
|
|
|
.wrap(from_fn(rate_limit_middleware))
|
2023-02-15 15:05:28 -07:00
|
|
|
.wrap(actix_web::middleware::Compress::default())
|
2023-08-02 14:43:04 -07:00
|
|
|
.wrap(sentry_actix::Sentry::new())
|
2023-10-06 09:57:33 -07:00
|
|
|
.configure(|cfg| labrinth::app_config(cfg, labrinth_config.clone()))
|
2020-05-09 22:11:48 -07:00
|
|
|
})
|
2022-10-22 21:23:31 -07:00
|
|
|
.bind(dotenvy::var("BIND_ADDR").unwrap())?
|
2020-05-27 20:51:28 +02:00
|
|
|
.run()
|
|
|
|
|
.await
|
2020-05-09 22:11:48 -07:00
|
|
|
}
|