conduit/src/main.rs

237 lines
9.2 KiB
Rust
Raw Normal View History

2021-02-09 22:58:40 +00:00
// #![warn(rust_2018_idioms)]
pub mod appservice_server;
pub mod client_server;
2020-08-14 09:34:15 +00:00
pub mod server_server;
2020-06-12 11:18:25 +00:00
mod database;
mod error;
2020-04-04 09:53:37 +00:00
mod pdu;
2020-08-14 09:34:15 +00:00
mod push_rules;
2020-02-15 21:42:21 +00:00
mod ruma_wrapper;
2020-03-29 11:48:44 +00:00
mod utils;
2020-02-15 21:42:21 +00:00
2021-02-28 11:41:03 +00:00
use database::Config;
pub use database::Database;
2021-02-28 11:41:03 +00:00
pub use error::{Error, Result};
2020-04-04 09:53:37 +00:00
pub use pdu::PduEvent;
pub use rocket::State;
use ruma::api::client::error::ErrorKind;
2020-06-09 13:13:17 +00:00
pub use ruma_wrapper::{ConduitResult, Ruma, RumaResponse};
use rocket::figment::{
providers::{Env, Format, Toml},
Figment,
};
2020-12-19 15:00:11 +00:00
use rocket::{catch, catchers, fairing::AdHoc, routes, Request};
2021-02-28 11:41:03 +00:00
use tracing::span;
use tracing_subscriber::{prelude::*, Registry};
2021-02-28 11:41:03 +00:00
fn setup_rocket() -> (rocket::Rocket, Config) {
// Force log level off, so we can use our own logger
std::env::set_var("CONDUIT_LOG_LEVEL", "off");
let config =
Figment::from(rocket::Config::release_default())
.merge(
Toml::file(Env::var("CONDUIT_CONFIG").expect(
"The CONDUIT_CONFIG env var needs to be set. Example: /etc/conduit.toml",
))
.nested(),
)
.merge(Env::prefixed("CONDUIT_").global());
2021-02-28 11:41:03 +00:00
let parsed_config = config
.extract::<Config>()
.expect("It looks like your config is invalid. Please take a look at the error");
let parsed_config2 = parsed_config.clone();
let rocket = rocket::custom(config)
2020-02-18 21:07:57 +00:00
.mount(
"/",
routes![
2020-04-11 07:30:11 +00:00
client_server::get_supported_versions_route,
client_server::get_register_available_route,
2020-04-11 07:30:11 +00:00
client_server::register_route,
2020-07-31 12:40:28 +00:00
client_server::get_login_types_route,
2020-04-11 07:30:11 +00:00
client_server::login_route,
2020-07-27 15:38:00 +00:00
client_server::whoami_route,
client_server::logout_route,
2020-07-05 06:35:19 +00:00
client_server::logout_all_route,
client_server::change_password_route,
client_server::deactivate_route,
2020-04-19 12:14:47 +00:00
client_server::get_capabilities_route,
2020-04-11 07:30:11 +00:00
client_server::get_pushrules_all_route,
client_server::set_pushrule_route,
2021-01-24 15:05:52 +00:00
client_server::get_pushrule_route,
client_server::set_pushrule_enabled_route,
2021-01-24 15:05:52 +00:00
client_server::get_pushrule_enabled_route,
client_server::get_pushrule_actions_route,
client_server::set_pushrule_actions_route,
client_server::delete_pushrule_route,
client_server::get_room_event_route,
2020-04-11 07:30:11 +00:00
client_server::get_filter_route,
client_server::create_filter_route,
client_server::set_global_account_data_route,
client_server::get_global_account_data_route,
client_server::set_displayname_route,
client_server::get_displayname_route,
client_server::set_avatar_url_route,
client_server::get_avatar_url_route,
client_server::get_profile_route,
client_server::set_presence_route,
client_server::upload_keys_route,
2020-05-17 17:56:40 +00:00
client_server::get_keys_route,
client_server::claim_keys_route,
2020-06-16 10:11:38 +00:00
client_server::create_backup_route,
client_server::update_backup_route,
client_server::delete_backup_route,
2020-06-16 10:11:38 +00:00
client_server::get_latest_backup_route,
client_server::get_backup_route,
client_server::add_backup_key_sessions_route,
2020-06-16 10:11:38 +00:00
client_server::add_backup_keys_route,
client_server::delete_backup_key_session_route,
client_server::delete_backup_key_sessions_route,
client_server::delete_backup_keys_route,
client_server::get_backup_key_session_route,
client_server::get_backup_key_sessions_route,
2020-06-16 10:11:38 +00:00
client_server::get_backup_keys_route,
2020-04-11 07:30:11 +00:00
client_server::set_read_marker_route,
2021-03-02 13:32:30 +00:00
client_server::create_receipt_route,
2020-04-12 19:12:50 +00:00
client_server::create_typing_event_route,
2020-04-11 07:30:11 +00:00
client_server::create_room_route,
2020-05-25 21:24:13 +00:00
client_server::redact_event_route,
client_server::create_alias_route,
client_server::delete_alias_route,
2020-04-11 07:30:11 +00:00
client_server::get_alias_route,
client_server::join_room_by_id_route,
client_server::join_room_by_id_or_alias_route,
client_server::joined_members_route,
2020-04-19 12:14:47 +00:00
client_server::leave_room_route,
2020-04-28 17:56:34 +00:00
client_server::forget_room_route,
client_server::joined_rooms_route,
2020-06-04 13:02:27 +00:00
client_server::kick_user_route,
client_server::ban_user_route,
client_server::unban_user_route,
client_server::invite_user_route,
2020-05-25 21:24:13 +00:00
client_server::set_room_visibility_route,
client_server::get_room_visibility_route,
2020-05-19 14:28:03 +00:00
client_server::get_public_rooms_route,
2020-04-11 07:30:11 +00:00
client_server::get_public_rooms_filtered_route,
client_server::search_users_route,
2020-04-19 12:14:47 +00:00
client_server::get_member_events_route,
2020-04-11 07:30:11 +00:00
client_server::get_protocols_route,
2020-08-12 21:32:39 +00:00
client_server::send_message_event_route,
client_server::send_state_event_for_key_route,
client_server::send_state_event_for_empty_key_route,
2020-05-18 07:22:07 +00:00
client_server::get_state_events_route,
client_server::get_state_events_for_key_route,
client_server::get_state_events_for_empty_key_route,
client_server::sync_events_route,
2020-06-04 11:58:55 +00:00
client_server::get_context_route,
2020-04-28 17:56:34 +00:00
client_server::get_message_events_route,
2020-08-18 10:15:27 +00:00
client_server::search_events_route,
client_server::turn_server_route,
client_server::send_event_to_device_route,
client_server::get_media_config_route,
2020-05-18 15:53:34 +00:00
client_server::create_content_route,
client_server::get_content_route,
client_server::get_content_thumbnail_route,
client_server::get_devices_route,
client_server::get_device_route,
client_server::update_device_route,
client_server::delete_device_route,
client_server::delete_devices_route,
client_server::get_tags_route,
client_server::update_tag_route,
client_server::delete_tag_route,
2020-04-11 07:30:11 +00:00
client_server::options_route,
2020-06-16 10:11:38 +00:00
client_server::upload_signing_keys_route,
client_server::upload_signatures_route,
2020-07-29 15:37:26 +00:00
client_server::get_key_changes_route,
2020-07-30 16:14:47 +00:00
client_server::get_pushers_route,
2020-06-16 10:11:38 +00:00
client_server::set_pushers_route,
2020-08-06 11:21:53 +00:00
client_server::upgrade_room_route,
server_server::get_server_version_route,
server_server::get_server_keys_route,
server_server::get_server_keys_deprecated_route,
server_server::get_public_rooms_route,
server_server::get_public_rooms_filtered_route,
server_server::send_transaction_message_route,
server_server::get_missing_events_route,
2020-10-05 20:19:22 +00:00
server_server::get_profile_information_route,
2020-02-18 21:07:57 +00:00
],
)
.register(catchers![
not_found_catcher,
forbidden_catcher,
unknown_token_catcher,
missing_token_catcher,
bad_json_catcher
])
.attach(AdHoc::on_attach("Config", |rocket| async {
2021-02-28 11:41:03 +00:00
let data = Database::load_or_create(parsed_config2)
.await
.expect("config is valid");
2020-04-29 06:48:56 +00:00
data.sending
.start_handler(&data.globals, &data.rooms, &data.appservice);
2020-09-15 14:13:54 +00:00
2020-04-29 06:48:56 +00:00
Ok(rocket.manage(data))
2021-02-28 11:41:03 +00:00
}));
(rocket, parsed_config)
}
2020-06-16 11:53:15 +00:00
#[rocket::main]
async fn main() {
2021-02-28 11:41:03 +00:00
let (rocket, config) = setup_rocket();
if config.allow_jaeger {
let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline()
.with_service_name("conduit")
.install()
.unwrap();
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
Registry::default().with(telemetry).try_init().unwrap();
let root = span!(tracing::Level::INFO, "app_start", work_units = 2);
let _enter = root.enter();
rocket.launch().await.unwrap();
} else {
let root = span!(tracing::Level::INFO, "app_start", work_units = 2);
let _enter = root.enter();
rocket.launch().await.unwrap();
}
2020-02-15 21:42:21 +00:00
}
2020-12-19 15:00:11 +00:00
#[catch(404)]
2020-12-22 17:45:35 +00:00
fn not_found_catcher(_: &Request<'_>) -> String {
2020-12-19 15:00:11 +00:00
"404 Not Found".to_owned()
}
#[catch(580)]
fn forbidden_catcher() -> Result<()> {
Err(Error::BadRequest(ErrorKind::Forbidden, "Forbidden."))
}
#[catch(581)]
fn unknown_token_catcher() -> Result<()> {
Err(Error::BadRequest(
ErrorKind::UnknownToken { soft_logout: false },
"Unknown token.",
))
}
#[catch(582)]
fn missing_token_catcher() -> Result<()> {
Err(Error::BadRequest(ErrorKind::MissingToken, "Missing token."))
}
#[catch(583)]
fn bad_json_catcher() -> Result<()> {
Err(Error::BadRequest(ErrorKind::BadJson, "Bad json."))
}