Skip to main content

camel_component_http/
registry.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3use std::sync::Arc;
4
5use camel_component_api::CamelError;
6use tokio::sync::{RwLock, mpsc};
7use tokio_util::sync::CancellationToken;
8use tower_http::services::ServeDir;
9
10use crate::RequestEnvelope;
11use crate::rest_match::RestEndpoint;
12
13/// Discriminates between a plain static file mount and
14/// a mount that also performs SPA‑style fallback to index.html.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum MountMode {
17    Static,
18    Spa,
19}
20
21#[allow(dead_code)]
22pub struct StaticMount {
23    pub mount_path: String,
24    pub mode: MountMode,
25    pub dir: PathBuf,
26    pub cache_control: String,
27    pub error_pages: HashMap<u16, PathBuf>,
28    pub serve_dir: ServeDir,
29}
30
31impl std::fmt::Debug for StaticMount {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        f.debug_struct("StaticMount")
34            .field("mount_path", &self.mount_path)
35            .field("mode", &self.mode)
36            .field("dir", &self.dir)
37            .field("cache_control", &self.cache_control)
38            .field("error_pages", &self.error_pages)
39            .finish_non_exhaustive()
40    }
41}
42
43pub(crate) struct HttpRouteRegistryInner {
44    /// Legacy path-keyed API route registry. Used for `http:` routes
45    /// registered without an `httpMethod=` URI param. Multiple routes on
46    /// the same path overwrite each other here.
47    pub api_routes: HashMap<String, mpsc::Sender<RequestEnvelope>>,
48    /// Method-aware REST endpoint registry. Populated by REST-lowered
49    /// `http:` routes (those whose URI carries `httpMethod=...`). Allows
50    /// GET and POST on the same path to coexist, and supports path
51    /// templates like `/users/{id}`. Per plan expert guidance E1.
52    pub rest_endpoints: Vec<RestEndpoint<mpsc::Sender<RequestEnvelope>>>,
53    pub mounts: Vec<StaticMount>,
54}
55
56impl std::fmt::Debug for HttpRouteRegistryInner {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        f.debug_struct("HttpRouteRegistryInner")
59            .field("api_routes", &self.api_routes.keys())
60            .field("rest_endpoints", &self.rest_endpoints.len())
61            .field("mounts", &self.mounts.len())
62            .finish()
63    }
64}
65
66#[derive(Clone)]
67pub struct HttpRouteRegistry {
68    pub(crate) inner: Arc<RwLock<HttpRouteRegistryInner>>,
69    /// Cancelled by `monitor_axum_task` when the shared Axum server task
70    /// backing this registry exits unexpectedly (panic or abort). Every
71    /// `HttpConsumer` hosted on that server selects on this token in its
72    /// `start()` loop; on cancellation the consumer returns `Err`, which
73    /// camel-core's consumer watcher converts into a per-route
74    /// `CrashNotification` → `FailRoute` → supervision backoff restart
75    /// (ADR-0007 route-supervised contract — shared transports must fail
76    /// their hosted routes like per-route transports do).
77    ///
78    /// Registries constructed directly via [`HttpRouteRegistry::new`]
79    /// (tests, embedding) carry a token that is never cancelled — their
80    /// consumers keep the pre-fix semantics.
81    pub(crate) server_exited: CancellationToken,
82}
83
84impl std::fmt::Debug for HttpRouteRegistry {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        f.debug_struct("HttpRouteRegistry").finish_non_exhaustive()
87    }
88}
89
90impl Default for HttpRouteRegistry {
91    fn default() -> Self {
92        Self::new()
93    }
94}
95
96impl HttpRouteRegistry {
97    pub fn new() -> Self {
98        Self::new_with_server_exited(CancellationToken::new())
99    }
100
101    /// Crate-private constructor used by `spawn_entry`: binds the registry
102    /// to the death signal of the shared server that serves it.
103    pub(crate) fn new_with_server_exited(server_exited: CancellationToken) -> Self {
104        Self {
105            inner: Arc::new(RwLock::new(HttpRouteRegistryInner {
106                api_routes: HashMap::new(),
107                rest_endpoints: Vec::new(),
108                mounts: Vec::new(),
109            })),
110            server_exited,
111        }
112    }
113
114    pub async fn register_api_route(&self, path: String, sender: mpsc::Sender<RequestEnvelope>) {
115        let mut inner = self.inner.write().await;
116        inner.api_routes.insert(path, sender);
117    }
118
119    pub async fn unregister_api_route(&self, path: &str) {
120        let mut inner = self.inner.write().await;
121        inner.api_routes.remove(path);
122    }
123
124    /// Register a method-aware REST endpoint. Two endpoints on the same
125    /// path with different methods coexist; two endpoints with the same
126    /// `(method, path)` overwrite (last write wins), matching the
127    /// semantics expected for re-registration of the same logical route.
128    pub async fn register_rest_endpoint(
129        &self,
130        method: String,
131        segments: Vec<crate::rest_match::PathSegment>,
132        sender: mpsc::Sender<RequestEnvelope>,
133    ) {
134        let mut inner = self.inner.write().await;
135        // Drop any prior endpoint with the same (method, segments)
136        // signature so the new sender wins.
137        inner
138            .rest_endpoints
139            .retain(|ep| !(ep.method == method && ep.segments == segments));
140        inner.rest_endpoints.push(RestEndpoint {
141            method,
142            segments,
143            payload: sender,
144        });
145    }
146
147    /// Remove the REST endpoint matching `(method, path_template)`.
148    ///
149    /// Only the endpoint with the SAME method AND segments is removed —
150    /// other HTTP methods sharing the path template are preserved. Stopping
151    /// the `GET /users` consumer must not tear down the live `POST /users`
152    /// endpoint (the core REST multi-verb use case). Fixes review C1.
153    pub async fn unregister_rest_endpoint(&self, method: &str, path_template: &str) {
154        let target_segments = crate::rest_match::parse_path_template(path_template);
155        let mut inner = self.inner.write().await;
156        inner.rest_endpoints.retain(|ep| {
157            // Match BOTH method and segments: a sibling verb on the same
158            // path template stays registered.
159            !(ep.method == method && ep.segments == target_segments)
160        });
161    }
162
163    /// Register a static mount. Duplicate detection is by `mount_path`
164    /// only — every mount on a given port must have a unique prefix.
165    ///
166    /// A single SPA mount is still the convention, but it is no longer
167    /// enforced structurally; the dispatch loop treats all mounts
168    /// uniformly (sorted by longest prefix) and uses `mode` to decide
169    /// whether to attempt SPA-fallback after ServeDir fails.
170    #[allow(dead_code)]
171    pub async fn register_static_mount(&self, mount: StaticMount) -> Result<(), CamelError> {
172        let mut inner = self.inner.write().await;
173        if inner
174            .mounts
175            .iter()
176            .any(|m| m.mount_path == mount.mount_path)
177        {
178            return Err(CamelError::Config(format!(
179                "duplicate static mount path '{}' on this port",
180                mount.mount_path
181            )));
182        }
183        inner.mounts.push(mount);
184        Ok(())
185    }
186
187    /// Unregister a static mount by its unique `mount_path`.
188    #[allow(dead_code)]
189    pub async fn unregister_static_mount(&self, mount_path: &str) {
190        let mut inner = self.inner.write().await;
191        inner.mounts.retain(|m| m.mount_path != mount_path);
192    }
193}