Skip to content

Commit 30b1cab

Browse files
committed
update deps: mark svc as http(s)
1 parent cfc9749 commit 30b1cab

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
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: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ use rama::{
66
error::{BoxError, ErrorContext as _},
77
graceful::{self, ShutdownGuard},
88
http::{server::HttpServer, tls::CertIssuerHttpClient},
9+
layer::AddExtensionLayer,
910
net::{
11+
Protocol,
1012
socket::Interface,
11-
tls::server::{CacheKind, ServerAuth, ServerCertIssuerData, ServerConfig},
13+
tls::{
14+
ApplicationProtocol,
15+
server::{CacheKind, ServerAuth, ServerCertIssuerData, ServerConfig},
16+
},
1217
},
1318
proxy::haproxy::server::HaProxyLayer,
1419
rt::Executor,
@@ -59,7 +64,14 @@ async fn spawn_service_http(
5964
interface: Interface,
6065
https_enabled: bool,
6166
) -> Result<(), BoxError> {
62-
let svc = self::service::load_https_service(https_enabled).await?;
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+
);
6375

6476
let http_server = HttpServer::auto(Executor::graceful(guard.clone())).service(svc);
6577
let tcp_server = HaProxyLayer::new().with_peek(true).into_layer(http_server);
@@ -81,15 +93,22 @@ async fn spawn_service_https(guard: ShutdownGuard, interface: Interface) -> Resu
8193

8294
issuer.prefetch_certs_in_background(&executor);
8395

84-
let tls_server_config = ServerConfig::new(ServerAuth::CertIssuer(ServerCertIssuerData {
85-
kind: issuer.into(),
86-
cache_kind: CacheKind::default(),
87-
}));
96+
let tls_server_config = ServerConfig {
97+
application_layer_protocol_negotiation: Some(vec![
98+
ApplicationProtocol::HTTP_2,
99+
ApplicationProtocol::HTTP_11,
100+
]),
101+
..ServerConfig::new(ServerAuth::CertIssuer(ServerCertIssuerData {
102+
kind: issuer.into(),
103+
cache_kind: CacheKind::default(),
104+
}))
105+
};
88106

89107
let acceptor_data =
90108
TlsAcceptorData::try_from(tls_server_config).context("create acceptor data")?;
91109

92-
let svc = self::service::load_https_service(true).await?;
110+
let svc = AddExtensionLayer::new(Protocol::HTTPS)
111+
.into_layer(self::service::load_http_service(self::service::ServiceMode::Https).await?);
93112

94113
let http_server = HttpServer::auto(executor).service(svc);
95114
let tcp_server = (

src/service/mod.rs

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

33
use rama::{
44
Layer as _, Service,
5-
combinators::Either,
5+
combinators::Either3,
66
error::{ErrorContext as _, OpaqueError},
77
http::{
88
Body, HeaderName, HeaderValue, Request, Response,
@@ -18,8 +18,15 @@ use rama::{
1818
utils::include_dir::include_dir,
1919
};
2020

21-
pub async fn load_https_service(
22-
https_enabled: bool,
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,
2330
) -> Result<impl Service<Request, Response = Response, Error = Infallible>, OpaqueError> {
2431
let app = Router::new().dir_embed_with_serve_mode(
2532
"/",
@@ -39,17 +46,16 @@ pub async fn load_https_service(
3946
HeaderValue::from_static("fly.io"),
4047
),
4148
cors::CorsLayer::permissive(),
42-
UriMatchRedirectLayer::permanent(Arc::new(if https_enabled {
43-
Either::A([
44-
UriMatchReplaceRule::http_to_https(),
49+
UriMatchRedirectLayer::permanent(Arc::new(match mode {
50+
ServiceMode::Https => Either3::A(
4551
UriMatchReplaceRule::try_new("https://www.*", "https://$1")
4652
.context("create www to APEX redirect rule")?,
47-
])
48-
} else {
49-
Either::B(
53+
),
54+
ServiceMode::Http => Either3::B(UriMatchReplaceRule::http_to_https()),
55+
ServiceMode::HttpOnly => Either3::C(
5056
UriMatchReplaceRule::try_new("http://www.*", "http://$1")
5157
.context("create www to APEX redirect rule")?,
52-
)
58+
),
5359
})),
5460
)
5561
.into_layer(app))

0 commit comments

Comments
 (0)