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_server::{code_migrations::run_advanced_migrations, routes::*};
14 use lemmy_structs::blocking;
16 rate_limit::{rate_limiter::RateLimiter, RateLimit},
20 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
23 use tokio::sync::Mutex;
28 async fn main() -> Result<(), LemmyError> {
30 let settings = Settings::get();
32 // Set up the r2d2 connection pool
33 let db_url = match get_database_url_from_env() {
35 Err(_) => settings.get_database_url(),
37 let manager = ConnectionManager::<PgConnection>::new(&db_url);
38 let pool = Pool::builder()
39 .max_size(settings.database.pool_size)
41 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
43 // Run the migrations from code
44 blocking(&pool, move |conn| {
45 embedded_migrations::run(conn)?;
46 run_advanced_migrations(conn)?;
47 Ok(()) as Result<(), LemmyError>
51 // Set up the rate limiter
52 let rate_limiter = RateLimit {
53 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
57 "Starting http server at {}:{}",
58 settings.bind, settings.port
61 let activity_queue = create_activity_queue();
62 let chat_server = ChatServer::startup(
65 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
67 activity_queue.clone(),
71 // Create Http server with websocket support
72 HttpServer::new(move || {
73 let context = LemmyContext::create(
75 chat_server.to_owned(),
77 activity_queue.to_owned(),
79 let rate_limiter = rate_limiter.clone();
81 .wrap(middleware::Logger::default())
84 .configure(|cfg| api::config(cfg, &rate_limiter))
85 .configure(federation::config)
86 .configure(feeds::config)
87 .configure(|cfg| images::config(cfg, &rate_limiter))
88 .configure(nodeinfo::config)
89 .configure(webfinger::config)
90 .service(actix_files::Files::new("/docs", Settings::get().docs_dir))
92 .bind((settings.bind, settings.port))?