pub mod api_routes_http;
-pub mod api_routes_websocket;
pub mod code_migrations;
pub mod root_span_builder;
pub mod scheduled_tasks;
use crate::{code_migrations::run_advanced_migrations, root_span_builder::QuieterRootSpanBuilder};
use activitypub_federation::config::{FederationConfig, FederationMiddleware};
-use actix::Actor;
+use actix_cors::Cors;
use actix_web::{middleware, web::Data, App, HttpServer, Result};
use doku::json::{AutoComments, CommentsStyle, Formatting, ObjectsStyle};
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::{
};
use reqwest::Client;
use reqwest_middleware::ClientBuilder;
-use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use reqwest_tracing::TracingMiddleware;
use std::{env, thread, time::Duration};
use tracing::subscriber::set_global_default;
return Ok(());
}
+ let scheduled_tasks_enabled = args.get(1) != Some(&"--disable-scheduled-tasks".to_string());
+
let settings = SETTINGS.to_owned();
// Run the DB migrations
let reqwest_client = Client::builder()
.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
.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(),
+ );
- let chat_server = ChatServer::default().start();
+ 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");
+ }
+ });
+ }
// Create Http server with websocket support
let settings_bind = settings.clone();
HttpServer::new(move || {
- let context = LemmyContext::create(
- pool.clone(),
- chat_server.clone(),
- client.clone(),
- secret.clone(),
- rate_limit_cell.clone(),
- );
-
+ let context = context.clone();
let federation_config = FederationConfig::builder()
.domain(settings.hostname.clone())
.app_data(context.clone())
.build()
.expect("configure federation");
+ let cors_config = if cfg!(debug_assertions) {
+ Cors::permissive()
+ } else {
+ let cors_origin = std::env::var("LEMMY_CORS_ORIGIN").unwrap_or("http://localhost".into());
+ Cors::default()
+ .allowed_origin(&cors_origin)
+ .allowed_origin(&settings.get_protocol_and_hostname())
+ };
+
App::new()
- .wrap(middleware::Logger::default())
+ .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(cors_config)
.wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
.app_data(Data::new(context))
.app_data(Data::new(rate_limit_cell.clone()))