Skip to content

Commit 8f5e658

Browse files
committed
update deps + simplify with new https redirect svc
1 parent 9207240 commit 8f5e658

File tree

3 files changed

+59
-65
lines changed

3 files changed

+59
-65
lines changed

Cargo.lock

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

src/main.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::time::Duration;
33

44
use rama::{
55
Layer as _,
6+
combinators::Either,
67
error::{BoxError, ErrorContext as _},
78
graceful::{self, ShutdownGuard},
89
http::{server::HttpServer, tls::CertIssuerHttpClient},
@@ -64,14 +65,11 @@ async fn spawn_service_http(
6465
interface: Interface,
6566
https_enabled: bool,
6667
) -> Result<(), BoxError> {
67-
let svc = AddExtensionLayer::new(Protocol::HTTP).into_layer(
68-
self::service::load_http_service(if https_enabled {
69-
self::service::ServiceMode::Http
70-
} else {
71-
self::service::ServiceMode::HttpOnly
72-
})
73-
.await?,
74-
);
68+
let svc = if https_enabled {
69+
Either::A(self::service::load_http_service().await?)
70+
} else {
71+
Either::B(self::service::load_https_service().await?)
72+
};
7573

7674
let http_server = HttpServer::auto(Executor::graceful(guard.clone())).service(svc);
7775
let tcp_server = HaProxyLayer::new().with_peek(true).into_layer(http_server);
@@ -108,7 +106,7 @@ async fn spawn_service_https(guard: ShutdownGuard, interface: Interface) -> Resu
108106
TlsAcceptorData::try_from(tls_server_config).context("create acceptor data")?;
109107

110108
let svc = AddExtensionLayer::new(Protocol::HTTPS)
111-
.into_layer(self::service::load_http_service(self::service::ServiceMode::Https).await?);
109+
.into_layer(self::service::load_http_service().await?);
112110

113111
let http_server = HttpServer::auto(executor).service(svc);
114112
let tcp_server = (

src/service/mod.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::{convert::Infallible, sync::Arc, time::Duration};
22

33
use rama::{
44
Layer as _, Service,
5-
combinators::Either3,
65
error::{ErrorContext as _, OpaqueError},
76
http::{
87
Body, HeaderName, HeaderValue, Request, Response,
@@ -12,29 +11,16 @@ use rama::{
1211
required_header::AddRequiredResponseHeadersLayer, set_header::SetResponseHeaderLayer,
1312
trace::TraceLayer,
1413
},
15-
service::{fs::DirectoryServeMode, web::Router},
14+
service::{fs::DirectoryServeMode, redirect::RedirectHttpToHttps, web::Router},
1615
},
1716
net::http::uri::UriMatchReplaceRule,
1817
utils::include_dir::include_dir,
1918
};
2019

21-
#[derive(Debug, Clone, Copy)]
22-
pub enum ServiceMode {
23-
Http,
24-
HttpOnly,
25-
Https,
26-
}
27-
28-
pub async fn load_http_service(
29-
mode: ServiceMode,
30-
) -> Result<impl Service<Request, Response = Response, Error = Infallible>, OpaqueError> {
31-
let app = Router::new().dir_embed_with_serve_mode(
32-
"/",
33-
include_dir!("$CARGO_MANIFEST_DIR/src/service/legacy"),
34-
DirectoryServeMode::AppendIndexHtml,
35-
);
36-
37-
Ok((
20+
fn apply_common_middleware(
21+
service: impl Service<Request, Response = Response, Error = Infallible>,
22+
) -> impl Service<Request, Response = Response, Error = Infallible> {
23+
(
3824
MapResponseBodyLayer::new(Body::new),
3925
TraceLayer::new_for_http(),
4026
SetResponseHeaderLayer::if_not_present_typed(
@@ -46,21 +32,31 @@ pub async fn load_http_service(
4632
HeaderValue::from_static("fly.io"),
4733
),
4834
cors::CorsLayer::permissive(),
49-
UriMatchRedirectLayer::permanent(Arc::new(match mode {
50-
ServiceMode::Https => Either3::A(
51-
UriMatchReplaceRule::try_new("https://www.*", "https://$1")
52-
.context("create www to APEX redirect rule")?,
53-
),
54-
ServiceMode::Http => Either3::B([
55-
UriMatchReplaceRule::try_new("http://www.*", "https://$1")
56-
.context("create www to APEX + https upgrade redirect rule")?,
57-
UriMatchReplaceRule::http_to_https(),
58-
]),
59-
ServiceMode::HttpOnly => Either3::C(
60-
UriMatchReplaceRule::try_new("http://www.*", "http://$1")
61-
.context("create www to APEX redirect rule")?,
62-
),
63-
})),
6435
)
65-
.into_layer(app))
36+
.into_layer(service)
37+
}
38+
39+
pub async fn load_http_service()
40+
-> Result<impl Service<Request, Response = Response, Error = Infallible>, OpaqueError> {
41+
let app = RedirectHttpToHttps::new().with_match_replace_uri_rule(
42+
UriMatchReplaceRule::try_new("http://www.*", "https://www.$1")
43+
.context("create APEX to root uri replace rule")?,
44+
);
45+
Ok(apply_common_middleware(app))
46+
}
47+
48+
pub async fn load_https_service()
49+
-> Result<impl Service<Request, Response = Response, Error = Infallible>, OpaqueError> {
50+
let app = Router::new().dir_embed_with_serve_mode(
51+
"/",
52+
include_dir!("$CARGO_MANIFEST_DIR/src/service/legacy"),
53+
DirectoryServeMode::AppendIndexHtml,
54+
);
55+
56+
let middlewares = UriMatchRedirectLayer::permanent(Arc::new(
57+
UriMatchReplaceRule::try_new("https://www.*", "https://$1")
58+
.context("create www to APEX redirect rule")?,
59+
));
60+
61+
Ok(apply_common_middleware(middlewares.into_layer(app)))
6662
}

0 commit comments

Comments
 (0)