-
Notifications
You must be signed in to change notification settings - Fork 967
/
service.rs
158 lines (139 loc) · 4.33 KB
/
service.rs
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
use super::{
future::RouteFuture, url_params, Endpoint, FallbackRoute, Node, Route, RouteId, Router,
};
use crate::{
body::{Body, HttpBody},
response::Response,
};
use http::Request;
use matchit::MatchError;
use std::{
collections::HashMap,
convert::Infallible,
sync::Arc,
task::{Context, Poll},
};
use sync_wrapper::SyncWrapper;
use tower::Service;
/// A [`Router`] converted into a [`Service`].
#[derive(Debug)]
pub struct RouterService<B = Body> {
routes: HashMap<RouteId, Route<B>>,
node: Arc<Node>,
fallback: FallbackRoute<B>,
}
impl<B> RouterService<B>
where
B: HttpBody + Send + 'static,
{
#[track_caller]
pub(super) fn new<S>(router: Router<S, B>) -> Self
where
S: Clone + Send + Sync + 'static,
{
let state = router
.state
.expect("Can't turn a `Router` that wants to inherit state into a service");
let routes = router
.routes
.into_iter()
.map(|(route_id, endpoint)| {
let route = match endpoint {
Endpoint::MethodRouter(method_router) => {
Route::new(method_router.with_state(state.clone()))
}
Endpoint::Route(route) => route,
};
(route_id, route)
})
.collect();
Self {
routes,
node: router.node,
fallback: router.fallback.into_fallback_route(&state),
}
}
#[inline]
fn call_route(
&self,
match_: matchit::Match<&RouteId>,
mut req: Request<B>,
) -> RouteFuture<B, Infallible> {
let id = *match_.value;
#[cfg(feature = "matched-path")]
crate::extract::matched_path::set_matched_path_for_request(
id,
&self.node.route_id_to_path,
req.extensions_mut(),
);
url_params::insert_url_params(req.extensions_mut(), match_.params);
let mut route = self
.routes
.get(&id)
.expect("no route for id. This is a bug in axum. Please file an issue")
.clone();
route.call(req)
}
}
impl<B> Clone for RouterService<B> {
fn clone(&self) -> Self {
Self {
routes: self.routes.clone(),
node: Arc::clone(&self.node),
fallback: self.fallback.clone(),
}
}
}
impl<B> Service<Request<B>> for RouterService<B>
where
B: HttpBody + Send + 'static,
{
type Response = Response;
type Error = Infallible;
type Future = RouteFuture<B, Infallible>;
#[inline]
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
#[inline]
fn call(&mut self, mut req: Request<B>) -> Self::Future {
#[cfg(feature = "original-uri")]
{
use crate::extract::OriginalUri;
if req.extensions().get::<OriginalUri>().is_none() {
let original_uri = OriginalUri(req.uri().clone());
req.extensions_mut().insert(original_uri);
}
}
let path = req.uri().path().to_owned();
match self.node.at(&path) {
Ok(match_) => {
match &self.fallback {
FallbackRoute::Default(_) => {}
FallbackRoute::Service(fallback) => {
req.extensions_mut()
.insert(SuperFallback(SyncWrapper::new(fallback.clone())));
}
}
self.call_route(match_, req)
}
Err(
MatchError::NotFound
| MatchError::ExtraTrailingSlash
| MatchError::MissingTrailingSlash,
) => match &mut self.fallback {
FallbackRoute::Default(fallback) => {
if let Some(super_fallback) = req.extensions_mut().remove::<SuperFallback<B>>()
{
let mut super_fallback = super_fallback.0.into_inner();
super_fallback.call(req)
} else {
fallback.call(req)
}
}
FallbackRoute::Service(fallback) => fallback.call(req),
},
}
}
}
struct SuperFallback<B>(SyncWrapper<Route<B>>);