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
//! # navree-http
//!
//! `HttpModule`: Navree's router and extractors, the default Tower layer
//! stack, JSON errors, controllers and route metadata.
//!
//! Items hidden from this documentation are not public API: Navree's own
//! macros and crates use them, and they can change in any release.
//!
//! - Modules contribute routes with [`Controller`]s
//! ([`ModuleBuilderHttpExt::controller`]); each [`RouteDef`] carries a
//! [`RouteMeta`] with its access declaration, guards, throttle, timeout,
//! body limit, audit action and OpenAPI metadata. **Deny by default:** a
//! route without [`public`](RouteDef::public),
//! [`authenticated`](RouteDef::authenticated), [`roles`](RouteDef::roles),
//! [`permissions`](RouteDef::permissions) or [`can`](RouteDef::can) fails
//! the boot.
//! - Modules serve any tower service under a path prefix with a [`Mount`]
//! ([`ModuleBuilderMountExt::mount`]), behind the same pipeline and access
//! check as a route, and put a tower layer whose service can fail on the
//! app with [`FallibleLayer`].
//! - Routes are matched by Navree's [`routing::Router`]: no path
//! normalization (dot segments and backslashes are refused), strict
//! percent-decoding of parameters, static before parameter before
//! catch-all, `HEAD`, `OPTIONS` and 405 with `Allow`. Handlers are async
//! functions whose arguments are [`extract`]ors ([`Path`], [`Query`],
//! [`Json`], [`Form`], [`Multipart`], [`Extension`], [`Scoped`], ...).
//! [`ws`] turns a request into a WebSocket.
//! - Responses are anything [`IntoResponse`] (with
//! [`Html`](response::Html), [`Redirect`](response::Redirect) and
//! [server-sent events](response::sse)), bodies are a boxed
//! [`Body`](body::Body) that streams, and [`middleware`] turns an async
//! function into a Tower layer.
//! - [`build_router`] assembles the app as an [`AppRouter`], which
//! [`GlobalLayer`]s wrap. Request pipeline, outermost first:
//! [`LayerSlot::Edge`] (protocols that share the port but not the
//! pipeline, such as gRPC) → request id → tracing and request log → CORS
//! → panic catcher → timeout →
//! security headers → compression (not for `text/event-stream`) →
//! error rendering → maintenance mode → per-IP rate limit → body limit →
//! [`LayerSlot::Session`] → [`LayerSlot::AuthContext`] (the registered
//! [`Authenticator`] runs first) → [`LayerSlot::Uow`] →
//! [`LayerSlot::Context`] → router → per-route:
//! authentication → per-user throttle → [`Guard`]s → [`RouteAuthorizer`]
//! → route timeout → handler → audit action.
//! - [`serve()`] runs an app on a listener: HTTP/1 and HTTP/2 on hyper, with
//! graceful shutdown and the limits of [`ServerOptions`], so a client
//! cannot hold a connection without finishing a request.
//! - [`list`] is the one query-string grammar of lists: filters with
//! operators, sort, search, numbered and cursor pages, read strictly
//! against an allow-list per resource by the [`list::List`] extractor.
//! - Every error is an [`AppError`] rendered in one JSON shape, as
//! `application/json`: `{"statusCode": 404, "message": "Not Found",
//! "error": "Not Found", "code": "not_found"}`; `code` is one of the
//! stable [`codes`] (or an app's own) for front ends to translate, and
//! validation errors are 400s whose `message` lists every field message
//! and whose `errors` maps fields to their messages.
//! This covers panics (`"Internal server error"`, nothing leaked),
//! timeouts, 404s and 405s, and extractor rejections (a body of the wrong
//! shape is a 400), and every response carries `X-Request-Id` and, for
//! allowed origins, the CORS headers.
//!
//! ```
//! use std::sync::Arc;
//!
//! use http::Method;
//! use navree_http::{Controller, Json, ModuleBuilderHttpExt, Path, RouteDef};
//! use navree_kernel::{BoxFuture, Injectable, KernelError, Module, ModuleBuilder, Resolver};
//!
//! struct HelloController;
//!
//! impl Injectable for HelloController {
//! fn construct(_r: Resolver) -> BoxFuture<'static, Result<Self, KernelError>> {
//! Box::pin(async { Ok(HelloController) })
//! }
//! }
//!
//! impl Controller for HelloController {
//! fn prefix(&self) -> &'static str {
//! "/hello"
//! }
//!
//! fn version(&self) -> Option<&'static str> {
//! Some("1")
//! }
//!
//! fn routes(self: Arc<Self>) -> Vec<RouteDef> {
//! vec![
//! // GET /api/v1/hello/{name}
//! RouteDef::on(Method::GET, "/{name}", |Path(name): Path<String>| async move {
//! Json(serde_json::json!({ "hello": name }))
//! })
//! .public(),
//! ]
//! }
//! }
//!
//! struct HelloModule;
//!
//! impl Module for HelloModule {
//! fn register(&self, m: &mut ModuleBuilder) {
//! m.controller::<HelloController>();
//! }
//! }
//! ```
pub use AppRouter;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Handler;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use PathParams;
pub use serve;
pub use ;
pub use RequestWorkspace;
/// Items Navree's own crates use; not public API.