]> Untitled Git - lemmy.git/blobdiff - src/lib.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / src / lib.rs
index f200baf32f311f831be8a282cb47a5966d3251c8..55bb91606341cdbfb8c4747cea93a884108c97fc 100644 (file)
@@ -1,6 +1,7 @@
 pub mod api_routes_http;
-pub mod api_routes_websocket;
 pub mod code_migrations;
+#[cfg(feature = "prometheus-metrics")]
+pub mod prometheus_metrics;
 pub mod root_span_builder;
 pub mod scheduled_tasks;
 #[cfg(feature = "console")]
@@ -8,9 +9,14 @@ 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 actix_cors::Cors;
+use actix_web::{
+  middleware::{self, ErrorHandlers},
+  web::Data,
+  App,
+  HttpServer,
+  Result,
+};
 use lemmy_api_common::{
   context::LemmyContext,
   lemmy_db_views::structs::SiteView,
@@ -19,7 +25,6 @@ use lemmy_api_common::{
     check_private_instance_and_federation_enabled,
     local_site_rate_limit_to_rate_limit_config,
   },
-  websocket::chat_server::ChatServer,
 };
 use lemmy_apub::{VerifyUrlData, FEDERATION_HTTP_FETCH_LIMIT};
 use lemmy_db_schema::{
@@ -30,7 +35,9 @@ use lemmy_routes::{feeds, images, nodeinfo, webfinger};
 use lemmy_utils::{
   error::LemmyError,
   rate_limit::RateLimitCell,
-  settings::{structs::Settings, SETTINGS},
+  response::jsonify_plain_text_errors,
+  settings::SETTINGS,
+  SYNCHRONOUS_FEDERATION,
 };
 use reqwest::Client;
 use reqwest_middleware::ClientBuilder;
@@ -42,6 +49,12 @@ use tracing_error::ErrorLayer;
 use tracing_log::LogTracer;
 use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry};
 use url::Url;
+#[cfg(feature = "prometheus-metrics")]
+use {
+  actix_web_prom::PrometheusMetricsBuilder,
+  prometheus::default_registry,
+  prometheus_metrics::serve_prometheus,
+};
 
 /// Max timeout for http requests
 pub(crate) const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);
@@ -49,21 +62,8 @@ 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.get(1) == Some(&"--print-config-docs".to_string()) {
-    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(());
-  }
+
+  let scheduled_tasks_enabled = args.get(1) != Some(&"--disable-scheduled-tasks".to_string());
 
   let settings = SETTINGS.to_owned();
 
@@ -75,15 +75,15 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {
   let pool = build_db_pool(&settings).await?;
 
   // Run the Code-required migrations
-  run_advanced_migrations(&pool, &settings).await?;
+  run_advanced_migrations(&mut (&pool).into(), &settings).await?;
 
   // Initialize the secrets
-  let secret = Secret::init(&pool)
+  let secret = Secret::init(&mut (&pool).into())
     .await
     .expect("Couldn't initialize secrets.");
 
   // Make sure the local site is set up.
-  let site_view = SiteView::read_local(&pool)
+  let site_view = SiteView::read_local(&mut (&pool).into())
     .await
     .expect("local site not set up");
   let local_site = site_view.local_site;
@@ -121,43 +121,83 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {
     .with(TracingMiddleware::default())
     .build();
 
-  // 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 context = LemmyContext::create(
+    pool.clone(),
+    client.clone(),
+    secret.clone(),
+    rate_limit_cell.clone(),
+  );
+
+  if scheduled_tasks_enabled {
+    // Schedules various cleanup tasks for the DB
+    thread::spawn({
+      let context = context.clone();
+      move || {
+        scheduled_tasks::setup(db_url, user_agent, context)
+          .expect("Couldn't set up scheduled_tasks");
+      }
+    });
+  }
 
-  let chat_server = ChatServer::default().start();
+  #[cfg(feature = "prometheus-metrics")]
+  serve_prometheus(settings.prometheus.as_ref(), context.clone());
 
-  // Create Http server with websocket support
   let settings_bind = settings.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(settings.worker_count)
+    .retry_count(settings.retry_count)
+    .debug(*SYNCHRONOUS_FEDERATION)
+    .http_signature_compat(true)
+    .url_verifier(Box::new(VerifyUrlData(context.inner_pool().clone())))
+    .build()
+    .await?;
+
+  // this must come before the HttpServer creation
+  // creates a middleware that populates http metrics for each path, method, and status code
+  #[cfg(feature = "prometheus-metrics")]
+  let prom_api_metrics = PrometheusMetricsBuilder::new("lemmy_api")
+    .registry(default_registry().clone())
+    .build()
+    .expect("Should always be buildable");
+
+  // Create Http server with websocket support
   HttpServer::new(move || {
-    let context = LemmyContext::create(
-      pool.clone(),
-      chat_server.clone(),
-      client.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())
+    let cors_origin = std::env::var("LEMMY_CORS_ORIGIN");
+    let cors_config = match (cors_origin, cfg!(debug_assertions)) {
+      (Ok(origin), false) => Cors::default()
+        .allowed_origin(&origin)
+        .allowed_origin(&settings.get_protocol_and_hostname()),
+      _ => Cors::default()
+        .allow_any_origin()
+        .allow_any_method()
+        .allow_any_header()
+        .expose_any_header()
+        .max_age(3600),
+    };
+
+    let app = App::new()
+      .wrap(middleware::Logger::new(
+        // This is the default log format save for the usage of %{r}a over %a to guarantee to record the client's (forwarded) IP and not the last peer address, since the latter is frequently just a reverse proxy
+        "%{r}a '%r' %s %b '%{Referer}i' '%{User-Agent}i' %T",
+      ))
+      .wrap(middleware::Compress::default())
+      .wrap(cors_config)
       .wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
-      .app_data(Data::new(context))
+      .wrap(ErrorHandlers::new().default_handler(jsonify_plain_text_errors))
+      .app_data(Data::new(context.clone()))
       .app_data(Data::new(rate_limit_cell.clone()))
-      .wrap(FederationMiddleware::new(federation_config))
-      // The routes
+      .wrap(FederationMiddleware::new(federation_config.clone()));
+
+    #[cfg(feature = "prometheus-metrics")]
+    let app = app.wrap(prom_api_metrics.clone());
+
+    // The routes
+    app
       .configure(|cfg| api_routes_http::config(cfg, rate_limit_cell))
       .configure(|cfg| {
         if federation_enabled {
@@ -186,7 +226,14 @@ pub fn init_logging(opentelemetry_url: &Option<Url>) -> Result<(), LemmyError> {
     .trim_matches('"')
     .parse::<Targets>()?;
 
-  let format_layer = tracing_subscriber::fmt::layer().with_filter(targets.clone());
+  let format_layer = {
+    #[cfg(feature = "json-log")]
+    let layer = tracing_subscriber::fmt::layer().json();
+    #[cfg(not(feature = "json-log"))]
+    let layer = tracing_subscriber::fmt::layer();
+
+    layer.with_filter(targets.clone())
+  };
 
   let subscriber = Registry::default()
     .with(format_layer)