2023-10-06 09:57:33 -07:00
|
|
|
use actix_web::{App, HttpServer};
|
2020-05-28 13:28:58 -07:00
|
|
|
use env_logger::Env;
|
2023-10-06 09:57:33 -07:00
|
|
|
use labrinth::database::redis::RedisPool;
|
|
|
|
|
use labrinth::file_hosting::S3Host;
|
|
|
|
|
use labrinth::ratelimit::errors::ARError;
|
|
|
|
|
use labrinth::ratelimit::memory::{MemoryStore, MemoryStoreActor};
|
|
|
|
|
use labrinth::ratelimit::middleware::RateLimiter;
|
|
|
|
|
use labrinth::util::env::parse_var;
|
|
|
|
|
use labrinth::{check_env_vars, clickhouse, database, file_hosting, queue};
|
|
|
|
|
use log::{error, info};
|
2020-05-10 22:30:28 -07:00
|
|
|
|
2023-10-06 09:57:33 -07:00
|
|
|
use std::sync::Arc;
|
2020-05-09 22:11:48 -07:00
|
|
|
|
2020-11-27 10:57:04 -07:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Pepper {
|
|
|
|
|
pub pepper: String,
|
|
|
|
|
}
|
|
|
|
|
|
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<()> {
|
2022-10-22 21:23:31 -07:00
|
|
|
dotenvy::dotenv().ok();
|
2023-04-20 16:38:30 -07:00
|
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("info")).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,
|
|
|
|
|
enable_profiling: true,
|
|
|
|
|
profiles_sample_rate: 0.1,
|
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
2022-11-20 19:50:14 -07:00
|
|
|
if sentry.is_enabled() {
|
|
|
|
|
info!("Enabled Sentry integration");
|
|
|
|
|
std::env::set_var("RUST_BACKTRACE", "1");
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-30 23:34:56 -07:00
|
|
|
info!(
|
|
|
|
|
"Starting Labrinth on {}",
|
|
|
|
|
dotenvy::var("BIND_ADDR").unwrap()
|
|
|
|
|
);
|
2022-02-21 18:57:40 -08:00
|
|
|
|
2020-09-06 08:19:53 -07:00
|
|
|
database::check_for_migrations()
|
|
|
|
|
.await
|
|
|
|
|
.expect("An error occurred while running migrations.");
|
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
|
|
|
|
2023-04-20 16:38:30 -07:00
|
|
|
let storage_backend = dotenvy::var("STORAGE_BACKEND").unwrap_or_else(|_| "local".to_string());
|
2020-07-23 22:46:33 +02:00
|
|
|
|
2023-04-20 16:38:30 -07:00
|
|
|
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
|
|
|
|
2023-10-06 09:57:33 -07:00
|
|
|
let maxmind_reader = Arc::new(queue::maxmind::MaxMindIndexer::new().await.unwrap());
|
2022-11-20 19:50:14 -07:00
|
|
|
|
2020-10-25 13:51:07 -07:00
|
|
|
let store = MemoryStore::new();
|
2021-09-21 06:26:16 +02:00
|
|
|
|
2020-05-28 13:28:58 -07:00
|
|
|
info!("Starting Actix HTTP server!");
|
2020-06-22 21:04:17 -07:00
|
|
|
|
2023-10-06 09:57:33 -07:00
|
|
|
let labrinth_config = labrinth::app_setup(
|
|
|
|
|
pool.clone(),
|
|
|
|
|
redis_pool.clone(),
|
|
|
|
|
&mut clickhouse,
|
|
|
|
|
file_host.clone(),
|
|
|
|
|
maxmind_reader.clone(),
|
|
|
|
|
);
|
|
|
|
|
|
2020-07-23 22:46:33 +02:00
|
|
|
// Init App
|
2020-05-09 22:11:48 -07:00
|
|
|
HttpServer::new(move || {
|
|
|
|
|
App::new()
|
2023-02-15 15:05:28 -07:00
|
|
|
.wrap(actix_web::middleware::Compress::default())
|
2022-02-20 20:16:32 -07:00
|
|
|
.wrap(
|
|
|
|
|
RateLimiter::new(MemoryStoreActor::from(store.clone()).start())
|
|
|
|
|
.with_identifier(|req| {
|
|
|
|
|
let connection_info = req.connection_info();
|
2023-04-20 16:38:30 -07:00
|
|
|
let ip =
|
|
|
|
|
String::from(if parse_var("CLOUDFLARE_INTEGRATION").unwrap_or(false) {
|
|
|
|
|
if let Some(header) = req.headers().get("CF-Connecting-IP") {
|
|
|
|
|
header.to_str().map_err(|_| ARError::Identification)?
|
2022-02-20 20:16:32 -07:00
|
|
|
} else {
|
2023-04-20 16:38:30 -07:00
|
|
|
connection_info.peer_addr().ok_or(ARError::Identification)?
|
2022-02-20 20:16:32 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-04-20 16:38:30 -07:00
|
|
|
connection_info.peer_addr().ok_or(ARError::Identification)?
|
|
|
|
|
});
|
2022-02-20 20:16:32 -07:00
|
|
|
|
|
|
|
|
Ok(ip)
|
|
|
|
|
})
|
|
|
|
|
.with_interval(std::time::Duration::from_secs(60))
|
|
|
|
|
.with_max_requests(300)
|
2023-04-20 16:38:30 -07:00
|
|
|
.with_ignore_key(dotenvy::var("RATE_LIMIT_IGNORE_KEY").ok()),
|
|
|
|
|
)
|
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
|
|
|
}
|