Skip to content

Commit f6245df

Browse files
committed
sentry - remove old code & routes
- rename all _axum routes and remove old ones - remove old middlewares - POST /v5/unit-for-slot - refactor to use axum - Cargo - add `headers` feature to axum - middleware - auth - fix error assertion in test
1 parent 00f4671 commit f6245df

File tree

17 files changed

+288
-2187
lines changed

17 files changed

+288
-2187
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sentry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ hex = "0.4"
4141
# CLI
4242
clap = { version = "3", features = ["cargo"] }
4343
# Server
44-
axum = { version = "0.5", features = ["http1", "http2"] }
44+
axum = { version = "0.5", features = ["http1", "http2", "headers"] }
4545
axum-server = { version = "0.4", features = ["tls-rustls"] }
4646
tower = "0.4"
4747
tower-http = { version = "0.3", features = ["cors"] }

sentry/src/application.rs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use std::{
66

77
use axum::{
88
extract::{FromRequest, RequestParts},
9-
http::StatusCode,
9+
http::{Method, StatusCode},
1010
middleware,
1111
routing::get,
1212
Extension, Router,
1313
};
1414
use axum_server::{tls_rustls::RustlsConfig, Handle};
15-
use hyper::{Body, Method, Request, Response};
1615
use once_cell::sync::Lazy;
1716
use redis::{aio::MultiplexedConnection, ConnectionInfo};
1817
use serde::{Deserialize, Deserializer};
@@ -25,19 +24,11 @@ use primitives::{config::Environment, ValidatorId};
2524

2625
use crate::{
2726
db::{CampaignRemaining, DbPool},
28-
middleware::{
29-
auth::{authenticate, Authenticate},
30-
cors::{cors, Cors},
31-
Middleware,
32-
},
27+
middleware::auth::authenticate,
3328
platform::PlatformApi,
34-
response::{map_response_error, ResponseError},
3529
routes::{
36-
get_cfg, get_cfg_axum,
37-
routers::{
38-
analytics_router, analytics_router_axum, campaigns_router, campaigns_router_axum,
39-
channels_router, channels_router_axum,
40-
},
30+
get_cfg,
31+
routers::{analytics_router, campaigns_router, channels_router, units_for_slot_router},
4132
},
4233
};
4334

@@ -94,9 +85,11 @@ where
9485
fn default_port() -> u16 {
9586
DEFAULT_PORT
9687
}
88+
9789
fn default_ip_addr() -> IpAddr {
9890
DEFAULT_IP_ADDR
9991
}
92+
10093
fn default_redis_url() -> ConnectionInfo {
10194
DEFAULT_REDIS_URL.clone()
10295
}
@@ -137,36 +130,7 @@ where
137130
}
138131
}
139132

140-
pub async fn handle_routing(&self, req: Request<Body>) -> Response<Body> {
141-
let headers = match cors(&req) {
142-
Some(Cors::Simple(headers)) => headers,
143-
// if we have a Preflight, just return the response directly
144-
Some(Cors::Preflight(response)) => return response,
145-
None => Default::default(),
146-
};
147-
148-
let req = match Authenticate.call(req, self).await {
149-
Ok(req) => req,
150-
Err(error) => return map_response_error(error),
151-
};
152-
153-
let mut response = match (req.uri().path(), req.method()) {
154-
("/cfg", &Method::GET) => get_cfg(req, self).await,
155-
(route, _) if route.starts_with("/v5/analytics") => analytics_router(req, self).await,
156-
// This is important because it prevents us from doing
157-
// expensive regex matching for routes without /channel
158-
(path, _) if path.starts_with("/v5/channel") => channels_router(req, self).await,
159-
(path, _) if path.starts_with("/v5/campaign") => campaigns_router(req, self).await,
160-
_ => Err(ResponseError::NotFound),
161-
}
162-
.unwrap_or_else(map_response_error);
163-
164-
// extend the headers with the initial headers we have from CORS (if there are some)
165-
response.headers_mut().extend(headers);
166-
response
167-
}
168-
169-
pub async fn axum_routing(&self) -> Router {
133+
pub async fn routing(&self) -> Router {
170134
let cors = CorsLayer::new()
171135
// "GET,HEAD,PUT,PATCH,POST,DELETE"
172136
.allow_methods([
@@ -182,13 +146,14 @@ where
182146
.allow_origin(tower_http::cors::Any);
183147

184148
let router = Router::new()
185-
.nest("/channel", channels_router_axum::<C>())
186-
.nest("/campaign", campaigns_router_axum::<C>())
187-
.nest("/analytics", analytics_router_axum::<C>());
149+
.nest("/channel", channels_router::<C>())
150+
.nest("/campaign", campaigns_router::<C>())
151+
.nest("/analytics", analytics_router::<C>())
152+
.nest("/units-for-slot", units_for_slot_router::<C>());
188153

189154
Router::new()
190155
.nest("/v5", router)
191-
.route("/cfg", get(get_cfg_axum::<C>))
156+
.route("/cfg", get(get_cfg::<C>))
192157
.layer(
193158
// keeps the order from top to bottom!
194159
ServiceBuilder::new()
@@ -208,7 +173,7 @@ impl<C: Locked + 'static> Application<C> {
208173
};
209174

210175
info!(&logger, "Listening on socket address: {}!", socket_addr);
211-
let router = self.axum_routing().await;
176+
let router = self.routing().await;
212177

213178
let handle = Handle::new();
214179

sentry/src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#![deny(clippy::all)]
22
#![deny(rust_2018_idioms)]
33

4-
use adapter::{primitives::AdapterTypes, Adapter};
4+
use std::{env, net::SocketAddr, path::PathBuf};
5+
56
use clap::{crate_version, value_parser, Arg, Command};
67

8+
use slog::info;
9+
10+
use adapter::{primitives::AdapterTypes, Adapter};
711
use primitives::{
812
config::configuration, postgres::POSTGRES_CONFIG, test_util::DUMMY_AUTH,
913
util::logging::new_logger, ValidatorId,
@@ -14,8 +18,6 @@ use sentry::{
1418
platform::PlatformApi,
1519
Application,
1620
};
17-
use slog::info;
18-
use std::{env, net::SocketAddr, path::PathBuf};
1921

2022
#[tokio::main]
2123
async fn main() -> Result<(), Box<dyn std::error::Error>> {

sentry/src/middleware.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,12 @@
11
//! This module contains all the routers' middlewares
22
//!
33
4-
use std::fmt::Debug;
5-
6-
use crate::{response::ResponseError, Application};
7-
use adapter::client::Locked;
8-
use hyper::{Body, Request};
9-
10-
use async_trait::async_trait;
11-
124
#[cfg(test)]
135
pub use test_util::*;
146

157
pub mod auth;
168
pub mod campaign;
179
pub mod channel;
18-
pub mod cors;
19-
20-
#[async_trait]
21-
pub trait Middleware<C: Locked + 'static>: Send + Sync + Debug {
22-
async fn call<'a>(
23-
&self,
24-
request: Request<Body>,
25-
application: &'a Application<C>,
26-
) -> Result<Request<Body>, ResponseError>;
27-
}
28-
29-
#[derive(Debug, Default)]
30-
/// `Chain` allows chaining multiple middleware to be applied on the Request of the application
31-
/// Chained middlewares are applied in the order they were chained
32-
pub struct Chain<C: Locked + 'static>(Vec<Box<dyn Middleware<C>>>);
33-
34-
impl<C: Locked + 'static> Chain<C> {
35-
pub fn new() -> Self {
36-
Chain(vec![])
37-
}
38-
39-
pub fn chain<M: Middleware<C> + 'static>(mut self, middleware: M) -> Self {
40-
self.0.push(Box::new(middleware));
41-
42-
self
43-
}
44-
45-
/// Applies chained middlewares in the order they were chained
46-
pub async fn apply(
47-
&self,
48-
mut request: Request<Body>,
49-
application: &Application<C>,
50-
) -> Result<Request<Body>, ResponseError> {
51-
for middleware in self.0.iter() {
52-
request = middleware.call(request, application).await?;
53-
}
54-
55-
Ok(request)
56-
}
57-
}
5810

5911
#[cfg(test)]
6012
pub mod test_util {

0 commit comments

Comments
 (0)