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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use std::convert::Infallible;
use axum::extract::Request;
use axum::handler::Handler;
pub use axum::response::IntoResponse as Responder;
use axum::routing::{MethodFilter, MethodRouter, Route};
use axum::Router;
use tower_layer::Layer;
use tower_service::Service;
#[cfg(feature = "openapi")]
use axum::http::Method;
#[cfg(feature = "openapi")]
use crate::Operable;
#[cfg(feature = "openapi")]
use std::collections::HashMap;
/// Generates the per-HTTP-method shorthand (`get`, `post`, …) on the router.
macro_rules! implement_method {
($method:expr, $fn_name: tt ) => {
#[doc = concat!("Route `", stringify!($fn_name), "` requests for `path` to `handler`.")]
pub fn $fn_name<H: Handler<T, State>, T: 'static>(self, path: &str, handler: H) -> Self {
self.method_route(path, $method, handler)
}
};
}
/// # GotchaRouter
///
/// A router for Gotcha web applications.
pub struct GotchaRouter<State = ()> {
#[cfg(feature = "openapi")]
/// The operations for the router, kept as their `Operable` descriptors: the `Operation` is
/// only built during `into_axum_router`, so every route's schemas are generated inside a
/// single collection scope and can share `components/schemas`.
pub(crate) operations: std::collections::HashMap<(String, Method), &'static Operable>,
/// Optional transform applied to the generated OpenAPI spec before it is served,
/// set via [`GotchaRouter::openapi`]. Lets apps customize `info`, `servers`,
/// `security`, `components`, etc.
#[cfg(feature = "openapi")]
pub(crate) openapi_transform: Option<Box<dyn FnOnce(oas::OpenAPIV3) -> oas::OpenAPIV3 + Send>>,
pub(crate) router: Router<State>,
}
impl<State: Clone + Send + Sync + 'static> Default for GotchaRouter<State> {
fn default() -> Self {
Self {
#[cfg(feature = "openapi")]
operations: Default::default(),
#[cfg(feature = "openapi")]
openapi_transform: None,
router: Router::new(),
}
}
}
impl<State: Clone + Send + Sync + 'static> GotchaRouter<State> {
/// add a route to the router
/// # Examples
///
/// ```rust,no_run
/// use gotcha::{GotchaRouter, Responder};
///
/// async fn hello_world() -> impl Responder {
/// "Hello World!"
/// }
///
/// let router: GotchaRouter<()> = GotchaRouter::default()
/// .route("/", axum::routing::get(hello_world));
/// ```
pub fn route(self, path: &str, method_router: MethodRouter<State>) -> Self {
Self {
#[cfg(feature = "openapi")]
operations: self.operations,
#[cfg(feature = "openapi")]
openapi_transform: self.openapi_transform,
router: self.router.route(path, method_router),
}
}
/// add a method route to the router
/// # Examples
///
/// ```rust,no_run
/// use gotcha::GotchaRouter;
/// use gotcha::axum::routing::MethodFilter;
/// # use gotcha::Responder;
///
/// async fn hello_world() -> impl Responder {
/// "Hello World!"
/// }
///
/// let router: GotchaRouter<()> = GotchaRouter::default()
/// .method_route("/", MethodFilter::GET, hello_world);
/// ```
#[allow(unused_mut)]
pub fn method_route<H, T>(mut self, path: &str, method: MethodFilter, handler: H) -> Self
where
H: Handler<T, State>,
T: 'static,
{
#[cfg(feature = "openapi")]
let handle_operable = extract_operable::<H, T, State>();
#[cfg(feature = "openapi")]
if let Some(operable) = handle_operable {
tracing::info!("generating openapi spec for {}[{}]", &operable.type_name, &path);
let documented_method = match method {
MethodFilter::DELETE => Some(Method::DELETE),
MethodFilter::GET => Some(Method::GET),
MethodFilter::HEAD => Some(Method::HEAD),
MethodFilter::OPTIONS => Some(Method::OPTIONS),
MethodFilter::PATCH => Some(Method::PATCH),
MethodFilter::POST => Some(Method::POST),
MethodFilter::PUT => Some(Method::PUT),
MethodFilter::TRACE => Some(Method::TRACE),
// `MethodFilter` is `#[non_exhaustive]`. A method axum adds later should leave the
// route working and merely undocumented, rather than bringing the application down
// while it registers its routes (this used to be a `todo!()`).
_ => None,
};
match documented_method {
Some(method) => {
self.operations.insert((path.to_string(), method), operable);
}
None => tracing::warn!("unrecognised method filter for {path}; the route works but is left out of the OpenAPI spec"),
}
}
let router = MethodRouter::new().on(method, handler);
Self {
#[cfg(feature = "openapi")]
operations: self.operations,
#[cfg(feature = "openapi")]
openapi_transform: self.openapi_transform,
router: self.router.route(path, router),
}
}
implement_method!(MethodFilter::GET, get);
implement_method!(MethodFilter::POST, post);
implement_method!(MethodFilter::PUT, put);
implement_method!(MethodFilter::PATCH, patch);
implement_method!(MethodFilter::HEAD, head);
implement_method!(MethodFilter::DELETE, delete);
implement_method!(MethodFilter::OPTIONS, options);
implement_method!(MethodFilter::TRACE, trace);
/// nest a router inside another router
/// # Examples
///
/// ```rust,no_run
/// use gotcha::{GotchaRouter, Responder};
///
/// let router: GotchaRouter<()> = GotchaRouter::default()
/// .nest("/users", GotchaRouter::default());
/// ```
pub fn nest(self, path: &str, router: Self) -> Self {
#[cfg(feature = "openapi")]
let operations = router
.operations
.into_iter()
.map(|(key, value)| {
let (path_str, method) = key;
let new_path = format!("{}/{}", path, path_str);
((new_path, method), value)
})
.collect::<HashMap<(String, Method), &'static Operable>>();
Self {
#[cfg(feature = "openapi")]
operations: self.operations.into_iter().chain(operations).collect(),
#[cfg(feature = "openapi")]
openapi_transform: self.openapi_transform,
router: self.router.nest(path, router.router),
}
}
/// merge two routers
/// # Examples
///
/// ```rust,no_run
/// use gotcha::{GotchaRouter};
///
/// let router: GotchaRouter<()> = GotchaRouter::default()
/// .merge(GotchaRouter::default());
/// ```
pub fn merge(self, other: Self) -> Self {
Self {
#[cfg(feature = "openapi")]
operations: self.operations.into_iter().chain(other.operations).collect(),
#[cfg(feature = "openapi")]
openapi_transform: self.openapi_transform,
router: self.router.merge(other.router),
}
}
/// add a layer to the router
/// # Examples
///
/// ```rust,no_run
/// use gotcha::GotchaRouter;
///
/// let router: GotchaRouter<()> = GotchaRouter::default()
/// .layer(gotcha::axum::Extension(0u32));
/// ```
pub fn layer<L>(self, layer: L) -> Self
where
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: Responder + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
{
Self {
#[cfg(feature = "openapi")]
operations: self.operations,
#[cfg(feature = "openapi")]
openapi_transform: self.openapi_transform,
router: self.router.layer(layer),
}
}
/// Handle requests that match no route.
pub fn fallback<H, T>(self, handler: H) -> Self
where
H: Handler<T, State>,
T: 'static,
{
Self {
#[cfg(feature = "openapi")]
operations: self.operations,
#[cfg(feature = "openapi")]
openapi_transform: self.openapi_transform,
router: self.router.fallback(handler),
}
}
/// Handle unmatched requests with a `Service` rather than a handler.
///
/// Useful for delegating to something that is already a tower service — serving a
/// single-page application's `index.html` with `ServeFile`, say, or forwarding to a
/// proxy — where [`fallback`](Self::fallback) would need a wrapper handler.
///
/// ```rust,ignore
/// use gotcha::GotchaRouter;
/// use gotcha::axum::{body::Body, extract::Request, response::Response};
///
/// let router: GotchaRouter<()> = GotchaRouter::default()
/// .fallback_service(tower::service_fn(|_: Request| async {
/// Ok::<_, std::convert::Infallible>(Response::new(Body::from("not found")))
/// }));
/// ```
pub fn fallback_service<Svc, ResBody>(self, service: Svc) -> Self
where
Svc: Service<Request, Response = axum::http::Response<ResBody>, Error = Infallible> + Clone + Send + Sync + 'static,
Svc::Future: Send + 'static,
ResBody: axum::body::HttpBody<Data = axum::body::Bytes> + Send + 'static,
ResBody::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
Self {
#[cfg(feature = "openapi")]
operations: self.operations,
#[cfg(feature = "openapi")]
openapi_transform: self.openapi_transform,
router: self.router.fallback_service(service),
}
}
/// Customize the generated OpenAPI spec before it is served at `/openapi.json`.
///
/// The transform receives the fully-generated [`oas::OpenAPIV3`] (with every route's
/// operation already filled in) and returns the spec to serve, so you can set the
/// title/version, add servers, security schemes, components, and so on.
///
/// ```rust,no_run
/// use gotcha::GotchaRouter;
///
/// let router: GotchaRouter<()> = GotchaRouter::default().openapi(|mut spec| {
/// spec.info.title = "My API".to_string();
/// spec.info.version = "2.0.0".to_string();
/// spec
/// });
/// ```
#[cfg(feature = "openapi")]
pub fn openapi<F>(mut self, transform: F) -> Self
where
F: FnOnce(oas::OpenAPIV3) -> oas::OpenAPIV3 + Send + 'static,
{
self.openapi_transform = Some(Box::new(transform));
self
}
/// Finalize this router into a plain `axum::Router`, injecting `state`.
///
/// When the `openapi` feature is enabled, this also mounts the generated
/// spec at `/openapi.json` and the Redoc / Scalar UIs at `/redoc` and
/// `/scalar`. This is the single assembly path shared by both the
/// [`GotchaApp`](crate::GotchaApp) trait and the [`Gotcha`](crate::Gotcha)
/// builder.
pub(crate) fn into_axum_router(self, state: State) -> Router {
cfg_if::cfg_if! {
if #[cfg(feature = "openapi")] {
let mut openapi_spec = crate::openapi::generate_openapi(self.operations);
if let Some(transform) = self.openapi_transform {
openapi_spec = transform(openapi_spec);
}
self.router
.with_state(state)
.route("/openapi.json", axum::routing::get(move || async move { axum::Json(openapi_spec.clone()) }))
.route("/redoc", axum::routing::get(crate::openapi::openapi_html))
.route("/scalar", axum::routing::get(crate::openapi::scalar_html))
} else {
self.router.with_state(state)
}
}
}
}
#[doc(hidden)]
#[cfg(feature = "openapi")]
pub fn extract_operable<H, T, State>() -> Option<&'static Operable>
where
H: Handler<T, State>,
T: 'static,
{
let handle_name = std::any::type_name::<H>();
inventory::iter::<Operable>.into_iter().find(|it| it.type_name.eq(handle_name))
}
#[cfg(all(test, feature = "openapi"))]
mod tests {
use std::sync::{Arc, Mutex};
use super::*;
#[test]
fn openapi_transform_runs_during_assembly() {
// Capture the title the transform sees, to prove `.openapi(..)` is stored and applied
// when the router is finalized (the transformed spec is what gets served).
let captured: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
let sink = captured.clone();
let router: GotchaRouter<()> = GotchaRouter::default().openapi(move |mut spec| {
spec.info.title = "Custom API".to_string();
spec.info.version = "9.9.9".to_string();
*sink.lock().unwrap() = Some(spec.info.title.clone());
spec
});
let _ = router.into_axum_router(());
assert_eq!(captured.lock().unwrap().as_deref(), Some("Custom API"));
}
#[test]
fn openapi_transform_survives_chained_builder_calls() {
// `.openapi(..)` set before other methods must not be dropped by the `Self { .. }`
// reconstructions in `route`/`layer`/etc.
let ran: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
let flag = ran.clone();
let router: GotchaRouter<()> = GotchaRouter::default()
.openapi(move |spec| {
*flag.lock().unwrap() = true;
spec
})
.route("/health", axum::routing::get(|| async { "ok" }))
.fallback(|| async { "not found" });
let _ = router.into_axum_router(());
assert!(*ran.lock().unwrap(), "transform set before route()/fallback() must still apply");
}
}