2 extern crate diesel_migrations;
5 use actix_web::{web::Data, *};
7 r2d2::{ConnectionManager, Pool},
10 use doku::json::{AutoComments, Formatting};
11 use lemmy_api::match_websocket_operation;
12 use lemmy_api_common::{blocking, check_private_instance_and_federation_enabled};
13 use lemmy_api_crud::match_websocket_operation_crud;
14 use lemmy_apub_lib::activity_queue::create_activity_queue;
15 use lemmy_db_schema::{get_database_url_from_env, source::secret::Secret};
16 use lemmy_routes::{feeds, images, nodeinfo, webfinger};
19 code_migrations::run_advanced_migrations,
21 root_span_builder::QuieterRootSpanBuilder,
25 rate_limit::{rate_limiter::RateLimiter, RateLimit},
26 request::build_user_agent,
27 settings::structs::Settings,
30 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
32 use reqwest_middleware::ClientBuilder;
33 use reqwest_tracing::TracingMiddleware;
34 use std::{env, sync::Arc, thread, time::Duration};
35 use tokio::sync::Mutex;
36 use tracing_actix_web::TracingLogger;
41 async fn main() -> Result<(), LemmyError> {
42 let args: Vec<String> = env::args().collect();
43 if args.len() == 2 && args[1] == "--print-config-docs" {
44 let fmt = Formatting {
45 auto_comments: AutoComments::none(),
48 println!("{}", doku::to_json_fmt_val(&fmt, &Settings::default()));
52 let settings = Settings::init().expect("Couldn't initialize settings.");
54 init_tracing(settings.opentelemetry_url.as_deref())?;
56 // Set up the r2d2 connection pool
57 let db_url = match get_database_url_from_env() {
59 Err(_) => settings.get_database_url(),
61 let manager = ConnectionManager::<PgConnection>::new(&db_url);
62 let pool = Pool::builder()
63 .max_size(settings.database.pool_size)
65 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
67 // Run the migrations from code
68 let protocol_and_hostname = settings.get_protocol_and_hostname();
69 blocking(&pool, move |conn| {
70 embedded_migrations::run(conn)?;
71 run_advanced_migrations(conn, &protocol_and_hostname)?;
72 Ok(()) as Result<(), LemmyError>
76 // Schedules various cleanup tasks for the DB
77 let pool2 = pool.clone();
78 thread::spawn(move || {
79 scheduled_tasks::setup(pool2).expect("Couldn't set up scheduled_tasks");
82 // Set up the rate limiter
83 let rate_limiter = RateLimit {
84 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
85 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
88 // Initialize the secrets
89 let conn = pool.get()?;
90 let secret = Secret::init(&conn).expect("Couldn't initialize secrets.");
93 "Starting http server at {}:{}",
94 settings.bind, settings.port
97 let client = Client::builder()
98 .user_agent(build_user_agent(&settings))
99 .timeout(Duration::from_secs(10))
102 let client = ClientBuilder::new(client).with(TracingMiddleware).build();
104 let queue_manager = create_activity_queue(client.clone(), settings.federation.worker_count);
106 let activity_queue = queue_manager.queue_handle().clone();
108 check_private_instance_and_federation_enabled(&pool, &settings).await?;
110 let chat_server = ChatServer::startup(
112 rate_limiter.clone(),
113 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
114 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
116 activity_queue.clone(),
122 // Create Http server with websocket support
123 let settings_bind = settings.clone();
124 HttpServer::new(move || {
125 let context = LemmyContext::create(
127 chat_server.to_owned(),
129 activity_queue.to_owned(),
133 let rate_limiter = rate_limiter.clone();
135 .wrap(actix_web::middleware::Logger::default())
136 .wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
137 .app_data(Data::new(context))
139 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
140 .configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
141 .configure(feeds::config)
142 .configure(|cfg| images::config(cfg, client.clone(), &rate_limiter))
143 .configure(nodeinfo::config)
144 .configure(|cfg| webfinger::config(cfg, &settings))
146 .bind((settings_bind.bind, settings_bind.port))?