Skip to content

Commit 17a7acf

Browse files
authored
Merge branch 'dev' into issue-232-binary-release
2 parents 1ff8cd7 + 6717c73 commit 17a7acf

File tree

5 files changed

+75
-120
lines changed

5 files changed

+75
-120
lines changed

primitives/src/sentry.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ pub struct Earner {
6262
pub promilles: u64,
6363
}
6464

65-
pub type EarnerAddress = ValidatorId;
66-
6765
#[derive(Debug, Serialize, Deserialize)]
6866
#[serde(rename_all = "camelCase")]
6967
pub struct EventAggregate {

sentry/src/chain.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
use crate::{Application, ResponseError};
2+
use futures::future::BoxFuture;
23
use hyper::{Body, Request};
34
use primitives::adapter::Adapter;
4-
use std::future::Future;
55

66
// chain middleware function calls
77
//
88
// function signature
99
// fn middleware(mut req: Request) -> Result<Request, ResponseError>
1010

11-
pub async fn chain<'a, A, M, MF>(
11+
pub async fn chain<'a, A: Adapter + 'static, M>(
1212
req: Request<Body>,
1313
app: &'a Application<A>,
1414
middlewares: Vec<M>,
1515
) -> Result<Request<Body>, ResponseError>
1616
where
17-
A: Adapter,
18-
MF: Future<Output = Result<Request<Body>, ResponseError>> + Send,
19-
M: FnMut(Request<Body>, &'a Application<A>) -> MF,
17+
M: FnMut(
18+
Request<Body>,
19+
&'a Application<A>,
20+
) -> BoxFuture<'a, Result<Request<Body>, ResponseError>>
21+
+ 'static,
2022
{
2123
let mut req = Ok(req);
2224

sentry/src/lib.rs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::middleware::cors::{cors, Cors};
99
use crate::routes::channel::channel_status;
1010
use crate::routes::event_aggregate::list_channel_event_aggregates;
1111
use crate::routes::validator_message::{extract_params, list_validator_messages};
12+
use futures::future::{BoxFuture, FutureExt};
1213
use hyper::{Body, Method, Request, Response, StatusCode};
1314
use lazy_static::lazy_static;
1415
use primitives::adapter::Adapter;
@@ -20,12 +21,10 @@ use routes::cfg::config;
2021
use routes::channel::{channel_list, create_channel, last_approved};
2122
use slog::{error, Logger};
2223
use std::collections::HashMap;
23-
2424
pub mod middleware {
2525
pub mod auth;
2626
pub mod channel;
2727
pub mod cors;
28-
pub mod event_aggregate;
2928
}
3029

3130
pub mod routes {
@@ -48,28 +47,23 @@ lazy_static! {
4847
static ref CHANNEL_STATUS_BY_CHANNEL_ID: Regex = Regex::new(r"^/channel/0x([a-zA-Z0-9]{64})/status/?$").expect("The regex should be valid");
4948
// Only the initial Regex to be matched.
5049
static ref CHANNEL_VALIDATOR_MESSAGES: Regex = Regex::new(r"^/channel/0x([a-zA-Z0-9]{64})/validator-messages(/.*)?$").expect("The regex should be valid");
51-
static ref CHANNEL_EVENTS_AGGREGATES: Regex = Regex::new(r"^/channel/0x([a-zA-Z0-9]{64})/events-aggregates(/|/0x[a-zA-Z0-9]{40}/?)?$").expect("The regex should be valid");
52-
// @TODO define other regex routes
50+
static ref CHANNEL_EVENTS_AGGREGATES: Regex = Regex::new(r"^/channel/0x([a-zA-Z0-9]{64})/events-aggregates/?$").expect("The regex should be valid");
5351
static ref ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
5452
static ref ADVERTISER_ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/for-advertiser/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
5553
}
5654

57-
async fn config_middleware<A: Adapter>(
58-
req: Request<Body>,
59-
_: &Application<A>,
60-
) -> Result<Request<Body>, ResponseError> {
61-
Ok(req)
62-
}
63-
64-
async fn auth_required_middleware<A: Adapter>(
55+
fn auth_required_middleware<'a, A: Adapter>(
6556
req: Request<Body>,
6657
_: &Application<A>,
67-
) -> Result<Request<Body>, ResponseError> {
68-
if req.extensions().get::<Session>().is_some() {
69-
Ok(req)
70-
} else {
71-
Err(ResponseError::Unauthorized)
58+
) -> BoxFuture<'a, Result<Request<Body>, ResponseError>> {
59+
async move {
60+
if req.extensions().get::<Session>().is_some() {
61+
Ok(req)
62+
} else {
63+
Err(ResponseError::Unauthorized)
64+
}
7265
}
66+
.boxed()
7367
}
7468

7569
#[derive(Debug)]
@@ -137,8 +131,7 @@ impl<A: Adapter + 'static> Application<A> {
137131

138132
("/analytics", &Method::GET) => analytics(req, &self).await,
139133
("/analytics/for-advertiser", &Method::GET) => {
140-
// @TODO get advertiser channels
141-
let req = match chain(req, &self, vec![auth_required_middleware]).await {
134+
let req = match chain(req, &self, vec![Box::new(auth_required_middleware)]).await {
142135
Ok(req) => req,
143136
Err(error) => {
144137
return map_response_error(error);
@@ -147,7 +140,7 @@ impl<A: Adapter + 'static> Application<A> {
147140
advertiser_analytics(req, &self).await
148141
}
149142
("/analytics/for-publisher", &Method::GET) => {
150-
let req = match chain(req, &self, vec![auth_required_middleware]).await {
143+
let req = match chain(req, &self, vec![Box::new(auth_required_middleware)]).await {
151144
Ok(req) => req,
152145
Err(error) => {
153146
return map_response_error(error);
@@ -170,7 +163,7 @@ impl<A: Adapter + 'static> Application<A> {
170163
}
171164
}
172165

173-
async fn analytics_router<A: Adapter>(
166+
async fn analytics_router<A: Adapter + 'static>(
174167
mut req: Request<Body>,
175168
app: &Application<A>,
176169
) -> Result<Response<Body>, ResponseError> {
@@ -184,15 +177,15 @@ async fn analytics_router<A: Adapter>(
184177
.map_or("".to_string(), |m| m.as_str().to_string())]);
185178
req.extensions_mut().insert(param);
186179

187-
let req = chain(req, app, vec![channel_load]).await?;
180+
let req = chain(req, app, vec![Box::new(channel_load)]).await?;
188181
analytics(req, app).await
189182
} else if let Some(caps) = ADVERTISER_ANALYTICS_BY_CHANNEL_ID.captures(route) {
190183
let param = RouteParams(vec![caps
191184
.get(1)
192185
.map_or("".to_string(), |m| m.as_str().to_string())]);
193186
req.extensions_mut().insert(param);
194187

195-
let req = chain(req, app, vec![auth_required_middleware]).await?;
188+
let req = auth_required_middleware(req, app).await?;
196189
advertiser_analytics(req, app).await
197190
} else {
198191
Err(ResponseError::NotFound)
@@ -202,7 +195,7 @@ async fn analytics_router<A: Adapter>(
202195
}
203196
}
204197

205-
async fn channels_router<A: Adapter>(
198+
async fn channels_router<A: Adapter + 'static>(
206199
mut req: Request<Body>,
207200
app: &Application<A>,
208201
) -> Result<Response<Body>, ResponseError> {
@@ -217,15 +210,6 @@ async fn channels_router<A: Adapter>(
217210
.map_or("".to_string(), |m| m.as_str().to_string())]);
218211
req.extensions_mut().insert(param);
219212

220-
// example with middleware
221-
// @TODO remove later
222-
let req = match chain(req, app, vec![config_middleware]).await {
223-
Ok(req) => req,
224-
Err(error) => {
225-
return Err(error);
226-
}
227-
};
228-
229213
last_approved(req, app).await
230214
} else if let (Some(caps), &Method::GET) =
231215
(CHANNEL_STATUS_BY_CHANNEL_ID.captures(&path), method)
@@ -235,13 +219,7 @@ async fn channels_router<A: Adapter>(
235219
.map_or("".to_string(), |m| m.as_str().to_string())]);
236220
req.extensions_mut().insert(param);
237221

238-
let req = match chain(req, app, vec![channel_load]).await {
239-
Ok(req) => req,
240-
Err(error) => {
241-
return Err(error);
242-
}
243-
};
244-
222+
let req = channel_load(req, app).await?;
245223
channel_status(req, app).await
246224
} else if let (Some(caps), &Method::GET) = (CHANNEL_VALIDATOR_MESSAGES.captures(&path), method)
247225
{
@@ -251,7 +229,7 @@ async fn channels_router<A: Adapter>(
251229

252230
req.extensions_mut().insert(param);
253231

254-
let req = match chain(req, app, vec![channel_load]).await {
232+
let req = match chain(req, app, vec![Box::new(channel_load)]).await {
255233
Ok(req) => req,
256234
Err(error) => {
257235
return Err(error);
@@ -280,7 +258,7 @@ async fn channels_router<A: Adapter>(
280258
]);
281259
req.extensions_mut().insert(param);
282260

283-
let req = chain(req, app, vec![channel_load]).await?;
261+
let req = chain(req, app, vec![Box::new(channel_load)]).await?;
284262

285263
list_channel_event_aggregates(req, app).await
286264
} else {

sentry/src/middleware/channel.rs

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,68 @@
11
use crate::db::{get_channel_by_id, get_channel_by_id_and_validator};
22
use crate::{Application, ResponseError, RouteParams};
3+
use futures::future::{BoxFuture, FutureExt};
34
use hex::FromHex;
45
use hyper::{Body, Request};
56
use primitives::adapter::Adapter;
67
use primitives::{ChannelId, ValidatorId};
78
use std::convert::TryFrom;
89

910
/// channel_load & channel_if_exist
10-
pub async fn channel_load<A: Adapter>(
11+
pub fn channel_load<'a, A: Adapter + 'static>(
1112
mut req: Request<Body>,
12-
app: &Application<A>,
13-
) -> Result<Request<Body>, ResponseError> {
14-
let id = req
15-
.extensions()
16-
.get::<RouteParams>()
17-
.ok_or_else(|| ResponseError::BadRequest("Route params not found".to_string()))?
18-
.get(0)
19-
.ok_or_else(|| ResponseError::BadRequest("No id".to_string()))?;
20-
21-
let channel_id = ChannelId::from_hex(id)
22-
.map_err(|_| ResponseError::BadRequest("Wrong Channel Id".to_string()))?;
23-
let channel = get_channel_by_id(&app.pool, &channel_id)
24-
.await?
25-
.ok_or_else(|| ResponseError::NotFound)?;
26-
27-
req.extensions_mut().insert(channel);
28-
29-
Ok(req)
13+
app: &'a Application<A>,
14+
) -> BoxFuture<'a, Result<Request<Body>, ResponseError>> {
15+
async move {
16+
let id = req
17+
.extensions()
18+
.get::<RouteParams>()
19+
.ok_or_else(|| ResponseError::BadRequest("Route params not found".to_string()))?
20+
.get(0)
21+
.ok_or_else(|| ResponseError::BadRequest("No id".to_string()))?;
22+
23+
let channel_id = ChannelId::from_hex(id)
24+
.map_err(|_| ResponseError::BadRequest("Wrong Channel Id".to_string()))?;
25+
let channel = get_channel_by_id(&app.pool, &channel_id)
26+
.await?
27+
.ok_or_else(|| ResponseError::NotFound)?;
28+
29+
req.extensions_mut().insert(channel);
30+
31+
Ok(req)
32+
}
33+
.boxed()
3034
}
3135

32-
pub async fn channel_if_active<A: Adapter>(
36+
pub fn channel_if_active<'a, A: Adapter + 'static>(
3337
mut req: Request<Body>,
34-
app: &Application<A>,
35-
) -> Result<Request<Body>, ResponseError> {
36-
let route_params = req
37-
.extensions()
38-
.get::<RouteParams>()
39-
.ok_or_else(|| ResponseError::BadRequest("Route params not found".to_string()))?;
38+
app: &'a Application<A>,
39+
) -> BoxFuture<'a, Result<Request<Body>, ResponseError>> {
40+
async move {
41+
let route_params = req
42+
.extensions()
43+
.get::<RouteParams>()
44+
.ok_or_else(|| ResponseError::BadRequest("Route params not found".to_string()))?;
4045

41-
let id = route_params
42-
.get(0)
43-
.ok_or_else(|| ResponseError::BadRequest("No id".to_string()))?;
46+
let id = route_params
47+
.get(0)
48+
.ok_or_else(|| ResponseError::BadRequest("No id".to_string()))?;
4449

45-
let channel_id = ChannelId::from_hex(id)
46-
.map_err(|_| ResponseError::BadRequest("Wrong Channel Id".to_string()))?;
50+
let channel_id = ChannelId::from_hex(id)
51+
.map_err(|_| ResponseError::BadRequest("Wrong Channel Id".to_string()))?;
4752

48-
let validator_id = route_params
49-
.get(1)
50-
.ok_or_else(|| ResponseError::BadRequest("No Validator Id".to_string()))?;
51-
let validator_id = ValidatorId::try_from(&validator_id)
52-
.map_err(|_| ResponseError::BadRequest("Wrong Validator Id".to_string()))?;
53+
let validator_id = route_params
54+
.get(1)
55+
.ok_or_else(|| ResponseError::BadRequest("No Validator Id".to_string()))?;
56+
let validator_id = ValidatorId::try_from(&validator_id)
57+
.map_err(|_| ResponseError::BadRequest("Wrong Validator Id".to_string()))?;
5358

54-
let channel = get_channel_by_id_and_validator(&app.pool, &channel_id, &validator_id)
55-
.await?
56-
.ok_or_else(|| ResponseError::NotFound)?;
59+
let channel = get_channel_by_id_and_validator(&app.pool, &channel_id, &validator_id)
60+
.await?
61+
.ok_or_else(|| ResponseError::NotFound)?;
5762

58-
req.extensions_mut().insert(channel);
63+
req.extensions_mut().insert(channel);
5964

60-
Ok(req)
65+
Ok(req)
66+
}
67+
.boxed()
6168
}

sentry/src/middleware/event_aggregate.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)