add state to router
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
720fbd09c2
commit
038b71fc9d
4 changed files with 19 additions and 18 deletions
|
@ -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 service::{services, user_is_local};
|
||||
|
||||
pub use crate::router::State;
|
||||
pub(crate) use crate::router::{Ruma, RumaResponse};
|
||||
|
||||
conduit::mod_ctor! {}
|
||||
|
|
|
@ -9,15 +9,16 @@ use axum::{
|
|||
routing::{any, get, post},
|
||||
Router,
|
||||
};
|
||||
use conduit::{err, Error, Server};
|
||||
use conduit::{err, Server};
|
||||
use http::Uri;
|
||||
use ruma::api::client::error::ErrorKind;
|
||||
|
||||
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};
|
||||
|
||||
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 router = router
|
||||
.ruma_route(client::get_supported_versions_route)
|
||||
|
|
|
@ -10,7 +10,7 @@ use conduit::Result;
|
|||
use http::Method;
|
||||
use ruma::api::IncomingRequest;
|
||||
|
||||
use super::{Ruma, RumaResponse};
|
||||
use super::{Ruma, RumaResponse, State};
|
||||
|
||||
pub(in super::super) trait RouterExt {
|
||||
fn ruma_route<H, T>(self, handler: H) -> Self
|
||||
|
@ -18,7 +18,7 @@ pub(in super::super) trait RouterExt {
|
|||
H: RumaHandler<T>;
|
||||
}
|
||||
|
||||
impl RouterExt for Router {
|
||||
impl RouterExt for Router<State> {
|
||||
fn ruma_route<H, T>(self, handler: H) -> Self
|
||||
where
|
||||
H: RumaHandler<T>,
|
||||
|
@ -28,9 +28,9 @@ impl RouterExt for Router {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -41,17 +41,17 @@ macro_rules! ruma_handler {
|
|||
Req: IncomingRequest + Send + 'static,
|
||||
Ret: IntoResponse,
|
||||
Fut: Future<Output = Result<Req::OutgoingResponse, Ret>> + Send,
|
||||
Fun: FnOnce($($tx,)* Ruma<Req>) -> Fut + Clone + Send + Sync + 'static,
|
||||
$( $tx: FromRequestParts<()> + Send + 'static, )*
|
||||
Fun: FnOnce($($tx,)* Ruma<Req>,) -> Fut + Clone + Send + Sync + 'static,
|
||||
$( $tx: FromRequestParts<State> + Send + 'static, )*
|
||||
{
|
||||
fn add_routes(&self, router: Router) -> Router {
|
||||
fn add_routes(&self, router: Router<State>) -> Router<State> {
|
||||
Req::METADATA
|
||||
.history
|
||||
.all_paths()
|
||||
.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 method = method_to_filter(&Req::METADATA.method);
|
||||
let action = |$($tx,)* req| async { handle($($tx,)* req).await.map(RumaResponse) };
|
||||
|
|
|
@ -2,20 +2,19 @@ use std::sync::Arc;
|
|||
|
||||
use axum::{response::IntoResponse, routing::get, Router};
|
||||
use conduit::{Error, Server};
|
||||
use conduit_service as service;
|
||||
use http::{StatusCode, Uri};
|
||||
use ruma::api::client::error::ErrorKind;
|
||||
|
||||
extern crate conduit_api as api;
|
||||
extern crate conduit_service as service;
|
||||
|
||||
pub(crate) fn build(server: &Arc<Server>) -> Router {
|
||||
let state = service::services();
|
||||
api::router::build(Router::new(), server)
|
||||
let router = Router::<api::State>::new();
|
||||
|
||||
api::router::build(router, server)
|
||||
.route("/", get(it_works))
|
||||
.fallback(not_found)
|
||||
.with_state(state);
|
||||
|
||||
api::routes::build(router, server)
|
||||
.with_state(service::services())
|
||||
}
|
||||
|
||||
async fn not_found(_uri: Uri) -> impl IntoResponse {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue