1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use std::collections::HashMap;
use std::sync::Arc;
use axum::routing::MethodRouter;
use axum::Router;
use proa_framework_core::{compute_metadata_map, resolve_route_specs, ResolvedRouteSpec};
pub use proa_framework_core::{RouteMode, RouteSpec, StaticPathsFn};
use super::context::EndpointContextLayer;
use super::layout::LayoutRef;
use super::metadata::{ComputedRouteMeta, MetadataRef};
/// Declarative route specification
#[derive(Clone)]
pub struct Route<State: Clone + Send + Sync + 'static> {
pub path: &'static str,
pub endpoint: Option<MethodRouter<State>>,
pub layout: Option<LayoutRef>,
pub metadata: Option<MetadataRef>, // For endpoints without layouts
pub children: Vec<Route<State>>,
pub route_mode: RouteMode,
/// Optional function to generate static paths for dynamic routes at build time
pub static_paths: Option<StaticPathsFn>,
/// Cache policy for this endpoint only; child routes configure their own.
pub page_cache: Option<super::PageCache>,
}
impl<State> Route<State>
where
State: Clone + Send + Sync + 'static,
{
pub fn spec(&self) -> RouteSpec {
RouteSpec {
path: self.path,
has_endpoint: self.endpoint.is_some(),
layout_metadata: self.layout.map(|layout| layout as MetadataRef),
metadata: self.metadata,
children: self.children.iter().map(Self::spec).collect(),
route_mode: self.route_mode,
static_paths: self.static_paths,
}
}
/// Creates a route with just an endpoint
pub fn endpoint(path: &'static str, router: MethodRouter<State>) -> Self {
Self {
path,
endpoint: Some(router),
layout: None,
metadata: None,
children: Vec::new(),
route_mode: RouteMode::default(),
static_paths: None,
page_cache: None,
}
}
/// Creates a route with an endpoint and title
pub fn page(path: &'static str, title: &'static str, router: MethodRouter<State>) -> Self {
Self {
path,
endpoint: Some(router),
layout: None,
metadata: Some(Box::leak(Box::new(super::metadata::TitleOnly::new(title)))),
children: Vec::new(),
route_mode: RouteMode::default(),
static_paths: None,
page_cache: None,
}
}
/// Creates a route with an endpoint and full metadata
pub fn page_with_metadata(
path: &'static str,
router: MethodRouter<State>,
metadata: MetadataRef,
) -> Self {
Self {
path,
endpoint: Some(router),
layout: None,
metadata: Some(metadata),
children: Vec::new(),
route_mode: RouteMode::default(),
static_paths: None,
page_cache: None,
}
}
/// Creates a metadata layout route with children.
///
/// Shared page chrome should be composed in a render component. The
/// layout contributes cascading metadata such as title templates.
pub fn layout(path: &'static str, layout: LayoutRef, children: Vec<Route<State>>) -> Self {
Self {
path,
endpoint: None,
layout: Some(layout),
metadata: None,
children,
route_mode: RouteMode::default(),
static_paths: None,
page_cache: None,
}
}
/// Builder method to add children
pub fn with_children(mut self, children: Vec<Route<State>>) -> Self {
self.children = children;
self
}
/// Builder method to add an endpoint
pub fn with_endpoint(mut self, router: MethodRouter<State>) -> Self {
self.endpoint = Some(router);
self
}
/// Configure CDN caching for this public page with `PageCache::new()`.
///
/// Add `.revalidate(interval)` to the policy for CDN-backed ISR. Edge hits
/// skip the application server; no in-process HTML cache is required.
/// Configure HTML caching and post-rollout invalidation with your CDN.
/// `proa deploy run` can invoke the integration in `deploy.cdn`.
/// A new build alone does not purge the CDN.
///
/// Applied when the route is mounted, so replacing the endpoint with
/// `with_endpoint` retains its cache policy. A layout without an endpoint
/// does not cache its children. The policy does not apply to child routes.
/// Existing `PageCacheLayer` conversions remain supported for compatibility.
pub fn with_page_cache(mut self, cache: impl Into<super::PageCache>) -> Self {
self.page_cache = Some(cache.into());
self
}
/// Convert this route and its children into a router for `Router::merge`.
///
/// Endpoint layers retain resolved metadata stacks and route parameters.
/// Mount through `FrameworkBuilder` or install `RouteContextLayer` to
/// establish the framework request context. Use `router_from_spec` when
/// the caller also needs the precomputed metadata map.
pub fn into_router(self) -> Router<State> {
router_from_spec(vec![self]).0
}
/// Builder method to add a layout
pub fn with_layout(mut self, layout: LayoutRef) -> Self {
self.layout = Some(layout);
self
}
/// Builder method to add metadata
pub fn with_metadata(mut self, metadata: MetadataRef) -> Self {
self.metadata = Some(metadata);
self
}
/// Builder method to set routing mode
pub fn with_route_mode(mut self, route_mode: RouteMode) -> Self {
self.route_mode = route_mode;
self
}
/// Builder method to provide static paths for dynamic routes.
/// Only used when route_mode is RouteMode::Static.
///
/// # Example
/// ```rust
/// use std::collections::HashMap;
/// use axum::routing::get;
/// use proa_framework_axum::{Route, RouteMode};
///
/// fn get_blog_slugs() -> Vec<HashMap<&'static str, String>> {
/// vec![
/// HashMap::from([("slug", "intro".to_string())]),
/// HashMap::from([("slug", "tutorial".to_string())]),
/// ]
/// }
///
/// async fn blog_handler() -> &'static str {
/// "ok"
/// }
///
/// let _route = Route::<()>::page("/blog/{slug}", "Blog Post", get(blog_handler))
/// .with_route_mode(RouteMode::Static)
/// .with_static_paths(get_blog_slugs);
/// ```
pub fn with_static_paths(mut self, paths_fn: StaticPathsFn) -> Self {
self.static_paths = Some(paths_fn);
self
}
}
/// Builds an Axum Router from a declarative route specification
/// Returns the router and the precomputed metadata map
pub fn router_from_spec<State>(
routes: Vec<Route<State>>,
) -> (Router<State>, Arc<HashMap<String, ComputedRouteMeta>>)
where
State: Clone + Send + Sync + 'static,
{
let route_specs: Vec<RouteSpec> = routes.iter().map(Route::spec).collect();
let computed_metadata = Arc::new(compute_metadata_map(&route_specs));
let resolved_specs = resolve_route_specs(&route_specs);
// Second pass: build the router
let router = routes
.into_iter()
.zip(resolved_specs)
.fold(Router::new(), |router, (route, resolved)| {
router.merge(into_router(route, resolved))
});
(router, computed_metadata)
}
fn into_router<State>(route: Route<State>, resolved: ResolvedRouteSpec) -> Router<State>
where
State: Clone + Send + Sync + 'static,
{
let Route {
path: _,
endpoint,
layout: _,
metadata: _,
children,
route_mode: _,
static_paths: _,
page_cache,
} = route;
let mut router = Router::new();
if let Some(method_router) = endpoint {
let method_router = match page_cache {
Some(cache) => cache.apply_to(method_router),
None => method_router,
};
let method_router = method_router.layer(EndpointContextLayer::new(
Arc::from(resolved.metadata_stack),
Arc::from(resolved.full_path.as_str()),
));
router = router.route(resolved.full_path.as_str(), method_router);
}
debug_assert_eq!(children.len(), resolved.children.len());
for (child, child_resolved) in children.into_iter().zip(resolved.children) {
router = router.merge(into_router(child, child_resolved));
}
router
}