@@ -2,7 +2,6 @@ use std::{convert::Infallible, sync::Arc, time::Duration};
22
33use 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