conduit "library" delete, resolve some warnings from that

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-04-22 22:36:11 -04:00 committed by June
parent 5e8ae971f1
commit 472c32f453
10 changed files with 344 additions and 96 deletions

View file

@ -214,7 +214,7 @@ pub fn parse_incoming_pdu(pdu: &RawJsonValue) -> Result<(OwnedEventId, Canonical
.ok_or(Error::BadRequest(ErrorKind::InvalidParam, "Invalid room id in pdu"))?;
let Ok(room_version_id) = services().rooms.state.get_room_version(&room_id) else {
return Err(Error::Error(format!("Server is not in room {room_id}")));
return Err(Error::Err(format!("Server is not in room {room_id}")));
};
let Ok((event_id, value)) = gen_event_id_canonical_json(pdu, &room_version_id) else {

View file

@ -544,6 +544,7 @@ impl KeyValueDatabase {
}
}
#[allow(dead_code)]
pub fn flush(&self) -> Result<()> {
let start = std::time::Instant::now();

View file

@ -1,27 +0,0 @@
pub mod api;
pub mod clap;
mod config;
mod database;
mod service;
mod utils;
// Not async due to services() being used in many closures, and async closures
// are not stable as of writing This is the case for every other occurence of
// sync Mutex/RwLock, except for database related ones, where the current
// maintainer (Timo) has asked to not modify those
use std::sync::RwLock;
pub use api::ruma_wrapper::{Ruma, RumaResponse};
pub use config::Config;
pub use database::KeyValueDatabase;
pub use service::{pdu::PduEvent, Services};
pub use utils::error::{Error, Result};
pub static SERVICES: RwLock<Option<&'static Services<'static>>> = RwLock::new(None);
pub fn services() -> &'static Services<'static> {
SERVICES
.read()
.unwrap()
.expect("SERVICES should be initialized when this is called")
}

View file

@ -3,8 +3,13 @@ use std::fs::Permissions; // not unix specific, just only for UNIX sockets stuff
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt as _; /* not unix specific, just only for UNIX sockets stuff and *nix
* container checks */
// Not async due to services() being used in many closures, and async closures
// are not stable as of writing This is the case for every other occurence of
// sync Mutex/RwLock, except for database related ones
use std::sync::RwLock;
use std::{any::Any, io, net::SocketAddr, sync::atomic, time::Duration};
use api::ruma_wrapper::{Ruma, RumaResponse};
use axum::{
extract::{DefaultBodyLimit, MatchedPath},
response::IntoResponse,
@ -13,7 +18,8 @@ use axum::{
use axum_server::{bind, bind_rustls, tls_rustls::RustlsConfig, Handle as ServerHandle};
#[cfg(feature = "axum_dual_protocol")]
use axum_server_dual_protocol::ServerExt;
pub use conduit::*; // Re-export everything from the library crate
use config::Config;
use database::KeyValueDatabase;
use http::{
header::{self, HeaderName},
Method, StatusCode,
@ -23,6 +29,7 @@ use ruma::api::client::{
error::{Error as RumaError, ErrorBody, ErrorKind},
uiaa::UiaaResponse,
};
use service::{pdu::PduEvent, Services};
use tokio::{
signal,
sync::oneshot::{self, Sender},
@ -37,8 +44,25 @@ use tower_http::{
};
use tracing::{debug, error, info, warn, Level};
use tracing_subscriber::{prelude::*, reload, EnvFilter, Registry};
use utils::error::{Error, Result};
mod api;
mod clap;
mod config;
mod database;
mod routes;
mod service;
mod utils;
pub static SERVICES: RwLock<Option<&'static Services<'static>>> = RwLock::new(None);
#[must_use]
pub fn services() -> &'static Services<'static> {
SERVICES
.read()
.unwrap()
.expect("SERVICES should be initialized when this is called")
}
#[cfg(all(not(target_env = "msvc"), feature = "jemalloc", not(feature = "hardened_malloc")))]
#[global_allocator]
@ -71,17 +95,17 @@ fn main() -> Result<(), Error> {
async fn async_main(server: &Server) -> Result<(), Error> {
if let Err(error) = start(server).await {
error!("Critical error starting server: {error}");
return Err(Error::Error(format!("{error}")));
return Err(Error::Err(format!("{error}")));
}
if let Err(error) = run(server).await {
error!("Critical error running server: {error}");
return Err(Error::Error(format!("{error}")));
return Err(Error::Err(format!("{error}")));
}
if let Err(error) = stop(server).await {
error!("Critical error stopping server: {error}");
return Err(Error::Error(format!("{error}")));
return Err(Error::Err(format!("{error}")));
}
Ok(())

View file

@ -6,14 +6,15 @@ use axum::{
routing::{any, get, on, post, MethodFilter},
Router,
};
use conduit::{
api::{client_server, server_server},
Config, Error, Result, Ruma, RumaResponse,
};
use http::{Method, Uri};
use ruma::api::{client::error::ErrorKind, IncomingRequest};
use tracing::{info, warn};
use crate::{
api::{client_server, server_server},
Config, Error, Result, Ruma, RumaResponse,
};
pub fn routes(config: &Config) -> Router {
let router = Router::new()
.ruma_route(client_server::get_supported_versions_route)
@ -208,6 +209,9 @@ pub fn routes(config: &Config) -> Router {
.ruma_route(server_server::get_event_authorization_route)
.ruma_route(server_server::get_room_state_route)
.ruma_route(server_server::get_room_state_ids_route)
.ruma_route(server_server::create_leave_event_template_route)
.ruma_route(server_server::create_leave_event_v1_route)
.ruma_route(server_server::create_leave_event_v2_route)
.ruma_route(server_server::create_join_event_template_route)
.ruma_route(server_server::create_join_event_v1_route)
.ruma_route(server_server::create_join_event_v2_route)

View file

@ -61,7 +61,7 @@ impl PduEvent {
let mut content = serde_json::from_str(self.content.get())
.map_err(|_| Error::bad_database("PDU in db has invalid content."))?;
redact_content_in_place(&mut content, &room_version_id, self.kind.to_string())
.map_err(|e| Error::RedactionError(self.sender.server_name().to_owned(), e))?;
.map_err(|e| Error::Redaction(self.sender.server_name().to_owned(), e))?;
self.unsigned = Some(
to_raw_value(&json!({

View file

@ -116,7 +116,7 @@ where
debug!("Got {status:?} for {method} {url}");
if !status.is_success() {
return Err(Error::FederationError(
return Err(Error::Federation(
destination.to_owned(),
RumaError::from_http_response(http_response),
));
@ -423,7 +423,7 @@ fn handle_resolve_error(e: &ResolveError) -> Result<()> {
},
_ => {
error!("{e}");
Err(Error::Error(e.to_string()))
Err(Error::Err(e.to_string()))
},
}
}

View file

@ -23,35 +23,35 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
pub enum Error {
#[cfg(feature = "sqlite")]
#[error("There was a problem with the connection to the sqlite database: {source}")]
SqliteError {
Sqlite {
#[from]
source: rusqlite::Error,
},
#[cfg(feature = "rocksdb")]
#[error("There was a problem with the connection to the rocksdb database: {source}")]
RocksDbError {
RocksDb {
#[from]
source: rust_rocksdb::Error,
},
#[error("Could not generate an image.")]
ImageError {
Image {
#[from]
source: image::error::ImageError,
},
#[error("Could not connect to server: {source}")]
ReqwestError {
Reqwest {
#[from]
source: reqwest::Error,
},
#[error("Could build regular expression: {source}")]
RegexError {
Regex {
#[from]
source: regex::Error,
},
#[error("{0}")]
FederationError(OwnedServerName, RumaError),
Federation(OwnedServerName, RumaError),
#[error("Could not do this io: {source}")]
IoError {
Io {
#[from]
source: std::io::Error,
},
@ -69,17 +69,17 @@ pub enum Error {
#[error("{0}")]
Conflict(&'static str), // This is only needed for when a room alias already exists
#[error("{0}")]
ExtensionError(#[from] axum::extract::rejection::ExtensionRejection),
Extension(#[from] axum::extract::rejection::ExtensionRejection),
#[error("{0}")]
PathError(#[from] axum::extract::rejection::PathRejection),
Path(#[from] axum::extract::rejection::PathRejection),
#[error("from {0}: {1}")]
RedactionError(OwnedServerName, ruma::canonical_json::RedactionError),
Redaction(OwnedServerName, ruma::canonical_json::RedactionError),
#[error("{0} in {1}")]
InconsistentRoomState(&'static str, ruma::OwnedRoomId),
#[error("{0}")]
AdminCommand(&'static str),
#[error("{0}")]
Error(String),
Err(String),
}
impl Error {
@ -100,7 +100,7 @@ impl Error {
return RumaResponse(UiaaResponse::AuthResponse(uiaainfo.clone()));
}
if let Self::FederationError(origin, error) = self {
if let Self::Federation(origin, error) = self {
let mut error = error.clone();
error.body = ErrorBody::Standard {
kind: error.error_kind().unwrap_or(&Unknown).clone(),
@ -153,7 +153,7 @@ impl Error {
/// Returns the Matrix error code / error kind
pub fn error_code(&self) -> ErrorKind {
if let Self::FederationError(_, error) = self {
if let Self::Federation(_, error) = self {
return error.error_kind().unwrap_or(&Unknown).clone();
}
@ -169,14 +169,14 @@ impl Error {
match self {
#[cfg(feature = "sqlite")]
Self::SqliteError {
Self::Sqlite {
..
} => db_error,
#[cfg(feature = "rocksdb")]
Self::RocksDbError {
Self::RocksDb {
..
} => db_error,
Self::IoError {
Self::Io {
..
} => db_error,
Self::BadConfig {