redfish_axum/
cooling_loop_collection.rs1pub struct CoolingLoopCollection<S, P>
8where
9 S: Clone,
10{
11 router: axum::routing::MethodRouter<S>,
12 privilege_marker: std::marker::PhantomData<fn() -> P>,
13 allowed_methods: Vec<axum::http::method::Method>,
14 members_router: axum::routing::MethodRouter<S>,
15 cooling_loop: Option<axum::Router<S>>,
16}
17
18
19impl<S, P> CoolingLoopCollection<S, P>
20where
21 S: AsRef<dyn redfish_core::auth::AuthenticateRequest> + Clone + Send + Sync + 'static,
22 P: redfish_core::privilege::OperationPrivilegeMapping + 'static,
23 <P as redfish_core::privilege::OperationPrivilegeMapping>::Get: Send,
24{
25 pub fn get<H, T>(mut self, handler: H) -> Self
26 where
27 H: axum::handler::Handler<T, S, axum::body::Body>,
28 T: 'static,
29 {
30 let operation = axum::routing::get(
31 |auth: redfish_core::extract::RedfishAuth<P::Get>,
32 axum::extract::State(state): axum::extract::State<S>,
33 mut request: axum::http::Request<axum::body::Body>| async {
34 request.extensions_mut().insert(auth.user);
35 handler.call(request, state).await
36 },
37 );
38 self.router = self.router.get(operation);
39 self.allowed_methods.push(axum::http::method::Method::GET);
40 self
41 }
42
43 pub fn cooling_loop(mut self, cooling_loop: axum::Router<S>) -> Self {
45 self.cooling_loop = Some(cooling_loop);
46 self
47 }
48
49 pub fn into_router(self) -> axum::Router<S> {
50 let Self {
51 router,
52 mut allowed_methods,
53 members_router,
54 cooling_loop,
55 ..
56 } = self;
57 let result = axum::Router::default();
58 let result = match cooling_loop {
59 Some(router) => result.nest("/:cooling_loop_id", router),
60 None => result,
61 };
62 let result = result.route(
63 "/Members",
64 members_router.fallback(|| async {
65 (
66 axum::http::StatusCode::METHOD_NOT_ALLOWED,
67 axum::Json(redfish_core::error::one_message(redfish_codegen::registries::base::v1_16_0::Base::OperationNotAllowed.into())),
68 )
69 })
70 );
71 allowed_methods.dedup();
72 let allow_header = allowed_methods
73 .into_iter()
74 .map(|method| method.to_string())
75 .reduce(|one, two| one + "," + &two)
76 .unwrap();
77 result.route(
78 "/",
79 router.fallback(|| async {
80 (
81 axum::http::StatusCode::METHOD_NOT_ALLOWED,
82 axum::Json(redfish_core::error::one_message(redfish_codegen::registries::base::v1_16_0::Base::OperationNotAllowed.into())),
83 )
84 })
85 .route_layer(axum::middleware::from_fn_with_state(
86 allow_header,
87 |axum::extract::State(allow_header): axum::extract::State<String>,
88 request: axum::http::Request<axum::body::Body>,
89 next: axum::middleware::Next<axum::body::Body>| async move {
90 let apply_allow = matches!(*request.method(), axum::http::Method::GET | axum::http::Method::HEAD);
91 let mut response = next.run(request).await;
92 if apply_allow && !response.headers().contains_key(axum::http::header::ALLOW) {
93 response.headers_mut().insert(
94 axum::http::header::ALLOW,
95 axum::http::HeaderValue::from_str(&allow_header).unwrap(),
96 );
97 }
98 response
99 },
100 )),
101 )
102 }
103}