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::{defaults::DEFAULT_DATABASE_POOL_SIZE, 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()
45 .unwrap_or(DEFAULT_DATABASE_POOL_SIZE),
48 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
50 // Run the migrations from code
51 blocking(&pool, move |conn| {
52 embedded_migrations::run(conn)?;
53 run_advanced_migrations(conn)?;
54 Ok(()) as Result<(), LemmyError>
58 let pool2 = pool.clone();
59 thread::spawn(move || {
60 scheduled_tasks::setup(pool2);
63 // Set up the rate limiter
64 let rate_limiter = RateLimit {
65 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
69 "Starting http server at {}:{}",
74 let activity_queue = create_activity_queue();
75 let chat_server = ChatServer::startup(
78 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
79 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
81 activity_queue.clone(),
85 // Create Http server with websocket support
86 HttpServer::new(move || {
87 let context = LemmyContext::create(
89 chat_server.to_owned(),
91 activity_queue.to_owned(),
93 let rate_limiter = rate_limiter.clone();
95 .wrap(middleware::Logger::default())
98 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
99 .configure(lemmy_apub_receive::routes::config)
100 .configure(feeds::config)
101 .configure(|cfg| images::config(cfg, &rate_limiter))
102 .configure(nodeinfo::config)
103 .configure(webfinger::config)
105 .bind((settings.bind(), settings.port()))?