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::get_database_url_from_env;
13 use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit};
14 use lemmy_server::{code_migrations::run_advanced_migrations, routes::*};
15 use lemmy_structs::blocking;
16 use lemmy_utils::{settings::Settings, LemmyError};
17 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
20 use tokio::sync::Mutex;
25 async fn main() -> Result<(), LemmyError> {
27 let settings = Settings::get();
29 // Set up the r2d2 connection pool
30 let db_url = match get_database_url_from_env() {
32 Err(_) => settings.get_database_url(),
34 let manager = ConnectionManager::<PgConnection>::new(&db_url);
35 let pool = Pool::builder()
36 .max_size(settings.database.pool_size)
38 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
40 // Run the migrations from code
41 blocking(&pool, move |conn| {
42 embedded_migrations::run(conn)?;
43 run_advanced_migrations(conn)?;
44 Ok(()) as Result<(), LemmyError>
48 // Set up the rate limiter
49 let rate_limiter = RateLimit {
50 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
54 "Starting http server at {}:{}",
55 settings.bind, settings.port
58 let activity_queue = create_activity_queue();
59 let chat_server = ChatServer::startup(
62 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
64 activity_queue.clone(),
68 // Create Http server with websocket support
69 HttpServer::new(move || {
70 let context = LemmyContext::create(
72 chat_server.to_owned(),
74 activity_queue.to_owned(),
76 let rate_limiter = rate_limiter.clone();
78 .wrap(middleware::Logger::default())
81 .configure(|cfg| api::config(cfg, &rate_limiter))
82 .configure(federation::config)
83 .configure(feeds::config)
84 .configure(|cfg| images::config(cfg, &rate_limiter))
85 .configure(nodeinfo::config)
86 .configure(webfinger::config)
87 .service(actix_files::Files::new("/docs", Settings::get().docs_dir))
89 .bind((settings.bind, settings.port))?