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,
24 rate_limit::{rate_limiter::RateLimiter, RateLimit},
25 request::build_user_agent,
26 settings::structs::Settings,
29 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
31 use std::{env, sync::Arc, thread};
32 use tokio::sync::Mutex;
33 use tracing_actix_web::TracingLogger;
38 async fn main() -> Result<(), LemmyError> {
39 let args: Vec<String> = env::args().collect();
40 if args.len() == 2 && args[1] == "--print-config-docs" {
41 let fmt = Formatting {
42 auto_comments: AutoComments::none(),
45 println!("{}", doku::to_json_fmt_val(&fmt, &Settings::default()));
51 let settings = Settings::init().expect("Couldn't initialize settings.");
53 // Set up the r2d2 connection pool
54 let db_url = match get_database_url_from_env() {
56 Err(_) => settings.get_database_url(),
58 let manager = ConnectionManager::<PgConnection>::new(&db_url);
59 let pool = Pool::builder()
60 .max_size(settings.database.pool_size)
62 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
64 // Run the migrations from code
65 let protocol_and_hostname = settings.get_protocol_and_hostname();
66 blocking(&pool, move |conn| {
67 embedded_migrations::run(conn)?;
68 run_advanced_migrations(conn, &protocol_and_hostname)?;
69 Ok(()) as Result<(), LemmyError>
73 let pool2 = pool.clone();
74 thread::spawn(move || {
75 scheduled_tasks::setup(pool2).expect("Couldn't set up scheduled_tasks");
78 // Set up the rate limiter
79 let rate_limiter = RateLimit {
80 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
81 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
84 // Initialize the secrets
85 let conn = pool.get()?;
86 let secret = Secret::init(&conn).expect("Couldn't initialize secrets.");
89 "Starting http server at {}:{}",
90 settings.bind, settings.port
93 let client = Client::builder()
94 .user_agent(build_user_agent(&settings))
97 let activity_queue = create_activity_queue();
99 let chat_server = ChatServer::startup(
101 rate_limiter.clone(),
102 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
103 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
105 activity_queue.clone(),
111 // Create Http server with websocket support
112 let settings_bind = settings.clone();
113 HttpServer::new(move || {
114 let context = LemmyContext::create(
116 chat_server.to_owned(),
118 activity_queue.to_owned(),
122 let rate_limiter = rate_limiter.clone();
124 .wrap(TracingLogger::default())
125 .app_data(Data::new(context))
127 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
128 .configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
129 .configure(feeds::config)
130 .configure(|cfg| images::config(cfg, &rate_limiter))
131 .configure(nodeinfo::config)
132 .configure(|cfg| webfinger::config(cfg, &settings))
134 .bind((settings_bind.bind, settings_bind.port))?