]> Untitled Git - lemmy.git/blobdiff - src/lib.rs
Remove websocket code (#2878)
[lemmy.git] / src / lib.rs
index 3c6b283eef3499e4ed20d24788e26084b4f2bd1f..382310b7cba08915c3fc5e43dcd9cce1cf25d870 100644 (file)
@@ -1,5 +1,4 @@
 pub mod api_routes_http;
-pub mod api_routes_websocket;
 pub mod code_migrations;
 pub mod root_span_builder;
 pub mod scheduled_tasks;
@@ -7,6 +6,8 @@ pub mod scheduled_tasks;
 pub mod telemetry;
 
 use crate::{code_migrations::run_advanced_migrations, root_span_builder::QuieterRootSpanBuilder};
+use activitypub_federation::config::{FederationConfig, FederationMiddleware};
+use actix::Actor;
 use actix_web::{middleware, web::Data, App, HttpServer, Result};
 use doku::json::{AutoComments, CommentsStyle, Formatting, ObjectsStyle};
 use lemmy_api_common::{
@@ -19,6 +20,7 @@ use lemmy_api_common::{
   },
   websocket::chat_server::ChatServer,
 };
+use lemmy_apub::{VerifyUrlData, FEDERATION_HTTP_FETCH_LIMIT};
 use lemmy_db_schema::{
   source::secret::Secret,
   utils::{build_db_pool, get_database_url, run_migrations},
@@ -31,9 +33,8 @@ use lemmy_utils::{
 };
 use reqwest::Client;
 use reqwest_middleware::ClientBuilder;
-use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
 use reqwest_tracing::TracingMiddleware;
-use std::{env, sync::Arc, thread, time::Duration};
+use std::{env, thread, time::Duration};
 use tracing::subscriber::set_global_default;
 use tracing_actix_web::TracingLogger;
 use tracing_error::ErrorLayer;
@@ -42,12 +43,12 @@ use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry}
 use url::Url;
 
 /// Max timeout for http requests
-const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);
+pub(crate) const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);
 
 /// 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();
-  if args.len() == 2 && args[1] == "--print-config-docs" {
+  if args.get(1) == Some(&"--print-config-docs".to_string()) {
     let fmt = Formatting {
       auto_comments: AutoComments::none(),
       comments_style: CommentsStyle {
@@ -65,18 +66,15 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {
 
   let settings = SETTINGS.to_owned();
 
-  // Set up the bb8 connection pool
+  // Run the DB migrations
   let db_url = get_database_url(Some(&settings));
   run_migrations(&db_url);
 
-  // Run the migrations from code
+  // Set up the connection pool
   let pool = build_db_pool(&settings).await?;
-  run_advanced_migrations(&pool, &settings).await?;
 
-  // Schedules various cleanup tasks for the DB
-  thread::spawn(move || {
-    scheduled_tasks::setup(db_url).expect("Couldn't set up scheduled_tasks");
-  });
+  // Run the Code-required migrations
+  run_advanced_migrations(&pool, &settings).await?;
 
   // Initialize the secrets
   let secret = Secret::init(&pool)
@@ -106,21 +104,15 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {
     settings.bind, settings.port
   );
 
+  let user_agent = build_user_agent(&settings);
   let reqwest_client = Client::builder()
-    .user_agent(build_user_agent(&settings))
+    .user_agent(user_agent.clone())
     .timeout(REQWEST_TIMEOUT)
+    .connect_timeout(REQWEST_TIMEOUT)
     .build()?;
 
-  let retry_policy = ExponentialBackoff {
-    max_n_retries: 3,
-    max_retry_interval: REQWEST_TIMEOUT,
-    min_retry_interval: Duration::from_millis(100),
-    backoff_exponent: 2,
-  };
-
   let client = ClientBuilder::new(reqwest_client.clone())
     .with(TracingMiddleware::default())
-    .with(RetryTransientMiddleware::new_with_policy(retry_policy))
     .build();
 
   // Pictrs cannot use the retry middleware
@@ -128,7 +120,12 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {
     .with(TracingMiddleware::default())
     .build();
 
-  let chat_server = Arc::new(ChatServer::startup());
+  // Schedules various cleanup tasks for the DB
+  thread::spawn(move || {
+    scheduled_tasks::setup(db_url, user_agent).expect("Couldn't set up scheduled_tasks");
+  });
+
+  let chat_server = ChatServer::default().start();
 
   // Create Http server with websocket support
   let settings_bind = settings.clone();
@@ -137,15 +134,28 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {
       pool.clone(),
       chat_server.clone(),
       client.clone(),
-      settings.clone(),
       secret.clone(),
       rate_limit_cell.clone(),
     );
+
+    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");
+
     App::new()
       .wrap(middleware::Logger::default())
       .wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
       .app_data(Data::new(context))
       .app_data(Data::new(rate_limit_cell.clone()))
+      .wrap(FederationMiddleware::new(federation_config))
       // The routes
       .configure(|cfg| api_routes_http::config(cfg, rate_limit_cell))
       .configure(|cfg| {