2 extern crate diesel_migrations;
7 r2d2::{ConnectionManager, Pool},
10 use lemmy_api::match_websocket_operation;
11 use lemmy_apub::activity_queue::create_activity_queue;
12 use lemmy_db_queries::get_database_url_from_env;
13 use lemmy_routes::{feeds, images, nodeinfo, webfinger};
14 use lemmy_server::{code_migrations::run_advanced_migrations, scheduled_tasks};
15 use lemmy_structs::blocking;
17 rate_limit::{rate_limiter::RateLimiter, RateLimit},
21 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
23 use std::{sync::Arc, thread};
24 use tokio::sync::Mutex;
29 async fn main() -> Result<(), LemmyError> {
31 let settings = Settings::get();
33 // Set up the r2d2 connection pool
34 let db_url = match get_database_url_from_env() {
36 Err(_) => settings.get_database_url(),
38 let manager = ConnectionManager::<PgConnection>::new(&db_url);
39 let pool = Pool::builder()
40 .max_size(settings.database.pool_size)
42 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
44 // Run the migrations from code
45 blocking(&pool, move |conn| {
46 embedded_migrations::run(conn)?;
47 run_advanced_migrations(conn)?;
48 Ok(()) as Result<(), LemmyError>
52 let pool2 = pool.clone();
53 thread::spawn(move || {
54 scheduled_tasks::setup(pool2);
57 // Set up the rate limiter
58 let rate_limiter = RateLimit {
59 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
63 "Starting http server at {}:{}",
64 settings.bind, settings.port
67 let activity_queue = create_activity_queue();
68 let chat_server = ChatServer::startup(
71 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
73 activity_queue.clone(),
77 // Create Http server with websocket support
78 HttpServer::new(move || {
79 let context = LemmyContext::create(
81 chat_server.to_owned(),
83 activity_queue.to_owned(),
85 let rate_limiter = rate_limiter.clone();
87 .wrap(middleware::Logger::default())
90 .configure(|cfg| lemmy_api::routes::config(cfg, &rate_limiter))
91 .configure(lemmy_apub::routes::config)
92 .configure(feeds::config)
93 .configure(|cfg| images::config(cfg, &rate_limiter))
94 .configure(nodeinfo::config)
95 .configure(webfinger::config)
97 .bind((settings.bind, settings.port))?