Plume/src/main.rs

258 lines
8.4 KiB
Rust
Raw Normal View History

#![allow(clippy::too_many_arguments)]
#![feature(decl_macro, proc_macro_hygiene)]
2018-09-02 20:55:42 +00:00
2018-06-10 11:13:07 +00:00
extern crate activitypub;
extern crate askama_escape;
2018-09-01 20:08:26 +00:00
extern crate atom_syndication;
2018-09-25 19:45:32 +00:00
extern crate canapi;
extern crate chrono;
extern crate colored;
extern crate ctrlc;
extern crate diesel;
extern crate dotenv;
2018-05-16 18:20:44 +00:00
extern crate failure;
#[macro_use]
extern crate gettext_macros;
extern crate gettext_utils;
2018-09-02 20:55:42 +00:00
extern crate guid_create;
2018-04-24 09:21:39 +00:00
extern crate heck;
extern crate lettre;
extern crate lettre_email;
2018-09-02 20:55:42 +00:00
extern crate multipart;
extern crate num_cpus;
2018-09-25 19:45:32 +00:00
extern crate plume_api;
extern crate plume_common;
extern crate plume_models;
#[macro_use]
extern crate rocket;
extern crate rocket_contrib;
2018-06-24 16:58:57 +00:00
extern crate rocket_csrf;
2018-06-17 14:28:44 +00:00
extern crate rocket_i18n;
extern crate scheduled_thread_pool;
extern crate serde;
#[macro_use]
2018-04-23 15:09:05 +00:00
extern crate serde_json;
2018-09-29 14:45:27 +00:00
extern crate serde_qs;
2018-06-29 12:22:43 +00:00
extern crate validator;
#[macro_use]
extern crate validator_derive;
2018-06-18 21:50:40 +00:00
extern crate webfinger;
2018-10-06 11:06:02 +00:00
use diesel::r2d2::ConnectionManager;
use rocket::{
Config, State,
config::Limits
};
2018-06-24 16:58:57 +00:00
use rocket_csrf::CsrfFairingBuilder;
use plume_models::{
DATABASE_URL, Connection, Error,
db_conn::{DbPool, PragmaForeignKey},
search::{Searcher as UnmanagedSearcher, SearcherError},
};
use scheduled_thread_pool::ScheduledThreadPool;
use std::env;
use std::process::exit;
use std::sync::{Arc, Mutex};
use std::time::Duration;
init_i18n!("plume", ar, de, en, es, fr, gl, it, ja, nb, pl, pt, ru);
2018-09-19 14:49:34 +00:00
mod api;
mod inbox;
mod mail;
#[macro_use]
mod template_utils;
mod routes;
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
compile_i18n!();
type Worker<'a> = State<'a, ScheduledThreadPool>;
type Searcher<'a> = State<'a, Arc<UnmanagedSearcher>>;
2018-10-06 11:06:02 +00:00
/// Initializes a database pool.
fn init_pool() -> Option<DbPool> {
2018-10-06 11:06:02 +00:00
dotenv::dotenv().ok();
let manager = ConnectionManager::<Connection>::new(DATABASE_URL.as_str());
DbPool::builder()
.connection_customizer(Box::new(PragmaForeignKey))
.build(manager).ok()
2018-10-06 11:06:02 +00:00
}
fn main() {
let dbpool = init_pool().expect("main: database pool initialization error");
let workpool = ScheduledThreadPool::with_name("worker {}", num_cpus::get());
// we want a fast exit here, so
#[allow(clippy::match_wild_err_arm)]
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)
};
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");
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));
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.");
}
rocket::custom(config)
.mount("/", routes![
routes::blogs::details,
2018-06-17 15:26:15 +00:00
routes::blogs::activity_details,
routes::blogs::outbox,
routes::blogs::new,
2018-06-04 19:57:03 +00:00
routes::blogs::new_auth,
routes::blogs::create,
2018-10-20 13:03:59 +00:00
routes::blogs::delete,
2018-09-01 20:08:26 +00:00
routes::blogs::atom_feed,
2018-05-10 18:01:16 +00:00
routes::comments::create,
routes::comments::delete,
routes::comments::activity_pub,
2018-04-24 08:35:45 +00:00
2018-04-29 17:50:46 +00:00
routes::instance::index,
2018-09-04 19:56:27 +00:00
routes::instance::local,
2018-09-05 14:21:50 +00:00
routes::instance::feed,
routes::instance::federated,
routes::instance::admin,
routes::instance::admin_instances,
2018-09-09 10:25:55 +00:00
routes::instance::admin_users,
routes::instance::ban,
routes::instance::toggle_block,
routes::instance::update_settings,
2018-05-13 17:39:18 +00:00
routes::instance::shared_inbox,
2018-06-10 19:33:42 +00:00
routes::instance::nodeinfo,
2018-09-01 16:39:40 +00:00
routes::instance::about,
2018-09-10 14:08:22 +00:00
routes::instance::web_manifest,
2018-04-22 18:13:12 +00:00
routes::likes::create,
2018-06-04 19:57:03 +00:00
routes::likes::create_auth,
2018-09-02 20:55:42 +00:00
routes::medias::list,
routes::medias::new,
routes::medias::upload,
routes::medias::details,
2018-09-02 21:10:15 +00:00
routes::medias::delete,
2018-09-03 12:04:17 +00:00
routes::medias::set_avatar,
2018-09-02 20:55:42 +00:00
2018-05-13 13:35:55 +00:00
routes::notifications::notifications,
2018-06-04 19:57:03 +00:00
routes::notifications::notifications_auth,
2018-05-13 13:35:55 +00:00
routes::posts::details,
routes::posts::activity_details,
2018-09-07 17:51:53 +00:00
routes::posts::edit,
routes::posts::update,
routes::posts::new,
routes::posts::new_auth,
routes::posts::create,
routes::posts::delete,
2018-05-19 09:51:10 +00:00
routes::reshares::create,
2018-06-04 19:57:03 +00:00
routes::reshares::create_auth,
2018-05-19 09:51:10 +00:00
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,
routes::plume_static_files,
routes::static_files,
2018-09-06 12:06:04 +00:00
routes::tags::tag,
2018-04-23 09:52:44 +00:00
routes::user::me,
2018-04-22 18:13:12 +00:00
routes::user::details,
2018-06-10 17:55:08 +00:00
routes::user::dashboard,
routes::user::dashboard_auth,
2018-06-17 15:26:15 +00:00
routes::user::followers,
routes::user::followed,
2018-05-12 15:30:14 +00:00
routes::user::edit,
routes::user::edit_auth,
2018-05-12 15:30:14 +00:00
routes::user::update,
2018-09-09 19:49:24 +00:00
routes::user::delete,
2018-05-01 19:57:30 +00:00
routes::user::follow,
routes::user::follow_auth,
2018-04-24 12:31:02 +00:00
routes::user::activity_details,
2018-04-29 18:01:42 +00:00
routes::user::outbox,
2018-05-01 14:00:29 +00:00
routes::user::inbox,
routes::user::ap_followers,
2018-04-22 18:13:12 +00:00
routes::user::new,
routes::user::create,
2018-09-01 20:08:26 +00:00
routes::user::atom_feed,
2018-04-23 09:52:44 +00:00
routes::well_known::host_meta,
2018-06-10 19:33:42 +00:00
routes::well_known::nodeinfo,
2018-06-24 16:58:57 +00:00
routes::well_known::webfinger,
routes::errors::csrf_violation
])
2018-09-19 14:49:34 +00:00
.mount("/api/v1", routes![
api::oauth,
2018-10-21 16:22:27 +00:00
api::apps::create,
2018-09-25 19:45:32 +00:00
api::posts::get,
2018-09-29 14:45:27 +00:00
api::posts::list,
2018-12-24 15:42:40 +00:00
api::posts::create,
2018-09-19 14:49:34 +00:00
])
.register(catchers![
2018-06-18 15:59:49 +00:00
routes::errors::not_found,
routes::errors::unprocessable_entity,
2018-06-18 15:59:49 +00:00
routes::errors::server_error
])
.manage(Arc::new(Mutex::new(mail)))
.manage::<Arc<Mutex<Vec<routes::session::ResetRequest>>>>(Arc::new(Mutex::new(vec![])))
.manage(dbpool)
.manage(workpool)
.manage(searcher)
.manage(include_i18n!())
.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),
("/login".to_owned(), "/login".to_owned(), rocket::http::Method::Post),
("/users/new".to_owned(), "/users/new".to_owned(), rocket::http::Method::Post),
2018-10-22 13:36:44 +00:00
("/api/<path..>".to_owned(), "/api/<path..>".to_owned(), rocket::http::Method::Post)
])
.finalize().expect("main: csrf fairing creation error"))
.launch();
}