You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-31 09:24:31 +03:00
Make the ServerLayer work properly with axum
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -2074,6 +2074,7 @@ dependencies = [
|
|||||||
"mas-config",
|
"mas-config",
|
||||||
"mas-data-model",
|
"mas-data-model",
|
||||||
"mas-email",
|
"mas-email",
|
||||||
|
"mas-http",
|
||||||
"mas-iana",
|
"mas-iana",
|
||||||
"mas-jose",
|
"mas-jose",
|
||||||
"mas-static-files",
|
"mas-static-files",
|
||||||
@ -2112,6 +2113,7 @@ dependencies = [
|
|||||||
"opentelemetry",
|
"opentelemetry",
|
||||||
"opentelemetry-http",
|
"opentelemetry-http",
|
||||||
"opentelemetry-semantic-conventions",
|
"opentelemetry-semantic-conventions",
|
||||||
|
"pin-project-lite",
|
||||||
"rustls 0.20.4",
|
"rustls 0.20.4",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -25,6 +25,7 @@ use hyper::Server;
|
|||||||
use mas_axum_utils::UrlBuilder;
|
use mas_axum_utils::UrlBuilder;
|
||||||
use mas_config::RootConfig;
|
use mas_config::RootConfig;
|
||||||
use mas_email::{MailTransport, Mailer};
|
use mas_email::{MailTransport, Mailer};
|
||||||
|
use mas_http::ServerLayer;
|
||||||
use mas_storage::MIGRATOR;
|
use mas_storage::MIGRATOR;
|
||||||
use mas_tasks::TaskQueue;
|
use mas_tasks::TaskQueue;
|
||||||
use mas_templates::Templates;
|
use mas_templates::Templates;
|
||||||
@ -215,7 +216,8 @@ impl Options {
|
|||||||
&encrypter,
|
&encrypter,
|
||||||
&mailer,
|
&mailer,
|
||||||
&url_builder,
|
&url_builder,
|
||||||
);
|
)
|
||||||
|
.layer(ServerLayer::default());
|
||||||
|
|
||||||
info!("Listening on http://{}", listener.local_addr().unwrap());
|
info!("Listening on http://{}", listener.local_addr().unwrap());
|
||||||
|
|
||||||
|
@ -56,16 +56,17 @@ rand = "0.8.5"
|
|||||||
headers = "0.3.7"
|
headers = "0.3.7"
|
||||||
|
|
||||||
oauth2-types = { path = "../oauth2-types" }
|
oauth2-types = { path = "../oauth2-types" }
|
||||||
|
mas-axum-utils = { path = "../axum-utils" }
|
||||||
mas-config = { path = "../config" }
|
mas-config = { path = "../config" }
|
||||||
mas-data-model = { path = "../data-model" }
|
mas-data-model = { path = "../data-model" }
|
||||||
mas-email = { path = "../email" }
|
mas-email = { path = "../email" }
|
||||||
|
mas-http = { path = "../http" }
|
||||||
mas-iana = { path = "../iana" }
|
mas-iana = { path = "../iana" }
|
||||||
mas-jose = { path = "../jose" }
|
mas-jose = { path = "../jose" }
|
||||||
mas-static-files = { path = "../static-files" }
|
mas-static-files = { path = "../static-files" }
|
||||||
mas-storage = { path = "../storage" }
|
mas-storage = { path = "../storage" }
|
||||||
mas-templates = { path = "../templates" }
|
mas-templates = { path = "../templates" }
|
||||||
mas-warp-utils = { path = "../warp-utils" }
|
mas-warp-utils = { path = "../warp-utils" }
|
||||||
mas-axum-utils = { path = "../axum-utils" }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
indoc = "1.0.4"
|
indoc = "1.0.4"
|
||||||
|
@ -15,6 +15,7 @@ hyper-rustls = { version = "0.23.0", features = ["http1", "http2"] }
|
|||||||
opentelemetry = "0.17.0"
|
opentelemetry = "0.17.0"
|
||||||
opentelemetry-http = "0.6.0"
|
opentelemetry-http = "0.6.0"
|
||||||
opentelemetry-semantic-conventions = "0.9.0"
|
opentelemetry-semantic-conventions = "0.9.0"
|
||||||
|
pin-project-lite = "0.2.8"
|
||||||
rustls = "0.20.4"
|
rustls = "0.20.4"
|
||||||
serde = "1.0.136"
|
serde = "1.0.136"
|
||||||
serde_json = "1.0.79"
|
serde_json = "1.0.79"
|
||||||
|
@ -12,45 +12,34 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
use std::{marker::PhantomData, time::Duration};
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use http::{Request, Response};
|
use http::{Request, Response};
|
||||||
use http_body::combinators::BoxBody;
|
use tower::{util::BoxCloneService, Layer, Service, ServiceBuilder, ServiceExt};
|
||||||
use tower::{
|
use tower_http::{compression::CompressionBody, ServiceBuilderExt};
|
||||||
timeout::TimeoutLayer, util::BoxCloneService, Layer, Service, ServiceBuilder, ServiceExt,
|
|
||||||
};
|
|
||||||
use tower_http::compression::{CompressionBody, CompressionLayer};
|
|
||||||
|
|
||||||
use super::otel::TraceLayer;
|
use super::otel::TraceLayer;
|
||||||
use crate::BoxError;
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct ServerLayer<ReqBody> {
|
pub struct ServerLayer<ReqBody> {
|
||||||
_t: PhantomData<ReqBody>,
|
_t: PhantomData<ReqBody>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ReqBody, ResBody, S, E> Layer<S> for ServerLayer<ReqBody>
|
impl<ReqBody, ResBody, S> Layer<S> for ServerLayer<ReqBody>
|
||||||
where
|
where
|
||||||
S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = E> + Clone + Send + 'static,
|
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone + Send + 'static,
|
||||||
ReqBody: http_body::Body + 'static,
|
|
||||||
ResBody: http_body::Body + Sync + Send + 'static,
|
|
||||||
ResBody::Error: std::fmt::Display + 'static,
|
|
||||||
S::Future: Send + 'static,
|
S::Future: Send + 'static,
|
||||||
E: std::error::Error + Into<BoxError>,
|
S::Error: std::fmt::Display,
|
||||||
|
ReqBody: http_body::Body + 'static,
|
||||||
|
ResBody: http_body::Body + Send + 'static,
|
||||||
{
|
{
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
type Service = BoxCloneService<
|
type Service = BoxCloneService<Request<ReqBody>, Response<CompressionBody<ResBody>>, S::Error>;
|
||||||
Request<ReqBody>,
|
|
||||||
Response<CompressionBody<BoxBody<ResBody::Data, ResBody::Error>>>,
|
|
||||||
BoxError,
|
|
||||||
>;
|
|
||||||
|
|
||||||
fn layer(&self, inner: S) -> Self::Service {
|
fn layer(&self, inner: S) -> Self::Service {
|
||||||
ServiceBuilder::new()
|
ServiceBuilder::new()
|
||||||
.layer(CompressionLayer::new())
|
.compression()
|
||||||
.layer(TraceLayer::http_server())
|
.layer(TraceLayer::http_server())
|
||||||
.map_response(|r: Response<_>| r.map(BoxBody::new))
|
|
||||||
.layer(TimeoutLayer::new(Duration::from_secs(10)))
|
|
||||||
.service(inner)
|
.service(inner)
|
||||||
.boxed_clone()
|
.boxed_clone()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user