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.
 
 
 
 
 
 

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