Skip to main content

waveless_runtime/services/http_facade/
handler.rs

1// Waveless
2// Copyright (C) 2026 Oscar Alvarez Gonzalez
3
4use waveless_commons::databases::DatabaseConsumer;
5
6use crate::*;
7
8/// TODO: add documentation.
9#[derive(Clone, Debug)]
10pub struct ExecuteHandler;
11
12impl Service<RequestCx> for ExecuteHandler {
13    type Response = ResponseCx;
14
15    type Error = RequestError;
16
17    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
18
19    fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
20        Poll::Ready(Ok(()))
21    }
22
23    /// Handles endpoints requests.
24    fn call(&mut self, cx: RequestCx) -> Self::Future {
25        let future: Pin<_> = Box::pin(async move {
26            let RequestCx { endpoint, .. } = &cx;
27
28            // Retrieves the endpoint's target database.
29            let db_conns = endpoint.get_db_handle()?;
30
31            // Force the endpoint to have the HTTP target.
32            let ExecutionTarget::Http(http_target) = endpoint.execution_target().to_owned() else {
33                unreachable!()
34            };
35
36            let Some(execution_atom) = http_target.execution_pipeline() else {
37                return Err(RequestError::Expected(
38                    StatusCode::INTERNAL_SERVER_ERROR,
39                    format!("The route doesn't have any executor defined. HINT: Go to your project's endpoints folder and check that '{}' has an executor set.", endpoint.id()).into(),
40                ));
41            };
42
43            // Initialize the pipeline context.
44            let mut pipeline_cx = PipelineCx::new(cx);
45
46            // Run the executors.
47            pipeline_cx = execution_atom.execute(pipeline_cx, db_conns).await?;
48
49            let PipelineCx { response, .. } = pipeline_cx;
50
51            Ok(response.unwrap_or_default())
52        }).into();
53
54        future as Self::Future // `rust-analyzer` complains here.
55    }
56}