2 extern crate diesel_migrations;
7 r2d2::{ConnectionManager, Pool},
10 use lemmy_api::match_websocket_operation;
11 use lemmy_api_common::blocking;
12 use lemmy_api_crud::match_websocket_operation_crud;
13 use lemmy_apub::activity_queue::create_activity_queue;
14 use lemmy_db_queries::get_database_url_from_env;
15 use lemmy_routes::{feeds, images, nodeinfo, webfinger};
16 use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
18 rate_limit::{rate_limiter::RateLimiter, RateLimit},
19 settings::structs::Settings,
22 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
24 use std::{sync::Arc, thread};
25 use tokio::sync::Mutex;
30 async fn main() -> Result<(), LemmyError> {
32 let settings = Settings::get();
34 // Set up the r2d2 connection pool
35 let db_url = match get_database_url_from_env() {
37 Err(_) => settings.get_database_url(),
39 let manager = ConnectionManager::<PgConnection>::new(&db_url);
40 let pool = Pool::builder()
41 .max_size(settings.database().pool_size())
43 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
45 // Run the migrations from code
46 blocking(&pool, move |conn| {
47 embedded_migrations::run(conn)?;
48 run_advanced_migrations(conn)?;
49 Ok(()) as Result<(), LemmyError>
53 let pool2 = pool.clone();
54 thread::spawn(move || {
55 scheduled_tasks::setup(pool2);
58 // Set up the rate limiter
59 let rate_limiter = RateLimit {
60 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
64 "Starting http server at {}:{}",
69 let activity_queue = create_activity_queue();
70 let chat_server = ChatServer::startup(
73 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
74 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
76 activity_queue.clone(),
80 // Create Http server with websocket support
81 HttpServer::new(move || {
82 let context = LemmyContext::create(
84 chat_server.to_owned(),
86 activity_queue.to_owned(),
88 let rate_limiter = rate_limiter.clone();
90 .wrap(middleware::Logger::default())
93 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
94 .configure(lemmy_apub_receive::routes::config)
95 .configure(feeds::config)
96 .configure(|cfg| images::config(cfg, &rate_limiter))
97 .configure(nodeinfo::config)
98 .configure(webfinger::config)
100 .bind((settings.bind(), settings.port()))?