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_queries::{get_database_url_from_env, source::secret::Secret_};
16 use lemmy_db_schema::source::secret::Secret;
17 use lemmy_routes::{feeds, images, nodeinfo, webfinger};
18 use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
20 rate_limit::{rate_limiter::RateLimiter, RateLimit},
21 request::build_user_agent,
22 settings::structs::Settings,
25 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
27 use std::{env, sync::Arc, thread};
28 use tokio::sync::Mutex;
33 async fn main() -> Result<(), LemmyError> {
34 let args: Vec<String> = env::args().collect();
35 if args.len() == 2 && args[1] == "--print-config-docs" {
36 let fmt = Formatting {
37 auto_comments: AutoComments::none(),
40 println!("{}", doku::to_json_fmt_val(&fmt, &Settings::default()));
45 let settings = Settings::init().expect("Couldn't initialize settings.");
47 // Set up the r2d2 connection pool
48 let db_url = match get_database_url_from_env() {
50 Err(_) => settings.get_database_url(),
52 let manager = ConnectionManager::<PgConnection>::new(&db_url);
53 let pool = Pool::builder()
54 .max_size(settings.database.pool_size)
56 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
58 // Run the migrations from code
59 let protocol_and_hostname = settings.get_protocol_and_hostname();
60 blocking(&pool, move |conn| {
61 embedded_migrations::run(conn)?;
62 run_advanced_migrations(conn, &protocol_and_hostname)?;
63 Ok(()) as Result<(), LemmyError>
67 let pool2 = pool.clone();
68 thread::spawn(move || {
69 scheduled_tasks::setup(pool2);
72 // Set up the rate limiter
73 let rate_limiter = RateLimit {
74 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
75 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
78 // Initialize the secrets
79 let conn = pool.get()?;
80 let secret = Secret::init(&conn).expect("Couldn't initialize secrets.");
83 "Starting http server at {}:{}",
84 settings.bind, settings.port
87 let client = Client::builder()
88 .user_agent(build_user_agent(&settings))
91 let activity_queue = create_activity_queue();
93 let chat_server = ChatServer::startup(
96 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
97 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
99 activity_queue.clone(),
105 // Create Http server with websocket support
106 let settings_bind = settings.clone();
107 HttpServer::new(move || {
108 let context = LemmyContext::create(
110 chat_server.to_owned(),
112 activity_queue.to_owned(),
116 let rate_limiter = rate_limiter.clone();
118 .wrap(middleware::Logger::default())
119 .app_data(Data::new(context))
121 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
122 .configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
123 .configure(feeds::config)
124 .configure(|cfg| images::config(cfg, &rate_limiter))
125 .configure(nodeinfo::config)
126 .configure(|cfg| webfinger::config(cfg, &settings))
128 .bind((settings_bind.bind, settings_bind.port))?