2019-03-19 14:37:56 +01:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
2019-02-27 13:29:26 +01:00
|
|
|
#![feature(decl_macro, proc_macro_hygiene)]
|
2018-09-02 22:55:42 +02:00
|
|
|
|
2018-06-10 13:13:07 +02:00
|
|
|
extern crate activitypub;
|
2018-12-06 18:54:16 +01:00
|
|
|
extern crate askama_escape;
|
2018-09-01 22:08:26 +02:00
|
|
|
extern crate atom_syndication;
|
2018-09-25 21:45:32 +02:00
|
|
|
extern crate canapi;
|
2018-09-10 20:38:19 +02:00
|
|
|
extern crate chrono;
|
2018-06-19 15:08:44 +02:00
|
|
|
extern crate colored;
|
2018-12-02 17:37:51 +01:00
|
|
|
extern crate ctrlc;
|
2018-06-23 18:36:11 +02:00
|
|
|
extern crate diesel;
|
|
|
|
extern crate dotenv;
|
2018-05-16 20:20:44 +02:00
|
|
|
extern crate failure;
|
2019-02-02 15:23:50 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate gettext_macros;
|
|
|
|
extern crate gettext_utils;
|
2018-09-02 22:55:42 +02:00
|
|
|
extern crate guid_create;
|
2018-04-24 11:21:39 +02:00
|
|
|
extern crate heck;
|
2019-02-27 13:29:26 +01:00
|
|
|
extern crate lettre;
|
|
|
|
extern crate lettre_email;
|
2018-09-02 22:55:42 +02:00
|
|
|
extern crate multipart;
|
2018-12-02 17:37:51 +01:00
|
|
|
extern crate num_cpus;
|
2018-09-25 21:45:32 +02:00
|
|
|
extern crate plume_api;
|
2018-06-23 18:36:11 +02:00
|
|
|
extern crate plume_common;
|
|
|
|
extern crate plume_models;
|
2018-09-08 01:11:27 +02:00
|
|
|
#[macro_use]
|
2018-04-22 15:35:37 +02:00
|
|
|
extern crate rocket;
|
|
|
|
extern crate rocket_contrib;
|
2018-06-24 18:58:57 +02:00
|
|
|
extern crate rocket_csrf;
|
2018-06-17 16:28:44 +02:00
|
|
|
extern crate rocket_i18n;
|
2018-12-02 17:37:51 +01:00
|
|
|
extern crate scheduled_thread_pool;
|
2018-07-06 19:29:36 +02:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
2018-04-23 17:09:05 +02:00
|
|
|
extern crate serde_json;
|
2018-09-29 16:45:27 +02:00
|
|
|
extern crate serde_qs;
|
2018-06-29 14:22:43 +02:00
|
|
|
extern crate validator;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate validator_derive;
|
2018-06-18 23:50:40 +02:00
|
|
|
extern crate webfinger;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-10-06 13:06:02 +02:00
|
|
|
use diesel::r2d2::ConnectionManager;
|
2019-01-27 10:55:22 +01:00
|
|
|
use rocket::{
|
|
|
|
Config, State,
|
|
|
|
config::Limits
|
|
|
|
};
|
2018-06-24 18:58:57 +02:00
|
|
|
use rocket_csrf::CsrfFairingBuilder;
|
2018-12-29 09:36:07 +01:00
|
|
|
use plume_models::{
|
|
|
|
DATABASE_URL, Connection, Error,
|
|
|
|
db_conn::{DbPool, PragmaForeignKey},
|
|
|
|
search::{Searcher as UnmanagedSearcher, SearcherError},
|
|
|
|
};
|
2018-12-02 17:37:51 +01:00
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
2019-01-27 10:55:22 +01:00
|
|
|
use std::env;
|
2018-12-02 17:37:51 +01:00
|
|
|
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-02-26 13:13:00 +01:00
|
|
|
init_i18n!("plume", ar, de, en, es, fr, gl, it, ja, nb, pl, pt, ru);
|
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;
|
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-12-02 17:37:51 +01:00
|
|
|
type Worker<'a> = State<'a, ScheduledThreadPool>;
|
|
|
|
type Searcher<'a> = State<'a, Arc<UnmanagedSearcher>>;
|
2018-09-03 20:53:20 +02:00
|
|
|
|
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> {
|
2018-10-06 13:06:02 +02:00
|
|
|
dotenv::dotenv().ok();
|
|
|
|
|
2018-10-06 20:17:36 +02:00
|
|
|
let manager = ConnectionManager::<Connection>::new(DATABASE_URL.as_str());
|
2018-12-14 23:16:18 +01:00
|
|
|
DbPool::builder()
|
|
|
|
.connection_customizer(Box::new(PragmaForeignKey))
|
|
|
|
.build(manager).ok()
|
2018-10-06 13:06:02 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 15:35:37 +02:00
|
|
|
fn main() {
|
2018-12-02 17:37:51 +01:00
|
|
|
let dbpool = init_pool().expect("main: database pool initialization error");
|
|
|
|
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)]
|
2018-12-29 09:36:07 +01:00
|
|
|
let searcher = match UnmanagedSearcher::open(&"search_index") {
|
|
|
|
Err(Error::Search(e)) => match e {
|
|
|
|
SearcherError::WriteLockAcquisitionError => panic!(
|
|
|
|
r#"Your search index is locked. Plume can't start. To fix this issue
|
|
|
|
make sure no other Plume instance is started, and run:
|
|
|
|
|
|
|
|
plm search unlock
|
|
|
|
|
|
|
|
Then try to restart Plume.
|
|
|
|
"#),
|
|
|
|
e => Err(e).unwrap()
|
|
|
|
},
|
|
|
|
Err(_) => panic!("Unexpected error while opening search index"),
|
|
|
|
Ok(s) => Arc::new(s)
|
|
|
|
};
|
2018-12-02 17:37:51 +01:00
|
|
|
|
|
|
|
let commiter = searcher.clone();
|
|
|
|
workpool.execute_with_fixed_delay(Duration::from_secs(5), Duration::from_secs(60*30), move || commiter.commit());
|
|
|
|
|
|
|
|
let search_unlocker = searcher.clone();
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
search_unlocker.drop_writer();
|
|
|
|
exit(0);
|
|
|
|
}).expect("Error setting Ctrl-c handler");
|
|
|
|
|
2019-01-27 10:55:22 +01:00
|
|
|
let mut config = Config::active().unwrap();
|
|
|
|
config.set_address(env::var("ROCKET_ADDRESS").unwrap_or_else(|_| "localhost".to_owned())).unwrap();
|
|
|
|
config.set_port(env::var("ROCKET_PORT").ok().map(|s| s.parse::<u16>().unwrap()).unwrap_or(7878));
|
|
|
|
let _ = env::var("ROCKET_SECRET_KEY").map(|k| config.set_secret_key(k).unwrap());
|
|
|
|
let form_size = &env::var("FORM_SIZE").unwrap_or_else(|_| "32".to_owned()).parse::<u64>().unwrap();
|
|
|
|
let activity_size = &env::var("ACTIVITY_SIZE").unwrap_or_else(|_| "1024".to_owned()).parse::<u64>().unwrap();
|
|
|
|
config.set_limits(Limits::new()
|
|
|
|
.limit("forms", form_size * 1024)
|
|
|
|
.limit("json", activity_size * 1024));
|
|
|
|
|
2019-02-27 13:29:26 +01:00
|
|
|
let mail = mail::init();
|
|
|
|
if mail.is_none() && config.environment.is_prod() {
|
|
|
|
println!("Warning: the email server is not configured (or not completely).");
|
|
|
|
println!("Please refer to the documentation to see how to configure it.");
|
|
|
|
}
|
|
|
|
|
2019-01-27 10:55:22 +01:00
|
|
|
rocket::custom(config)
|
2018-04-22 15:35:37 +02:00
|
|
|
.mount("/", routes![
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::blogs::details,
|
2018-06-17 17:26:15 +02:00
|
|
|
routes::blogs::activity_details,
|
|
|
|
routes::blogs::outbox,
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::blogs::new,
|
2018-06-04 21:57:03 +02:00
|
|
|
routes::blogs::new_auth,
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::blogs::create,
|
2018-10-20 15:03:59 +02:00
|
|
|
routes::blogs::delete,
|
2018-09-01 22:08:26 +02:00
|
|
|
routes::blogs::atom_feed,
|
2018-05-10 20:01:16 +02:00
|
|
|
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::comments::create,
|
2018-12-23 11:13:36 +01:00
|
|
|
routes::comments::delete,
|
2018-09-09 17:08:53 +02:00
|
|
|
routes::comments::activity_pub,
|
2018-04-24 10:35:45 +02:00
|
|
|
|
2018-04-29 19:50:46 +02:00
|
|
|
routes::instance::index,
|
2018-09-04 21:56:27 +02:00
|
|
|
routes::instance::local,
|
2018-09-05 16:21:50 +02:00
|
|
|
routes::instance::feed,
|
2018-09-05 16:37:49 +02:00
|
|
|
routes::instance::federated,
|
2018-07-27 19:05:36 +02:00
|
|
|
routes::instance::admin,
|
2018-09-08 20:54:09 +02:00
|
|
|
routes::instance::admin_instances,
|
2018-09-09 12:25:55 +02:00
|
|
|
routes::instance::admin_users,
|
|
|
|
routes::instance::ban,
|
2018-09-08 21:07:55 +02:00
|
|
|
routes::instance::toggle_block,
|
2018-07-27 19:05:36 +02:00
|
|
|
routes::instance::update_settings,
|
2018-05-13 19:39:18 +02:00
|
|
|
routes::instance::shared_inbox,
|
2018-06-10 21:33:42 +02:00
|
|
|
routes::instance::nodeinfo,
|
2018-09-01 18:39:40 +02:00
|
|
|
routes::instance::about,
|
2018-09-10 16:08:22 +02:00
|
|
|
routes::instance::web_manifest,
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::likes::create,
|
2018-06-04 21:57:03 +02:00
|
|
|
routes::likes::create_auth,
|
2018-05-13 19:41:49 +02:00
|
|
|
|
2018-09-02 22:55:42 +02:00
|
|
|
routes::medias::list,
|
|
|
|
routes::medias::new,
|
|
|
|
routes::medias::upload,
|
|
|
|
routes::medias::details,
|
2018-09-02 23:10:15 +02:00
|
|
|
routes::medias::delete,
|
2018-09-03 14:04:17 +02:00
|
|
|
routes::medias::set_avatar,
|
2018-09-02 22:55:42 +02:00
|
|
|
|
2018-05-13 15:35:55 +02:00
|
|
|
routes::notifications::notifications,
|
2018-06-04 21:57:03 +02:00
|
|
|
routes::notifications::notifications_auth,
|
2018-05-13 15:35:55 +02:00
|
|
|
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::posts::details,
|
|
|
|
routes::posts::activity_details,
|
2018-09-07 19:51:53 +02:00
|
|
|
routes::posts::edit,
|
|
|
|
routes::posts::update,
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::posts::new,
|
|
|
|
routes::posts::new_auth,
|
|
|
|
routes::posts::create,
|
2018-09-01 17:28:47 +02:00
|
|
|
routes::posts::delete,
|
2018-05-13 19:41:49 +02:00
|
|
|
|
2018-05-19 11:51:10 +02:00
|
|
|
routes::reshares::create,
|
2018-06-04 21:57:03 +02:00
|
|
|
routes::reshares::create_auth,
|
2018-05-19 11:51:10 +02:00
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
routes::search::search,
|
2018-12-02 17:37:51 +01:00
|
|
|
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::session::new,
|
|
|
|
routes::session::create,
|
|
|
|
routes::session::delete,
|
2019-02-27 13:29:26 +01:00
|
|
|
routes::session::password_reset_request_form,
|
|
|
|
routes::session::password_reset_request,
|
|
|
|
routes::session::password_reset_form,
|
|
|
|
routes::session::password_reset,
|
2018-05-13 19:41:49 +02:00
|
|
|
|
2019-03-16 15:33:28 +01:00
|
|
|
routes::plume_static_files,
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::static_files,
|
|
|
|
|
2018-09-06 14:06:04 +02:00
|
|
|
routes::tags::tag,
|
|
|
|
|
2018-04-23 11:52:44 +02:00
|
|
|
routes::user::me,
|
2018-04-22 20:13:12 +02:00
|
|
|
routes::user::details,
|
2018-06-10 19:55:08 +02:00
|
|
|
routes::user::dashboard,
|
|
|
|
routes::user::dashboard_auth,
|
2018-06-17 17:26:15 +02:00
|
|
|
routes::user::followers,
|
2019-02-26 13:13:00 +01:00
|
|
|
routes::user::followed,
|
2018-05-12 17:30:14 +02:00
|
|
|
routes::user::edit,
|
2018-06-07 10:39:22 +02:00
|
|
|
routes::user::edit_auth,
|
2018-05-12 17:30:14 +02:00
|
|
|
routes::user::update,
|
2018-09-09 21:49:24 +02:00
|
|
|
routes::user::delete,
|
2018-05-01 21:57:30 +02:00
|
|
|
routes::user::follow,
|
2018-06-07 10:39:22 +02:00
|
|
|
routes::user::follow_auth,
|
2018-04-24 14:31:02 +02:00
|
|
|
routes::user::activity_details,
|
2018-04-29 20:01:42 +02:00
|
|
|
routes::user::outbox,
|
2018-05-01 16:00:29 +02:00
|
|
|
routes::user::inbox,
|
2018-05-13 13:53:58 +02:00
|
|
|
routes::user::ap_followers,
|
2018-04-22 20:13:12 +02:00
|
|
|
routes::user::new,
|
|
|
|
routes::user::create,
|
2018-09-01 22:08:26 +02:00
|
|
|
routes::user::atom_feed,
|
2018-04-23 11:52:44 +02:00
|
|
|
|
2018-05-13 19:41:49 +02:00
|
|
|
routes::well_known::host_meta,
|
2018-06-10 21:33:42 +02:00
|
|
|
routes::well_known::nodeinfo,
|
2018-06-24 18:58:57 +02:00
|
|
|
routes::well_known::webfinger,
|
|
|
|
|
|
|
|
routes::errors::csrf_violation
|
2018-04-22 15:35:37 +02:00
|
|
|
])
|
2018-09-19 16:49:34 +02:00
|
|
|
.mount("/api/v1", routes![
|
2018-10-22 15:30:04 +02:00
|
|
|
api::oauth,
|
|
|
|
|
2018-10-21 18:22:27 +02:00
|
|
|
api::apps::create,
|
|
|
|
|
2018-09-25 21:45:32 +02:00
|
|
|
api::posts::get,
|
2018-09-29 16:45:27 +02:00
|
|
|
api::posts::list,
|
2018-12-24 16:42:40 +01:00
|
|
|
api::posts::create,
|
2018-09-19 16:49:34 +02: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)
|
|
|
|
.manage(workpool)
|
|
|
|
.manage(searcher)
|
2019-02-02 15:23:50 +01:00
|
|
|
.manage(include_i18n!())
|
2018-09-09 11:27:03 +02:00
|
|
|
.attach(CsrfFairingBuilder::new()
|
|
|
|
.set_default_target("/csrf-violation?target=<uri>".to_owned(), rocket::http::Method::Post)
|
|
|
|
.add_exceptions(vec![
|
|
|
|
("/inbox".to_owned(), "/inbox".to_owned(), rocket::http::Method::Post),
|
|
|
|
("/@/<name>/inbox".to_owned(), "/@/<name>/inbox".to_owned(), rocket::http::Method::Post),
|
2018-09-30 11:56:12 +02:00
|
|
|
("/login".to_owned(), "/login".to_owned(), rocket::http::Method::Post),
|
2018-10-02 11:49:11 +02:00
|
|
|
("/users/new".to_owned(), "/users/new".to_owned(), rocket::http::Method::Post),
|
2018-10-22 15:36:44 +02:00
|
|
|
("/api/<path..>".to_owned(), "/api/<path..>".to_owned(), rocket::http::Method::Post)
|
2018-09-09 11:27:03 +02:00
|
|
|
])
|
2018-10-20 11:04:20 +02:00
|
|
|
.finalize().expect("main: csrf fairing creation error"))
|
2018-04-22 15:35:37 +02:00
|
|
|
.launch();
|
|
|
|
}
|