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};
17 use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
19 rate_limit::{rate_limiter::RateLimiter, RateLimit},
20 request::build_user_agent,
21 settings::structs::Settings,
24 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
26 use std::{env, sync::Arc, thread};
27 use tokio::sync::Mutex;
32 async fn main() -> Result<(), LemmyError> {
33 let args: Vec<String> = env::args().collect();
34 if args.len() == 2 && args[1] == "--print-config-docs" {
35 let fmt = Formatting {
36 auto_comments: AutoComments::none(),
39 println!("{}", doku::to_json_fmt_val(&fmt, &Settings::default()));
44 let settings = Settings::init().expect("Couldn't initialize settings.");
46 // Set up the r2d2 connection pool
47 let db_url = match get_database_url_from_env() {
49 Err(_) => settings.get_database_url(),
51 let manager = ConnectionManager::<PgConnection>::new(&db_url);
52 let pool = Pool::builder()
53 .max_size(settings.database.pool_size)
55 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
57 // Run the migrations from code
58 let protocol_and_hostname = settings.get_protocol_and_hostname();
59 blocking(&pool, move |conn| {
60 embedded_migrations::run(conn)?;
61 run_advanced_migrations(conn, &protocol_and_hostname)?;
62 Ok(()) as Result<(), LemmyError>
66 let pool2 = pool.clone();
67 thread::spawn(move || {
68 scheduled_tasks::setup(pool2).expect("Couldn't set up scheduled_tasks");
71 // Set up the rate limiter
72 let rate_limiter = RateLimit {
73 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
74 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
77 // Initialize the secrets
78 let conn = pool.get()?;
79 let secret = Secret::init(&conn).expect("Couldn't initialize secrets.");
82 "Starting http server at {}:{}",
83 settings.bind, settings.port
86 let client = Client::builder()
87 .user_agent(build_user_agent(&settings))
90 let activity_queue = create_activity_queue();
92 let chat_server = ChatServer::startup(
95 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
96 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
98 activity_queue.clone(),
104 // Create Http server with websocket support
105 let settings_bind = settings.clone();
106 HttpServer::new(move || {
107 let context = LemmyContext::create(
109 chat_server.to_owned(),
111 activity_queue.to_owned(),
115 let rate_limiter = rate_limiter.clone();
117 .wrap(middleware::Logger::default())
118 .app_data(Data::new(context))
120 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
121 .configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
122 .configure(feeds::config)
123 .configure(|cfg| images::config(cfg, &rate_limiter))
124 .configure(nodeinfo::config)
125 .configure(|cfg| webfinger::config(cfg, &settings))
127 .bind((settings_bind.bind, settings_bind.port))?