mirror of
https://gitlab.com/famedly/conduit.git
synced 2024-12-26 20:43:48 +03:00
feat: simple endpoint handlers
This commit is contained in:
parent
6264628c11
commit
cd777af41c
4 changed files with 181 additions and 30 deletions
58
Cargo.lock
generated
58
Cargo.lock
generated
|
@ -145,6 +145,14 @@ dependencies = [
|
|||
"wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.6"
|
||||
|
@ -276,9 +284,10 @@ name = "matrixserver"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pretty_env_logger 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rocket 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-client-api 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-client-api 0.6.0 (git+https://github.com/ruma/ruma-client-api)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -509,22 +518,23 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "ruma-api"
|
||||
version = "0.13.1"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-api-macros 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-api-macros 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-identifiers 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"strum 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ruma-api-macros"
|
||||
version = "0.10.1"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -535,14 +545,16 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "ruma-client-api"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
source = "git+https://github.com/ruma/ruma-client-api#8e9a6ffededb89bc87c6ac5d067d8d4249eabf04"
|
||||
dependencies = [
|
||||
"http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"js_int 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-api 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-api 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-events 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ruma-identifiers 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"strum 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
@ -645,6 +657,25 @@ name = "state"
|
|||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"strum_macros 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum_macros"
|
||||
version = "0.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "0.15.44"
|
||||
|
@ -733,6 +764,11 @@ dependencies = [
|
|||
"smallvec 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.1.0"
|
||||
|
@ -841,6 +877,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
|
||||
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
|
||||
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
|
||||
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
|
||||
"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"
|
||||
"checksum http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b708cc7f06493459026f53b9a61a7a121a5d1ec6238dee58ea4941132b30156b"
|
||||
"checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
|
||||
|
@ -883,9 +920,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum rocket 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "42c1e9deb3ef4fa430d307bfccd4231434b707ca1328fae339c43ad1201cc6f7"
|
||||
"checksum rocket_codegen 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "79aa1366f9b2eccddc05971e17c5de7bb75a5431eb12c2b5c66545fd348647f4"
|
||||
"checksum rocket_http 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b1391457ee4e80b40d4b57fa5765c0f2836b20d73bcbee4e3f35d93cf3b80817"
|
||||
"checksum ruma-api 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b56cf718a9b575a9ce4fae92399c5c00b9059b2d3fbc6bfb4ff4f00431e50290"
|
||||
"checksum ruma-api-macros 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e2c60086b570af5d8b88c9e2f10b3c4c950436658a104c3e60117eca2b1c466"
|
||||
"checksum ruma-client-api 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aa35e24b165401c1066416d804d830313f27d934dcd71f4388396a42f98ba020"
|
||||
"checksum ruma-api 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3652d110c06f4ca71815d043c7aee3d9d90de5b4b687c037a27563d266fccd5b"
|
||||
"checksum ruma-api-macros 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "876dac1a0fdc5495849219542b4b4ea024153f32dc486e65a613f37c69018627"
|
||||
"checksum ruma-client-api 0.6.0 (git+https://github.com/ruma/ruma-client-api)" = "<none>"
|
||||
"checksum ruma-events 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4a196cbeaa7bffe3756448ee9cf142645790e2a0ece78dfaf10a311eaf5e2a5e"
|
||||
"checksum ruma-events-macros 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "962d93056619ed61826a9d8872c863560e4892ff6a69b70f593baa5ae8b19dc8"
|
||||
"checksum ruma-identifiers 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "07e442c700a3b33fc4dd4a1c4b463ebdd252d2c2db31b83da6bb3009307039b9"
|
||||
|
@ -898,6 +935,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6"
|
||||
"checksum smallvec 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5c2fb2ec9bcd216a5b0d0ccf31ab17b5ed1d627960edff65bbe95d3ce221cefc"
|
||||
"checksum state 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7345c971d1ef21ffdbd103a75990a15eb03604fc8b8852ca8cb418ee1a099028"
|
||||
"checksum strum 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "530efb820d53b712f4e347916c5e7ed20deb76a4f0457943b3182fb889b06d2c"
|
||||
"checksum strum_macros 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5e6e163a520367c465f59e0a61a23cfae3b10b6546d78b6f672a382be79f7110"
|
||||
"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
|
||||
"checksum syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5"
|
||||
"checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
|
||||
|
@ -909,6 +948,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33"
|
||||
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
|
||||
"checksum unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4"
|
||||
"checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
|
||||
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
||||
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
"checksum untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f"
|
||||
|
|
|
@ -9,5 +9,6 @@ edition = "2018"
|
|||
[dependencies]
|
||||
rocket = "0.4.2"
|
||||
http = "0.2.0"
|
||||
ruma-client-api = "0.6.0"
|
||||
ruma-client-api = { git = "https://github.com/ruma/ruma-client-api" }
|
||||
pretty_env_logger = "0.4.0"
|
||||
log = "0.4.8"
|
||||
|
|
108
src/main.rs
108
src/main.rs
|
@ -2,25 +2,111 @@
|
|||
mod ruma_wrapper;
|
||||
|
||||
use {
|
||||
rocket::{get, post, routes},
|
||||
ruma_client_api::r0::account::register,
|
||||
ruma_wrapper::Ruma,
|
||||
rocket::{get, post, put, routes},
|
||||
ruma_client_api::{
|
||||
error::{Error, ErrorKind},
|
||||
r0::{
|
||||
account::register, alias::get_alias, membership::join_room_by_id,
|
||||
message::create_message_event,
|
||||
},
|
||||
unversioned::get_supported_versions,
|
||||
},
|
||||
ruma_wrapper::{MatrixResult, Ruma},
|
||||
std::convert::TryInto,
|
||||
};
|
||||
|
||||
#[get("/_matrix/client/versions")]
|
||||
fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Response> {
|
||||
MatrixResult(Ok(get_supported_versions::Response {
|
||||
versions: vec!["r0.6.0".to_owned()],
|
||||
}))
|
||||
}
|
||||
|
||||
#[post("/_matrix/client/r0/register", data = "<body>")]
|
||||
fn register_route(body: Ruma<register::Request>) -> Ruma<register::Response> {
|
||||
Ruma(register::Response {
|
||||
access_token: "42".to_owned(),
|
||||
home_server: "deprecated".to_owned(),
|
||||
user_id: "@yourrequestedid:homeserver.com".try_into().unwrap(),
|
||||
device_id: body.device_id.clone().unwrap_or_default(),
|
||||
})
|
||||
fn register_route(body: Ruma<register::Request>) -> MatrixResult<register::Response> {
|
||||
let user_id = match (*format!(
|
||||
"@{}:localhost",
|
||||
body.username.clone().unwrap_or("randomname".to_owned())
|
||||
))
|
||||
.try_into()
|
||||
{
|
||||
Err(_) => {
|
||||
return MatrixResult(Err(Error {
|
||||
kind: ErrorKind::InvalidUsername,
|
||||
message: "Username was invalid. ".to_owned(),
|
||||
status_code: http::StatusCode::BAD_REQUEST,
|
||||
}))
|
||||
}
|
||||
Ok(user_id) => user_id,
|
||||
};
|
||||
|
||||
MatrixResult(Ok(register::Response {
|
||||
access_token: "randomtoken".to_owned(),
|
||||
home_server: "localhost".to_owned(),
|
||||
user_id,
|
||||
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()),
|
||||
}))
|
||||
}
|
||||
|
||||
#[get("/_matrix/client/r0/directory/room/<room_alias>")]
|
||||
fn get_alias_route(room_alias: String) -> MatrixResult<get_alias::Response> {
|
||||
let room_id = match &*room_alias {
|
||||
"#room:localhost" => "!xclkjvdlfj:localhost",
|
||||
_ => {
|
||||
return MatrixResult(Err(Error {
|
||||
kind: ErrorKind::NotFound,
|
||||
message: "Room not found.".to_owned(),
|
||||
status_code: http::StatusCode::NOT_FOUND,
|
||||
}))
|
||||
}
|
||||
}
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
MatrixResult(Ok(get_alias::Response {
|
||||
room_id,
|
||||
servers: vec!["localhost".to_owned()],
|
||||
}))
|
||||
}
|
||||
|
||||
#[post("/_matrix/client/r0/rooms/<_room_id>/join", data = "<body>")]
|
||||
fn join_room_by_id_route(
|
||||
_room_id: String,
|
||||
body: Ruma<join_room_by_id::Request>,
|
||||
) -> MatrixResult<join_room_by_id::Response> {
|
||||
MatrixResult(Ok(join_room_by_id::Response {
|
||||
room_id: body.room_id.clone(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[put(
|
||||
"/_matrix/client/r0/rooms/<_room_id>/send/<_event_type>/<_txn_id>",
|
||||
data = "<body>"
|
||||
)]
|
||||
fn create_message_event_route(
|
||||
_room_id: String,
|
||||
_event_type: String,
|
||||
_txn_id: String,
|
||||
body: Ruma<create_message_event::IncomingRequest>,
|
||||
) -> MatrixResult<create_message_event::Response> {
|
||||
dbg!(body.0);
|
||||
MatrixResult(Ok(create_message_event::Response {
|
||||
event_id: "$randomeventid".try_into().unwrap(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
pretty_env_logger::init();
|
||||
rocket::ignite()
|
||||
.mount("/", routes![register_route])
|
||||
.mount(
|
||||
"/",
|
||||
routes![
|
||||
get_supported_versions_route,
|
||||
register_route,
|
||||
get_alias_route,
|
||||
join_room_by_id_route,
|
||||
create_message_event_route,
|
||||
],
|
||||
)
|
||||
.launch();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ use {
|
|||
rocket::response::Responder,
|
||||
rocket::Request,
|
||||
rocket::{Data, Outcome::*},
|
||||
ruma_client_api::error::Error,
|
||||
std::fmt::Debug,
|
||||
std::ops::Deref,
|
||||
std::{
|
||||
convert::{TryFrom, TryInto},
|
||||
|
@ -14,24 +16,33 @@ use {
|
|||
const MESSAGE_LIMIT: u64 = 65535;
|
||||
|
||||
pub struct Ruma<T>(pub T);
|
||||
impl<T: TryFrom<http::Request<Vec<u8>>>> FromDataSimple for Ruma<T> {
|
||||
impl<T: TryFrom<http::Request<Vec<u8>>>> FromDataSimple for Ruma<T>
|
||||
where
|
||||
T::Error: Debug,
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
fn from_data(request: &Request, data: Data) -> Outcome<Self, Self::Error> {
|
||||
let mut handle = data.open().take(MESSAGE_LIMIT);
|
||||
let mut body = Vec::new();
|
||||
handle.read_to_end(&mut body).unwrap();
|
||||
dbg!(&body);
|
||||
let mut http_request = http::Request::builder().uri(request.uri().to_string());
|
||||
let mut http_request = http::Request::builder()
|
||||
.uri(request.uri().to_string())
|
||||
.method(&*request.method().to_string());
|
||||
for header in request.headers().iter() {
|
||||
http_request = http_request.header(header.name.as_str(), &*header.value);
|
||||
}
|
||||
|
||||
let mut handle = data.open().take(MESSAGE_LIMIT);
|
||||
let mut body = Vec::new();
|
||||
handle.read_to_end(&mut body).unwrap();
|
||||
|
||||
let http_request = http_request.body(body).unwrap();
|
||||
|
||||
log::info!("{:?}", http_request);
|
||||
match T::try_from(http_request) {
|
||||
Ok(r) => Success(Ruma(r)),
|
||||
Err(_) => Failure((Status::InternalServerError, ())),
|
||||
Err(e) => {
|
||||
log::error!("{:?}", e);
|
||||
Failure((Status::InternalServerError, ()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,9 +55,22 @@ impl<T> Deref for Ruma<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'r, T: TryInto<http::Response<Vec<u8>>>> Responder<'r> for Ruma<T> {
|
||||
pub struct MatrixResult<T>(pub std::result::Result<T, Error>);
|
||||
impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for MatrixResult<T> {
|
||||
type Error = T::Error;
|
||||
|
||||
fn try_into(self) -> Result<http::Response<Vec<u8>>, T::Error> {
|
||||
match self.0 {
|
||||
Ok(t) => t.try_into(),
|
||||
Err(e) => Ok(e.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r, T: TryInto<http::Response<Vec<u8>>>> Responder<'r> for MatrixResult<T> {
|
||||
fn respond_to(self, _: &Request) -> rocket::response::Result<'r> {
|
||||
match self.0.try_into() {
|
||||
let http_response: Result<http::Response<_>, _> = self.try_into();
|
||||
match http_response {
|
||||
Ok(http_response) => {
|
||||
let mut response = rocket::response::Response::build();
|
||||
response.sized_body(Cursor::new(http_response.body().clone()));
|
||||
|
|
Loading…
Reference in a new issue