Skip to main content

rs_zero/rest/
layer_stack.rs

1use axum::Router;
2
3use crate::rest::{RestConfig, middleware::apply_default_layers};
4
5/// Tower-first REST layer stack builder.
6///
7/// The current implementation preserves the existing rs-zero REST middleware
8/// order while exposing an explicit stack object. Future releases can move
9/// individual middleware pieces behind concrete Tower [`tower::Layer`] types
10/// without changing [`RestServer`](crate::rest::RestServer) callers.
11#[derive(Debug, Clone)]
12pub struct RestLayerStack {
13    config: RestConfig,
14}
15
16impl RestLayerStack {
17    /// Creates a REST layer stack from runtime configuration.
18    pub fn new(config: RestConfig) -> Self {
19        Self { config }
20    }
21
22    /// Creates a production-oriented REST layer stack.
23    pub fn go_zero_defaults(name: impl Into<String>) -> Self {
24        Self::new(RestConfig::go_zero_defaults(name))
25    }
26
27    /// Applies the stack to an axum router.
28    pub fn layer(&self, router: Router) -> Router {
29        apply_default_layers(router, self.config.clone())
30    }
31
32    /// Returns an axum-compatible layer value.
33    pub fn into_layer(self) -> RestRouterLayer {
34        RestRouterLayer {
35            config: self.config,
36        }
37    }
38}
39
40/// Axum router layer produced by [`RestLayerStack`].
41#[derive(Debug, Clone)]
42pub struct RestRouterLayer {
43    config: RestConfig,
44}
45
46impl tower::Layer<Router> for RestRouterLayer {
47    type Service = Router;
48
49    fn layer(&self, inner: Router) -> Self::Service {
50        apply_default_layers(inner, self.config.clone())
51    }
52}