-
Notifications
You must be signed in to change notification settings - Fork 605
/
middleware.rs
102 lines (82 loc) · 2.77 KB
/
middleware.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
mod prelude {
pub use conduit::{Handler, Request, Response};
pub use conduit_middleware::{AroundMiddleware, Middleware};
pub use std::error::Error;
}
pub use self::app::AppMiddleware;
pub use self::current_user::CurrentUser;
pub use self::debug::*;
pub use self::ember_index_rewrite::EmberIndexRewrite;
pub use self::head::Head;
use self::log_connection_pool_status::LogConnectionPoolStatus;
pub use self::security_headers::SecurityHeaders;
pub use self::static_or_continue::StaticOrContinue;
pub mod app;
mod block_traffic;
pub mod current_user;
mod debug;
mod ember_index_rewrite;
mod ensure_well_formed_500;
mod head;
mod log_connection_pool_status;
mod log_request;
mod require_user_agent;
mod security_headers;
mod static_or_continue;
use conduit_conditional_get::ConditionalGet;
use conduit_cookie::{Middleware as Cookie, SessionMiddleware};
use conduit_middleware::MiddlewareBuilder;
use std::env;
use std::sync::Arc;
use crate::router::R404;
use crate::{App, Env};
pub fn build_middleware(app: Arc<App>, endpoints: R404) -> MiddlewareBuilder {
let mut m = MiddlewareBuilder::new(endpoints);
let config = app.config.clone();
let env = config.env;
if env != Env::Test {
m.add(ensure_well_formed_500::EnsureWellFormed500);
}
if env == Env::Development {
// Print a log for each request.
m.add(Debug);
// Locally serve crates and readmes
m.around(StaticOrContinue::new("local_uploads"));
}
if env::var_os("DEBUG_REQUESTS").is_some() {
m.add(DebugRequest);
}
if env::var_os("LOG_CONNECTION_POOL_STATUS").is_some() {
m.add(LogConnectionPoolStatus::new(&app));
}
m.add(ConditionalGet);
m.add(Cookie::new());
m.add(SessionMiddleware::new(
"cargo_session",
cookie::Key::from_master(app.session_key.as_bytes()),
env == Env::Production,
));
if env == Env::Production {
m.add(SecurityHeaders::new(&config.uploader));
}
m.add(AppMiddleware::new(app));
// Sets the current user on each request.
m.add(CurrentUser);
// Serve the static files in the *dist* directory, which are the frontend assets.
// Not needed for the backend tests.
if env != Env::Test {
m.around(StaticOrContinue::new("dist"));
m.around(EmberIndexRewrite::default());
m.around(StaticOrContinue::new("dist"));
// Note: around middleware is run from bottom to top, so the rewrite occurs first
}
m.around(Head::default());
for (header, blocked_values) in config.blocked_traffic {
m.around(block_traffic::BlockTraffic::new(header, blocked_values));
}
m.around(require_user_agent::RequireUserAgent::default());
if env != Env::Test {
m.around(log_request::LogRequests::default());
}
m
}