add state to router

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-15 03:56:27 +00:00
parent 720fbd09c2
commit 038b71fc9d
4 changed files with 19 additions and 18 deletions

View file

@ -8,6 +8,7 @@ extern crate conduit_service as service;
pub(crate) use conduit::{debug_info, debug_warn, pdu::PduEvent, utils, Error, Result}; pub(crate) use conduit::{debug_info, debug_warn, pdu::PduEvent, utils, Error, Result};
pub(crate) use service::{services, user_is_local}; pub(crate) use service::{services, user_is_local};
pub use crate::router::State;
pub(crate) use crate::router::{Ruma, RumaResponse}; pub(crate) use crate::router::{Ruma, RumaResponse};
conduit::mod_ctor! {} conduit::mod_ctor! {}

View file

@ -9,15 +9,16 @@ use axum::{
routing::{any, get, post}, routing::{any, get, post},
Router, Router,
}; };
use conduit::{err, Error, Server}; use conduit::{err, Server};
use http::Uri; use http::Uri;
use ruma::api::client::error::ErrorKind;
use self::handler::RouterExt; use self::handler::RouterExt;
pub(super) use self::{ar::Ruma, response::RumaResponse}; pub(super) use self::{args::Args as Ruma, response::RumaResponse};
use crate::{client, server}; use crate::{client, server};
pub fn build(router: Router, server: &Server) -> Router { pub type State = &'static service::Services;
pub fn build(router: Router<State>, server: &Server) -> Router<State> {
let config = &server.config; let config = &server.config;
let router = router let router = router
.ruma_route(client::get_supported_versions_route) .ruma_route(client::get_supported_versions_route)

View file

@ -10,7 +10,7 @@ use conduit::Result;
use http::Method; use http::Method;
use ruma::api::IncomingRequest; use ruma::api::IncomingRequest;
use super::{Ruma, RumaResponse}; use super::{Ruma, RumaResponse, State};
pub(in super::super) trait RouterExt { pub(in super::super) trait RouterExt {
fn ruma_route<H, T>(self, handler: H) -> Self fn ruma_route<H, T>(self, handler: H) -> Self
@ -18,7 +18,7 @@ pub(in super::super) trait RouterExt {
H: RumaHandler<T>; H: RumaHandler<T>;
} }
impl RouterExt for Router { impl RouterExt for Router<State> {
fn ruma_route<H, T>(self, handler: H) -> Self fn ruma_route<H, T>(self, handler: H) -> Self
where where
H: RumaHandler<T>, H: RumaHandler<T>,
@ -28,9 +28,9 @@ impl RouterExt for Router {
} }
pub(in super::super) trait RumaHandler<T> { pub(in super::super) trait RumaHandler<T> {
fn add_routes(&self, router: Router) -> Router; fn add_routes(&self, router: Router<State>) -> Router<State>;
fn add_route(&self, router: Router, path: &str) -> Router; fn add_route(&self, router: Router<State>, path: &str) -> Router<State>;
} }
macro_rules! ruma_handler { macro_rules! ruma_handler {
@ -41,17 +41,17 @@ macro_rules! ruma_handler {
Req: IncomingRequest + Send + 'static, Req: IncomingRequest + Send + 'static,
Ret: IntoResponse, Ret: IntoResponse,
Fut: Future<Output = Result<Req::OutgoingResponse, Ret>> + Send, Fut: Future<Output = Result<Req::OutgoingResponse, Ret>> + Send,
Fun: FnOnce($($tx,)* Ruma<Req>) -> Fut + Clone + Send + Sync + 'static, Fun: FnOnce($($tx,)* Ruma<Req>,) -> Fut + Clone + Send + Sync + 'static,
$( $tx: FromRequestParts<()> + Send + 'static, )* $( $tx: FromRequestParts<State> + Send + 'static, )*
{ {
fn add_routes(&self, router: Router) -> Router { fn add_routes(&self, router: Router<State>) -> Router<State> {
Req::METADATA Req::METADATA
.history .history
.all_paths() .all_paths()
.fold(router, |router, path| self.add_route(router, path)) .fold(router, |router, path| self.add_route(router, path))
} }
fn add_route(&self, router: Router, path: &str) -> Router { fn add_route(&self, router: Router<State>, path: &str) -> Router<State> {
let handle = self.clone(); let handle = self.clone();
let method = method_to_filter(&Req::METADATA.method); let method = method_to_filter(&Req::METADATA.method);
let action = |$($tx,)* req| async { handle($($tx,)* req).await.map(RumaResponse) }; let action = |$($tx,)* req| async { handle($($tx,)* req).await.map(RumaResponse) };

View file

@ -2,20 +2,19 @@ use std::sync::Arc;
use axum::{response::IntoResponse, routing::get, Router}; use axum::{response::IntoResponse, routing::get, Router};
use conduit::{Error, Server}; use conduit::{Error, Server};
use conduit_service as service;
use http::{StatusCode, Uri}; use http::{StatusCode, Uri};
use ruma::api::client::error::ErrorKind; use ruma::api::client::error::ErrorKind;
extern crate conduit_api as api; extern crate conduit_api as api;
extern crate conduit_service as service;
pub(crate) fn build(server: &Arc<Server>) -> Router { pub(crate) fn build(server: &Arc<Server>) -> Router {
let state = service::services(); let router = Router::<api::State>::new();
api::router::build(Router::new(), server)
api::router::build(router, server)
.route("/", get(it_works)) .route("/", get(it_works))
.fallback(not_found) .fallback(not_found)
.with_state(state); .with_state(service::services())
api::routes::build(router, server)
} }
async fn not_found(_uri: Uri) -> impl IntoResponse { async fn not_found(_uri: Uri) -> impl IntoResponse {