2 extern crate diesel_migrations;
5 use actix_web::{web::Data, *};
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, source::secret::Secret_};
15 use lemmy_db_schema::source::secret::Secret;
16 use lemmy_routes::{feeds, images, nodeinfo, webfinger};
17 use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
19 rate_limit::{rate_limiter::RateLimiter, RateLimit},
20 request::build_user_agent,
21 settings::structs::Settings,
24 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
26 use std::{sync::Arc, thread};
27 use tokio::sync::Mutex;
32 async fn main() -> Result<(), LemmyError> {
34 let settings = Settings::init().expect("Couldn't initialize settings.");
36 // Set up the r2d2 connection pool
37 let db_url = match get_database_url_from_env() {
39 Err(_) => settings.get_database_url(),
41 let manager = ConnectionManager::<PgConnection>::new(&db_url);
42 let pool = Pool::builder()
43 .max_size(settings.database.pool_size)
45 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
47 // Run the migrations from code
48 let protocol_and_hostname = settings.get_protocol_and_hostname();
49 blocking(&pool, move |conn| {
50 embedded_migrations::run(conn)?;
51 run_advanced_migrations(conn, &protocol_and_hostname)?;
52 Ok(()) as Result<(), LemmyError>
56 let pool2 = pool.clone();
57 thread::spawn(move || {
58 scheduled_tasks::setup(pool2);
61 // Set up the rate limiter
62 let rate_limiter = RateLimit {
63 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
64 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
67 // Initialize the secrets
68 let conn = pool.get()?;
69 let secret = Secret::init(&conn).expect("Couldn't initialize secrets.");
72 "Starting http server at {}:{}",
73 settings.bind, settings.port
76 let client = Client::builder()
77 .user_agent(build_user_agent(&settings))
80 let activity_queue = create_activity_queue();
82 let chat_server = ChatServer::startup(
85 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
86 |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
88 activity_queue.clone(),
94 // Create Http server with websocket support
95 let settings_bind = settings.clone();
96 HttpServer::new(move || {
97 let context = LemmyContext::create(
99 chat_server.to_owned(),
101 activity_queue.to_owned(),
105 let rate_limiter = rate_limiter.clone();
107 .wrap(middleware::Logger::default())
108 .app_data(Data::new(context))
110 .configure(|cfg| api_routes::config(cfg, &rate_limiter))
111 .configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
112 .configure(feeds::config)
113 .configure(|cfg| images::config(cfg, &rate_limiter))
114 .configure(nodeinfo::config)
115 .configure(|cfg| webfinger::config(cfg, &settings))
117 .bind((settings_bind.bind, settings_bind.port))?