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};
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 let pool2 = pool.clone();
77 thread::spawn(move || {
78 scheduled_tasks::setup(pool2).expect("Couldn't set up scheduled_tasks");
81 // Set up the rate limiter
82 let rate_limiter = RateLimit {
83 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
84 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
87 // Initialize the secrets
88 let conn = pool.get()?;
89 let secret = Secret::init(&conn).expect("Couldn't initialize secrets.");
92 "Starting http server at {}:{}",
93 settings.bind, settings.port
96 let client = Client::builder()
97 .user_agent(build_user_agent(&settings))
100 let client = ClientBuilder::new(client).with(TracingMiddleware).build();
102 let queue_manager = create_activity_queue(client.clone());
104 let activity_queue = queue_manager.queue_handle().clone();
106 check_private_instance_and_federation_enabled(&pool, &settings).await?;
108 let chat_server = ChatServer::startup(
110 rate_limiter.clone(),
111 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
112 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
114 activity_queue.clone(),
120 // Create Http server with websocket support
121 let settings_bind = settings.clone();
122 HttpServer::new(move || {
123 let context = LemmyContext::create(
125 chat_server.to_owned(),
127 activity_queue.to_owned(),
131 let rate_limiter = rate_limiter.clone();
133 .wrap(actix_web::middleware::Logger::default())
134 .wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
135 .app_data(Data::new(context))
137 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
138 .configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
139 .configure(feeds::config)
140 .configure(|cfg| images::config(cfg, client.clone(), &rate_limiter))
141 .configure(nodeinfo::config)
142 .configure(|cfg| webfinger::config(cfg, &settings))
144 .bind((settings_bind.bind, settings_bind.port))?