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;
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 std::{env, sync::Arc, thread};
33 use tokio::sync::Mutex;
34 use tracing_actix_web::TracingLogger;
39 async fn main() -> Result<(), LemmyError> {
40 let args: Vec<String> = env::args().collect();
41 if args.len() == 2 && args[1] == "--print-config-docs" {
42 let fmt = Formatting {
43 auto_comments: AutoComments::none(),
46 println!("{}", doku::to_json_fmt_val(&fmt, &Settings::default()));
52 let settings = Settings::init().expect("Couldn't initialize settings.");
54 // Set up the r2d2 connection pool
55 let db_url = match get_database_url_from_env() {
57 Err(_) => settings.get_database_url(),
59 let manager = ConnectionManager::<PgConnection>::new(&db_url);
60 let pool = Pool::builder()
61 .max_size(settings.database.pool_size)
63 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
65 // Run the migrations from code
66 let protocol_and_hostname = settings.get_protocol_and_hostname();
67 blocking(&pool, move |conn| {
68 embedded_migrations::run(conn)?;
69 run_advanced_migrations(conn, &protocol_and_hostname)?;
70 Ok(()) as Result<(), LemmyError>
74 let pool2 = pool.clone();
75 thread::spawn(move || {
76 scheduled_tasks::setup(pool2).expect("Couldn't set up scheduled_tasks");
79 // Set up the rate limiter
80 let rate_limiter = RateLimit {
81 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
82 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
85 // Initialize the secrets
86 let conn = pool.get()?;
87 let secret = Secret::init(&conn).expect("Couldn't initialize secrets.");
90 "Starting http server at {}:{}",
91 settings.bind, settings.port
94 let client = Client::builder()
95 .user_agent(build_user_agent(&settings))
98 let queue_manager = create_activity_queue();
100 let activity_queue = queue_manager.queue_handle().clone();
102 let chat_server = ChatServer::startup(
104 rate_limiter.clone(),
105 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
106 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
108 activity_queue.clone(),
114 // Create Http server with websocket support
115 let settings_bind = settings.clone();
116 HttpServer::new(move || {
117 let context = LemmyContext::create(
119 chat_server.to_owned(),
121 activity_queue.to_owned(),
125 let rate_limiter = rate_limiter.clone();
127 .wrap(actix_web::middleware::Logger::default())
128 .wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
129 .app_data(Data::new(context))
131 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
132 .configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
133 .configure(feeds::config)
134 .configure(|cfg| images::config(cfg, &rate_limiter))
135 .configure(nodeinfo::config)
136 .configure(|cfg| webfinger::config(cfg, &settings))
138 .bind((settings_bind.bind, settings_bind.port))?