Federated blogging application, thanks to ActivityPub https://joinplu.me
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

251 lines
8.5 KiB

  1. #![allow(clippy::too_many_arguments)]
  2. #![feature(decl_macro, proc_macro_hygiene, try_trait)]
  3. #[macro_use]
  4. extern crate gettext_macros;
  5. #[macro_use]
  6. extern crate rocket;
  7. #[macro_use]
  8. extern crate serde_json;
  9. #[macro_use]
  10. extern crate validator_derive;
  11. use clap::App;
  12. use plume_models::{db_conn::init_pool, migrations::IMPORTED_MIGRATIONS, search::Searcher, CONFIG};
  13. use rocket_csrf::CsrfFairingBuilder;
  14. use scheduled_thread_pool::ScheduledThreadPool;
  15. use std::process::exit;
  16. use std::sync::{Arc, Mutex};
  17. use std::time::Duration;
  18. init_i18n!(
  19. "plume", ar, bg, ca, cs, de, en, eo, es, fa, fr, gl, hi, hr, it, ja, nb, pl, pt, ro, ru, sr,
  20. sk, sv
  21. );
  22. mod api;
  23. mod inbox;
  24. mod mail;
  25. #[macro_use]
  26. mod template_utils;
  27. mod routes;
  28. #[macro_use]
  29. extern crate shrinkwraprs;
  30. #[cfg(feature = "test")]
  31. mod test_routes;
  32. include!(concat!(env!("OUT_DIR"), "/templates.rs"));
  33. compile_i18n!();
  34. fn main() {
  35. App::new("Plume")
  36. .bin_name("plume")
  37. .version(env!("CARGO_PKG_VERSION"))
  38. .about("Plume backend server")
  39. .after_help(
  40. r#"
  41. The plume command should be run inside the directory
  42. containing the `.env` configuration file and `static` directory.
  43. See https://docs.joinplu.me/installation/config
  44. and https://docs.joinplu.me/installation/init for more info.
  45. "#,
  46. )
  47. .get_matches();
  48. match dotenv::dotenv() {
  49. Ok(path) => println!("Configuration read from {}", path.display()),
  50. Err(ref e) if e.not_found() => eprintln!("no .env was found"),
  51. e => e.map(|_| ()).unwrap(),
  52. }
  53. let dbpool = init_pool().expect("main: database pool initialization error");
  54. if IMPORTED_MIGRATIONS
  55. .is_pending(&dbpool.get().unwrap())
  56. .unwrap_or(true)
  57. {
  58. panic!(
  59. r#"
  60. It appear your database migration does not run the migration required
  61. by this version of Plume. To fix this, you can run migrations via
  62. this command:
  63. plm migration run
  64. Then try to restart Plume.
  65. "#
  66. )
  67. }
  68. let workpool = ScheduledThreadPool::with_name("worker {}", num_cpus::get());
  69. let searcher = Searcher::new(dbpool.clone());
  70. let commiter = searcher.clone();
  71. workpool.execute_with_fixed_delay(
  72. Duration::from_secs(5),
  73. Duration::from_secs(60 * 30),
  74. move || commiter.commit(),
  75. );
  76. let search_unlocker = searcher.clone();
  77. ctrlc::set_handler(move || {
  78. search_unlocker.commit();
  79. search_unlocker.drop_writer();
  80. exit(0);
  81. })
  82. .expect("Error setting Ctrl-c handler");
  83. let mail = mail::init();
  84. if mail.is_none() && CONFIG.rocket.as_ref().unwrap().environment.is_prod() {
  85. println!("Warning: the email server is not configured (or not completely).");
  86. println!("Please refer to the documentation to see how to configure it.");
  87. }
  88. let rocket = rocket::custom(CONFIG.rocket.clone().unwrap())
  89. .mount(
  90. "/",
  91. routes![
  92. routes::blogs::details,
  93. routes::blogs::activity_details,
  94. routes::blogs::outbox,
  95. routes::blogs::outbox_page,
  96. routes::blogs::new,
  97. routes::blogs::new_auth,
  98. routes::blogs::create,
  99. routes::blogs::delete,
  100. routes::blogs::edit,
  101. routes::blogs::update,
  102. routes::blogs::atom_feed,
  103. routes::comments::create,
  104. routes::comments::delete,
  105. routes::comments::activity_pub,
  106. routes::instance::index,
  107. routes::instance::admin,
  108. routes::instance::admin_mod,
  109. routes::instance::admin_instances,
  110. routes::instance::admin_users,
  111. routes::instance::admin_email_blocklist,
  112. routes::instance::add_email_blocklist,
  113. routes::instance::delete_email_blocklist,
  114. routes::instance::edit_users,
  115. routes::instance::toggle_block,
  116. routes::instance::update_settings,
  117. routes::instance::shared_inbox,
  118. routes::instance::interact,
  119. routes::instance::nodeinfo,
  120. routes::instance::about,
  121. routes::instance::privacy,
  122. routes::instance::web_manifest,
  123. routes::likes::create,
  124. routes::likes::create_auth,
  125. routes::medias::list,
  126. routes::medias::new,
  127. routes::medias::upload,
  128. routes::medias::details,
  129. routes::medias::delete,
  130. routes::medias::set_avatar,
  131. routes::notifications::notifications,
  132. routes::notifications::notifications_auth,
  133. routes::posts::details,
  134. routes::posts::activity_details,
  135. routes::posts::edit,
  136. routes::posts::update,
  137. routes::posts::new,
  138. routes::posts::new_auth,
  139. routes::posts::create,
  140. routes::posts::delete,
  141. routes::posts::remote_interact,
  142. routes::posts::remote_interact_post,
  143. routes::reshares::create,
  144. routes::reshares::create_auth,
  145. routes::search::search,
  146. routes::session::new,
  147. routes::session::create,
  148. routes::session::delete,
  149. routes::session::password_reset_request_form,
  150. routes::session::password_reset_request,
  151. routes::session::password_reset_form,
  152. routes::session::password_reset,
  153. routes::theme_files,
  154. routes::plume_static_files,
  155. routes::static_files,
  156. routes::plume_media_files,
  157. routes::tags::tag,
  158. routes::timelines::details,
  159. routes::timelines::new,
  160. routes::timelines::create,
  161. routes::timelines::edit,
  162. routes::timelines::update,
  163. routes::timelines::delete,
  164. routes::user::me,
  165. routes::user::details,
  166. routes::user::dashboard,
  167. routes::user::dashboard_auth,
  168. routes::user::followers,
  169. routes::user::followed,
  170. routes::user::edit,
  171. routes::user::edit_auth,
  172. routes::user::update,
  173. routes::user::delete,
  174. routes::user::follow,
  175. routes::user::follow_not_connected,
  176. routes::user::follow_auth,
  177. routes::user::activity_details,
  178. routes::user::outbox,
  179. routes::user::outbox_page,
  180. routes::user::inbox,
  181. routes::user::ap_followers,
  182. routes::user::new,
  183. routes::user::create,
  184. routes::user::atom_feed,
  185. routes::well_known::host_meta,
  186. routes::well_known::nodeinfo,
  187. routes::well_known::webfinger,
  188. routes::errors::csrf_violation
  189. ],
  190. )
  191. .mount(
  192. "/api/v1",
  193. routes![
  194. api::oauth,
  195. api::apps::create,
  196. api::posts::get,
  197. api::posts::list,
  198. api::posts::create,
  199. api::posts::delete,
  200. ],
  201. )
  202. .register(catchers![
  203. routes::errors::not_found,
  204. routes::errors::unprocessable_entity,
  205. routes::errors::server_error
  206. ])
  207. .manage(Arc::new(Mutex::new(mail)))
  208. .manage::<Arc<Mutex<Vec<routes::session::ResetRequest>>>>(Arc::new(Mutex::new(vec![])))
  209. .manage(dbpool)
  210. .manage(Arc::new(workpool))
  211. .manage(searcher)
  212. .manage(include_i18n!())
  213. .attach(
  214. CsrfFairingBuilder::new()
  215. .set_default_target(
  216. "/csrf-violation?target=<uri>".to_owned(),
  217. rocket::http::Method::Post,
  218. )
  219. .add_exceptions(vec![
  220. ("/inbox".to_owned(), "/inbox".to_owned(), None),
  221. (
  222. "/@/<name>/inbox".to_owned(),
  223. "/@/<name>/inbox".to_owned(),
  224. None,
  225. ),
  226. ("/api/<path..>".to_owned(), "/api/<path..>".to_owned(), None),
  227. ])
  228. .finalize()
  229. .expect("main: csrf fairing creation error"),
  230. );
  231. #[cfg(feature = "test")]
  232. let rocket = rocket.mount("/test", routes![test_routes::health,]);
  233. rocket.launch();
  234. }