serve well-known client and server config

This way users can have a simpler time configuring this stuff and we can
worry about the spec compliance parts and specifying the same thing over
and over parts.
This commit is contained in:
Charles Hall 2024-09-05 10:47:02 -07:00
parent 3a55684623
commit 806cc0cb28
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
4 changed files with 99 additions and 1 deletions

58
src/api/well_known.rs Normal file
View file

@ -0,0 +1,58 @@
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
//! Handle requests for `/.well-known/matrix/...` files
use http::StatusCode;
use ruma::api::{
client::discovery::discover_homeserver as client,
federation::discovery::discover_homeserver as server,
};
use crate::{services, Ar, Ra};
/// Handler for `/.well-known/matrix/server`
pub(crate) async fn server(
_: Ar<server::Request>,
) -> Result<Ra<server::Response>, StatusCode> {
let Some(authority) =
services().globals.config.server_discovery.server.authority.clone()
else {
return Err(StatusCode::NOT_FOUND);
};
if authority == services().globals.config.server_name {
// Delegation isn't needed in this case
return Err(StatusCode::NOT_FOUND);
}
Ok(Ra(server::Response::new(authority)))
}
/// Handler for `/.well-known/matrix/client`
pub(crate) async fn client(_: Ar<client::Request>) -> Ra<client::Response> {
let authority = services()
.globals
.config
.server_discovery
.client
.authority
.clone()
.unwrap_or_else(|| services().globals.config.server_name.clone());
let scheme = if services().globals.config.server_discovery.client.insecure {
"http"
} else {
"https"
};
let base_url = format!("{scheme}://{authority}");
// I wish ruma used an actual URL type instead of `String`
Ra(client::Response {
homeserver: client::HomeserverInfo::new(base_url.clone()),
identity_server: None,
sliding_sync_proxy: Some(client::SlidingSyncProxyInfo {
url: base_url,
}),
})
}