2022-12-09 15:31:47 +00:00
|
|
|
pub mod api_routes_http;
|
2020-07-10 18:15:41 +00:00
|
|
|
pub mod code_migrations;
|
2021-12-06 14:54:47 +00:00
|
|
|
pub mod root_span_builder;
|
2021-01-29 16:38:27 +00:00
|
|
|
pub mod scheduled_tasks;
|
2022-02-04 17:31:38 +00:00
|
|
|
#[cfg(feature = "console")]
|
2022-05-10 12:06:32 +00:00
|
|
|
pub mod telemetry;
|
|
|
|
|
2022-12-19 15:54:42 +00:00
|
|
|
use crate::{code_migrations::run_advanced_migrations, root_span_builder::QuieterRootSpanBuilder};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::config::{FederationConfig, FederationMiddleware};
|
2023-05-29 21:14:00 +00:00
|
|
|
use actix_cors::Cors;
|
2022-12-19 15:54:42 +00:00
|
|
|
use actix_web::{middleware, web::Data, App, HttpServer, Result};
|
|
|
|
use doku::json::{AutoComments, CommentsStyle, Formatting, ObjectsStyle};
|
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
|
|
|
lemmy_db_views::structs::SiteView,
|
|
|
|
request::build_user_agent,
|
|
|
|
utils::{
|
|
|
|
check_private_instance_and_federation_enabled,
|
|
|
|
local_site_rate_limit_to_rate_limit_config,
|
|
|
|
},
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use lemmy_apub::{VerifyUrlData, FEDERATION_HTTP_FETCH_LIMIT};
|
2022-12-19 15:54:42 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::secret::Secret,
|
|
|
|
utils::{build_db_pool, get_database_url, run_migrations},
|
|
|
|
};
|
|
|
|
use lemmy_routes::{feeds, images, nodeinfo, webfinger};
|
|
|
|
use lemmy_utils::{
|
|
|
|
error::LemmyError,
|
|
|
|
rate_limit::RateLimitCell,
|
|
|
|
settings::{structs::Settings, SETTINGS},
|
|
|
|
};
|
|
|
|
use reqwest::Client;
|
|
|
|
use reqwest_middleware::ClientBuilder;
|
|
|
|
use reqwest_tracing::TracingMiddleware;
|
2023-04-13 10:53:55 +00:00
|
|
|
use std::{env, thread, time::Duration};
|
2021-11-23 12:16:47 +00:00
|
|
|
use tracing::subscriber::set_global_default;
|
2022-12-19 15:54:42 +00:00
|
|
|
use tracing_actix_web::TracingLogger;
|
2021-11-23 12:16:47 +00:00
|
|
|
use tracing_error::ErrorLayer;
|
|
|
|
use tracing_log::LogTracer;
|
2022-01-07 14:53:45 +00:00
|
|
|
use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry};
|
2022-07-11 20:38:37 +00:00
|
|
|
use url::Url;
|
2021-11-23 12:16:47 +00:00
|
|
|
|
2022-12-19 15:54:42 +00:00
|
|
|
/// Max timeout for http requests
|
2023-02-18 14:36:12 +00:00
|
|
|
pub(crate) const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);
|
2022-12-19 15:54:42 +00:00
|
|
|
|
|
|
|
/// Placing the main function in lib.rs allows other crates to import it and embed Lemmy
|
|
|
|
pub async fn start_lemmy_server() -> Result<(), LemmyError> {
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2023-02-28 11:34:50 +00:00
|
|
|
if args.get(1) == Some(&"--print-config-docs".to_string()) {
|
2022-12-19 15:54:42 +00:00
|
|
|
let fmt = Formatting {
|
|
|
|
auto_comments: AutoComments::none(),
|
|
|
|
comments_style: CommentsStyle {
|
|
|
|
separator: "#".to_owned(),
|
|
|
|
},
|
|
|
|
objects_style: ObjectsStyle {
|
|
|
|
surround_keys_with_quotes: false,
|
|
|
|
use_comma_as_separator: false,
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
println!("{}", doku::to_json_fmt_val(&fmt, &Settings::default()));
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-06-15 09:29:12 +00:00
|
|
|
let scheduled_tasks_enabled = args.get(1) != Some(&"--disable-scheduled-tasks".to_string());
|
|
|
|
|
2022-12-19 15:54:42 +00:00
|
|
|
let settings = SETTINGS.to_owned();
|
|
|
|
|
2023-02-28 21:45:37 +00:00
|
|
|
// Run the DB migrations
|
2022-12-19 15:54:42 +00:00
|
|
|
let db_url = get_database_url(Some(&settings));
|
|
|
|
run_migrations(&db_url);
|
|
|
|
|
2023-02-28 21:45:37 +00:00
|
|
|
// Set up the connection pool
|
2022-12-19 15:54:42 +00:00
|
|
|
let pool = build_db_pool(&settings).await?;
|
2023-02-28 21:45:37 +00:00
|
|
|
|
|
|
|
// Run the Code-required migrations
|
2022-12-19 15:54:42 +00:00
|
|
|
run_advanced_migrations(&pool, &settings).await?;
|
|
|
|
|
|
|
|
// Initialize the secrets
|
|
|
|
let secret = Secret::init(&pool)
|
|
|
|
.await
|
|
|
|
.expect("Couldn't initialize secrets.");
|
|
|
|
|
|
|
|
// Make sure the local site is set up.
|
|
|
|
let site_view = SiteView::read_local(&pool)
|
|
|
|
.await
|
|
|
|
.expect("local site not set up");
|
|
|
|
let local_site = site_view.local_site;
|
|
|
|
let federation_enabled = local_site.federation_enabled;
|
|
|
|
|
|
|
|
if federation_enabled {
|
|
|
|
println!("federation enabled, host is {}", &settings.hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_private_instance_and_federation_enabled(&local_site)?;
|
|
|
|
|
|
|
|
// Set up the rate limiter
|
|
|
|
let rate_limit_config =
|
|
|
|
local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit);
|
|
|
|
let rate_limit_cell = RateLimitCell::new(rate_limit_config).await;
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Starting http server at {}:{}",
|
|
|
|
settings.bind, settings.port
|
|
|
|
);
|
|
|
|
|
2023-02-18 14:36:12 +00:00
|
|
|
let user_agent = build_user_agent(&settings);
|
2022-12-19 15:54:42 +00:00
|
|
|
let reqwest_client = Client::builder()
|
2023-02-18 14:36:12 +00:00
|
|
|
.user_agent(user_agent.clone())
|
2022-12-19 15:54:42 +00:00
|
|
|
.timeout(REQWEST_TIMEOUT)
|
2023-05-18 11:11:06 +00:00
|
|
|
.connect_timeout(REQWEST_TIMEOUT)
|
2022-12-19 15:54:42 +00:00
|
|
|
.build()?;
|
|
|
|
|
|
|
|
let client = ClientBuilder::new(reqwest_client.clone())
|
|
|
|
.with(TracingMiddleware::default())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Pictrs cannot use the retry middleware
|
|
|
|
let pictrs_client = ClientBuilder::new(reqwest_client.clone())
|
|
|
|
.with(TracingMiddleware::default())
|
|
|
|
.build();
|
|
|
|
|
2023-06-15 09:29:12 +00:00
|
|
|
if scheduled_tasks_enabled {
|
|
|
|
// Schedules various cleanup tasks for the DB
|
|
|
|
thread::spawn(move || {
|
|
|
|
scheduled_tasks::setup(db_url, user_agent).expect("Couldn't set up scheduled_tasks");
|
|
|
|
});
|
|
|
|
}
|
2023-02-18 14:36:12 +00:00
|
|
|
|
2022-12-19 15:54:42 +00:00
|
|
|
// Create Http server with websocket support
|
|
|
|
let settings_bind = settings.clone();
|
|
|
|
HttpServer::new(move || {
|
|
|
|
let context = LemmyContext::create(
|
|
|
|
pool.clone(),
|
|
|
|
client.clone(),
|
|
|
|
secret.clone(),
|
|
|
|
rate_limit_cell.clone(),
|
|
|
|
);
|
2023-03-21 15:03:05 +00:00
|
|
|
|
|
|
|
let federation_config = FederationConfig::builder()
|
|
|
|
.domain(settings.hostname.clone())
|
|
|
|
.app_data(context.clone())
|
|
|
|
.client(client.clone())
|
|
|
|
.http_fetch_limit(FEDERATION_HTTP_FETCH_LIMIT)
|
|
|
|
.worker_count(local_site.federation_worker_count as u64)
|
|
|
|
.debug(cfg!(debug_assertions))
|
|
|
|
.http_signature_compat(true)
|
|
|
|
.url_verifier(Box::new(VerifyUrlData(context.pool().clone())))
|
|
|
|
.build()
|
|
|
|
.expect("configure federation");
|
|
|
|
|
2023-05-29 21:14:00 +00:00
|
|
|
let cors_config = if cfg!(debug_assertions) {
|
|
|
|
Cors::permissive()
|
|
|
|
} else {
|
|
|
|
Cors::default()
|
|
|
|
};
|
|
|
|
|
2022-12-19 15:54:42 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(middleware::Logger::default())
|
2023-05-29 21:14:00 +00:00
|
|
|
.wrap(cors_config)
|
2022-12-19 15:54:42 +00:00
|
|
|
.wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
|
|
|
|
.app_data(Data::new(context))
|
|
|
|
.app_data(Data::new(rate_limit_cell.clone()))
|
2023-03-21 15:03:05 +00:00
|
|
|
.wrap(FederationMiddleware::new(federation_config))
|
2022-12-19 15:54:42 +00:00
|
|
|
// The routes
|
|
|
|
.configure(|cfg| api_routes_http::config(cfg, rate_limit_cell))
|
|
|
|
.configure(|cfg| {
|
|
|
|
if federation_enabled {
|
|
|
|
lemmy_apub::http::routes::config(cfg);
|
|
|
|
webfinger::config(cfg);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.configure(feeds::config)
|
|
|
|
.configure(|cfg| images::config(cfg, pictrs_client.clone(), rate_limit_cell))
|
|
|
|
.configure(nodeinfo::config)
|
|
|
|
})
|
|
|
|
.bind((settings_bind.bind, settings_bind.port))?
|
|
|
|
.run()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-11 20:38:37 +00:00
|
|
|
pub fn init_logging(opentelemetry_url: &Option<Url>) -> Result<(), LemmyError> {
|
2021-11-23 12:16:47 +00:00
|
|
|
LogTracer::init()?;
|
|
|
|
|
2022-01-07 14:53:45 +00:00
|
|
|
let log_description = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into());
|
|
|
|
|
|
|
|
let targets = log_description
|
|
|
|
.trim()
|
|
|
|
.trim_matches('"')
|
|
|
|
.parse::<Targets>()?;
|
|
|
|
|
|
|
|
let format_layer = tracing_subscriber::fmt::layer().with_filter(targets.clone());
|
|
|
|
|
2021-11-23 12:16:47 +00:00
|
|
|
let subscriber = Registry::default()
|
|
|
|
.with(format_layer)
|
2022-02-04 17:31:38 +00:00
|
|
|
.with(ErrorLayer::default());
|
|
|
|
|
2022-05-10 12:06:32 +00:00
|
|
|
if let Some(_url) = opentelemetry_url {
|
|
|
|
#[cfg(feature = "console")]
|
2022-07-11 20:38:37 +00:00
|
|
|
telemetry::init_tracing(_url.as_ref(), subscriber, targets)?;
|
2022-05-10 12:06:32 +00:00
|
|
|
#[cfg(not(feature = "console"))]
|
|
|
|
tracing::error!("Feature `console` must be enabled for opentelemetry tracing");
|
2022-01-06 19:10:20 +00:00
|
|
|
} else {
|
|
|
|
set_global_default(subscriber)?;
|
|
|
|
}
|
2021-11-23 12:16:47 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|