2019-03-19 14:37:56 +01:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
2020-01-22 22:41:36 +01:00
|
|
|
#![feature(proc_macro_hygiene, try_trait)]
|
2018-09-02 22:55:42 +02:00
|
|
|
|
2019-02-02 15:23:50 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate gettext_macros;
|
2018-09-08 01:11:27 +02:00
|
|
|
#[macro_use]
|
2018-04-22 15:35:37 +02:00
|
|
|
extern crate rocket;
|
2019-04-17 22:09:07 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate runtime_fmt;
|
2018-07-06 19:29:36 +02:00
|
|
|
#[macro_use]
|
2018-04-23 17:09:05 +02:00
|
|
|
extern crate serde_json;
|
2018-06-29 14:22:43 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate validator_derive;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2019-07-03 20:36:42 +02:00
|
|
|
use clap::App;
|
2018-10-06 13:06:02 +02:00
|
|
|
use diesel::r2d2::ConnectionManager;
|
2018-12-29 09:36:07 +01:00
|
|
|
use plume_models::{
|
|
|
|
db_conn::{DbPool, PragmaForeignKey},
|
2019-05-10 22:59:34 +02:00
|
|
|
instance::Instance,
|
2019-04-29 16:30:20 +02:00
|
|
|
migrations::IMPORTED_MIGRATIONS,
|
2018-12-29 09:36:07 +01:00
|
|
|
search::{Searcher as UnmanagedSearcher, SearcherError},
|
2019-03-21 10:30:33 +01:00
|
|
|
Connection, Error, CONFIG,
|
2018-12-29 09:36:07 +01:00
|
|
|
};
|
2018-12-02 17:37:51 +01:00
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
use std::process::exit;
|
2019-02-27 13:29:26 +01:00
|
|
|
use std::sync::{Arc, Mutex};
|
2018-12-02 17:37:51 +01:00
|
|
|
use std::time::Duration;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2019-04-19 14:59:03 +02:00
|
|
|
init_i18n!(
|
|
|
|
"plume", ar, bg, ca, cs, de, en, eo, es, fr, gl, hi, hr, it, ja, nb, pl, pt, ro, ru, sr, sk, sv
|
|
|
|
);
|
2019-02-02 15:23:50 +01:00
|
|
|
|
2018-09-19 16:49:34 +02:00
|
|
|
mod api;
|
2018-06-23 18:36:11 +02:00
|
|
|
mod inbox;
|
2019-02-27 13:29:26 +01:00
|
|
|
mod mail;
|
2018-12-06 18:54:16 +01:00
|
|
|
#[macro_use]
|
|
|
|
mod template_utils;
|
2018-04-22 15:35:37 +02:00
|
|
|
mod routes;
|
2019-05-25 20:23:45 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate shrinkwraprs;
|
2019-04-06 17:41:57 +02:00
|
|
|
#[cfg(feature = "test")]
|
|
|
|
mod test_routes;
|
2018-05-02 13:53:42 +02:00
|
|
|
|
2019-02-02 15:23:50 +01:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
|
|
|
|
|
|
|
|
compile_i18n!();
|
|
|
|
|
2018-10-06 13:06:02 +02:00
|
|
|
/// Initializes a database pool.
|
2018-10-06 13:35:58 +02:00
|
|
|
fn init_pool() -> Option<DbPool> {
|
2019-07-03 09:30:44 +02:00
|
|
|
match dotenv::dotenv() {
|
|
|
|
Ok(path) => println!("Configuration read from {}", path.display()),
|
|
|
|
Err(ref e) if e.not_found() => eprintln!("no .env was found"),
|
|
|
|
e => e.map(|_| ()).unwrap(),
|
|
|
|
}
|
2018-10-06 13:06:02 +02:00
|
|
|
|
2019-03-21 10:30:33 +01:00
|
|
|
let manager = ConnectionManager::<Connection>::new(CONFIG.database_url.as_str());
|
2020-05-06 19:27:59 +02:00
|
|
|
let mut builder = DbPool::builder()
|
2018-12-14 23:16:18 +01:00
|
|
|
.connection_customizer(Box::new(PragmaForeignKey))
|
2020-05-06 19:27:59 +02:00
|
|
|
.min_idle(CONFIG.db_min_idle);
|
|
|
|
if let Some(max_size) = CONFIG.db_max_size {
|
|
|
|
builder = builder.max_size(max_size);
|
|
|
|
};
|
|
|
|
let pool = builder.build(manager).ok()?;
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::cache_local(&pool.get().unwrap());
|
|
|
|
Some(pool)
|
2018-10-06 13:06:02 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 15:35:37 +02:00
|
|
|
fn main() {
|
2019-07-03 20:36:42 +02:00
|
|
|
App::new("Plume")
|
|
|
|
.bin_name("plume")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.about("Plume backend server")
|
|
|
|
.after_help(
|
|
|
|
r#"
|
|
|
|
The plume command should be run inside the directory
|
|
|
|
containing the `.env` configuration file and `static` directory.
|
|
|
|
See https://docs.joinplu.me/installation/config
|
|
|
|
and https://docs.joinplu.me/installation/init for more info.
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.get_matches();
|
2018-12-02 17:37:51 +01:00
|
|
|
let dbpool = init_pool().expect("main: database pool initialization error");
|
2019-04-29 16:30:20 +02:00
|
|
|
if IMPORTED_MIGRATIONS
|
|
|
|
.is_pending(&dbpool.get().unwrap())
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
|
|
|
panic!(
|
|
|
|
r#"
|
|
|
|
It appear your database migration does not run the migration required
|
|
|
|
by this version of Plume. To fix this, you can run migrations via
|
|
|
|
this command:
|
|
|
|
|
|
|
|
plm migration run
|
|
|
|
|
|
|
|
Then try to restart Plume.
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
}
|
2018-12-02 17:37:51 +01:00
|
|
|
let workpool = ScheduledThreadPool::with_name("worker {}", num_cpus::get());
|
2019-03-19 14:37:56 +01:00
|
|
|
// we want a fast exit here, so
|
|
|
|
#[allow(clippy::match_wild_err_arm)]
|
2019-03-21 10:30:33 +01:00
|
|
|
let searcher = match UnmanagedSearcher::open(&CONFIG.search_index) {
|
2018-12-29 09:36:07 +01:00
|
|
|
Err(Error::Search(e)) => match e {
|
|
|
|
SearcherError::WriteLockAcquisitionError => panic!(
|
2019-03-21 10:30:33 +01:00
|
|
|
r#"
|
|
|
|
Your search index is locked. Plume can't start. To fix this issue
|
2018-12-29 09:36:07 +01:00
|
|
|
make sure no other Plume instance is started, and run:
|
|
|
|
|
|
|
|
plm search unlock
|
|
|
|
|
|
|
|
Then try to restart Plume.
|
2019-03-21 10:30:33 +01:00
|
|
|
"#
|
|
|
|
),
|
|
|
|
SearcherError::IndexOpeningError => panic!(
|
|
|
|
r#"
|
|
|
|
Plume was unable to open the search index. If you created the index
|
2019-04-28 23:17:21 +02:00
|
|
|
before, make sure to run Plume in the same directory it was created in, or
|
|
|
|
to set SEARCH_INDEX accordingly. If you did not yet create the search
|
2019-03-21 10:30:33 +01:00
|
|
|
index, run this command:
|
|
|
|
|
|
|
|
plm search init
|
|
|
|
|
|
|
|
Then try to restart Plume
|
2019-03-20 17:56:17 +01:00
|
|
|
"#
|
|
|
|
),
|
|
|
|
e => Err(e).unwrap(),
|
2018-12-29 09:36:07 +01:00
|
|
|
},
|
|
|
|
Err(_) => panic!("Unexpected error while opening search index"),
|
2019-03-20 17:56:17 +01:00
|
|
|
Ok(s) => Arc::new(s),
|
2018-12-29 09:36:07 +01:00
|
|
|
};
|
2018-12-02 17:37:51 +01:00
|
|
|
|
|
|
|
let commiter = searcher.clone();
|
2019-03-20 17:56:17 +01:00
|
|
|
workpool.execute_with_fixed_delay(
|
|
|
|
Duration::from_secs(5),
|
|
|
|
Duration::from_secs(60 * 30),
|
|
|
|
move || commiter.commit(),
|
|
|
|
);
|
2018-12-02 17:37:51 +01:00
|
|
|
|
|
|
|
let search_unlocker = searcher.clone();
|
|
|
|
ctrlc::set_handler(move || {
|
2019-04-06 17:41:57 +02:00
|
|
|
search_unlocker.commit();
|
2018-12-02 17:37:51 +01:00
|
|
|
search_unlocker.drop_writer();
|
|
|
|
exit(0);
|
2019-03-20 17:56:17 +01:00
|
|
|
})
|
|
|
|
.expect("Error setting Ctrl-c handler");
|
2018-12-02 17:37:51 +01:00
|
|
|
|
2019-02-27 13:29:26 +01:00
|
|
|
let mail = mail::init();
|
2019-03-21 10:30:33 +01:00
|
|
|
if mail.is_none() && CONFIG.rocket.as_ref().unwrap().environment.is_prod() {
|
2019-02-27 13:29:26 +01:00
|
|
|
println!("Warning: the email server is not configured (or not completely).");
|
|
|
|
println!("Please refer to the documentation to see how to configure it.");
|
|
|
|
}
|
|
|
|
|
2019-04-06 17:41:57 +02:00
|
|
|
let rocket = rocket::custom(CONFIG.rocket.clone().unwrap())
|
2019-03-20 17:56:17 +01:00
|
|
|
.mount(
|
|
|
|
"/",
|
|
|
|
routes![
|
|
|
|
routes::blogs::details,
|
|
|
|
routes::blogs::activity_details,
|
|
|
|
routes::blogs::outbox,
|
2019-10-30 11:22:28 +01:00
|
|
|
routes::blogs::outbox_page,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::blogs::new,
|
|
|
|
routes::blogs::new_auth,
|
|
|
|
routes::blogs::create,
|
|
|
|
routes::blogs::delete,
|
2019-03-22 19:51:36 +01:00
|
|
|
routes::blogs::edit,
|
|
|
|
routes::blogs::update,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::blogs::atom_feed,
|
|
|
|
routes::comments::create,
|
|
|
|
routes::comments::delete,
|
|
|
|
routes::comments::activity_pub,
|
|
|
|
routes::instance::index,
|
|
|
|
routes::instance::admin,
|
2019-09-13 12:28:36 +02:00
|
|
|
routes::instance::admin_mod,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::instance::admin_instances,
|
|
|
|
routes::instance::admin_users,
|
2020-01-12 19:41:35 +01:00
|
|
|
routes::instance::admin_email_blocklist,
|
|
|
|
routes::instance::add_email_blocklist,
|
|
|
|
routes::instance::delete_email_blocklist,
|
2019-09-13 12:28:36 +02:00
|
|
|
routes::instance::edit_users,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::instance::toggle_block,
|
|
|
|
routes::instance::update_settings,
|
|
|
|
routes::instance::shared_inbox,
|
2019-04-17 22:09:07 +02:00
|
|
|
routes::instance::interact,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::instance::nodeinfo,
|
|
|
|
routes::instance::about,
|
2019-05-18 14:09:51 +02:00
|
|
|
routes::instance::privacy,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::instance::web_manifest,
|
|
|
|
routes::likes::create,
|
|
|
|
routes::likes::create_auth,
|
|
|
|
routes::medias::list,
|
|
|
|
routes::medias::new,
|
|
|
|
routes::medias::upload,
|
|
|
|
routes::medias::details,
|
|
|
|
routes::medias::delete,
|
|
|
|
routes::medias::set_avatar,
|
|
|
|
routes::notifications::notifications,
|
|
|
|
routes::notifications::notifications_auth,
|
|
|
|
routes::posts::details,
|
|
|
|
routes::posts::activity_details,
|
|
|
|
routes::posts::edit,
|
|
|
|
routes::posts::update,
|
|
|
|
routes::posts::new,
|
|
|
|
routes::posts::new_auth,
|
|
|
|
routes::posts::create,
|
|
|
|
routes::posts::delete,
|
2019-04-17 22:09:07 +02:00
|
|
|
routes::posts::remote_interact,
|
|
|
|
routes::posts::remote_interact_post,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::reshares::create,
|
|
|
|
routes::reshares::create_auth,
|
|
|
|
routes::search::search,
|
|
|
|
routes::session::new,
|
|
|
|
routes::session::create,
|
|
|
|
routes::session::delete,
|
|
|
|
routes::session::password_reset_request_form,
|
|
|
|
routes::session::password_reset_request,
|
|
|
|
routes::session::password_reset_form,
|
|
|
|
routes::session::password_reset,
|
2019-08-21 00:42:04 +02:00
|
|
|
routes::theme_files,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::plume_static_files,
|
|
|
|
routes::static_files,
|
2019-10-28 22:28:28 +01:00
|
|
|
routes::plume_media_files,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::tags::tag,
|
2019-10-07 19:08:20 +02:00
|
|
|
routes::timelines::details,
|
|
|
|
routes::timelines::new,
|
|
|
|
routes::timelines::create,
|
|
|
|
routes::timelines::edit,
|
|
|
|
routes::timelines::update,
|
|
|
|
routes::timelines::delete,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::user::me,
|
|
|
|
routes::user::details,
|
|
|
|
routes::user::dashboard,
|
|
|
|
routes::user::dashboard_auth,
|
|
|
|
routes::user::followers,
|
|
|
|
routes::user::followed,
|
|
|
|
routes::user::edit,
|
|
|
|
routes::user::edit_auth,
|
|
|
|
routes::user::update,
|
|
|
|
routes::user::delete,
|
|
|
|
routes::user::follow,
|
2019-04-17 22:09:07 +02:00
|
|
|
routes::user::follow_not_connected,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::user::follow_auth,
|
|
|
|
routes::user::activity_details,
|
|
|
|
routes::user::outbox,
|
2019-10-30 11:22:28 +01:00
|
|
|
routes::user::outbox_page,
|
2019-03-20 17:56:17 +01:00
|
|
|
routes::user::inbox,
|
|
|
|
routes::user::ap_followers,
|
|
|
|
routes::user::new,
|
|
|
|
routes::user::create,
|
|
|
|
routes::user::atom_feed,
|
|
|
|
routes::well_known::host_meta,
|
|
|
|
routes::well_known::nodeinfo,
|
|
|
|
routes::well_known::webfinger,
|
|
|
|
routes::errors::csrf_violation
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.mount(
|
|
|
|
"/api/v1",
|
|
|
|
routes![
|
|
|
|
api::oauth,
|
|
|
|
api::apps::create,
|
|
|
|
api::posts::get,
|
|
|
|
api::posts::list,
|
|
|
|
api::posts::create,
|
2019-04-28 23:17:21 +02:00
|
|
|
api::posts::delete,
|
2019-03-20 17:56:17 +01:00
|
|
|
],
|
|
|
|
)
|
2018-12-06 18:54:16 +01:00
|
|
|
.register(catchers![
|
2018-06-18 17:59:49 +02:00
|
|
|
routes::errors::not_found,
|
2019-01-27 10:55:22 +01:00
|
|
|
routes::errors::unprocessable_entity,
|
2018-06-18 17:59:49 +02:00
|
|
|
routes::errors::server_error
|
|
|
|
])
|
2019-02-27 13:29:26 +01:00
|
|
|
.manage(Arc::new(Mutex::new(mail)))
|
|
|
|
.manage::<Arc<Mutex<Vec<routes::session::ResetRequest>>>>(Arc::new(Mutex::new(vec![])))
|
2018-12-02 17:37:51 +01:00
|
|
|
.manage(dbpool)
|
2019-04-17 19:31:47 +02:00
|
|
|
.manage(Arc::new(workpool))
|
2018-12-02 17:37:51 +01:00
|
|
|
.manage(searcher)
|
2020-01-21 17:36:47 +01:00
|
|
|
.manage(include_i18n!());
|
2019-04-06 17:41:57 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "test")]
|
|
|
|
let rocket = rocket.mount("/test", routes![test_routes::health,]);
|
|
|
|
rocket.launch();
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|