11use std:: collections:: HashMap ;
22use std:: net:: SocketAddr ;
33
4+ use axum:: error_handling:: HandleError ;
5+ use axum:: extract:: Path ;
46use axum:: headers:: HeaderName ;
5- use axum:: http:: { HeaderValue , StatusCode , Uri } ;
7+ use axum:: http:: { HeaderValue , StatusCode } ;
68use axum:: response:: { Html , IntoResponse , Response } ;
79use axum:: routing:: { get, get_service} ;
810use axum:: Router ;
@@ -56,28 +58,26 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
5658 } ;
5759
5860 let serve_dir =
59- get_service ( ServeDir :: new ( options. directory ) ) . handle_error ( internal_server_error) ;
60-
61- let serve_wasm = || async move { WithContentType ( "application/wasm" , wasm) } ;
61+ HandleError :: new ( get_service ( ServeDir :: new ( options. directory ) ) , internal_server_error) ;
6262
6363 let app = Router :: new ( )
6464 . route ( "/" , get ( move || async { Html ( html) } ) )
6565 . route ( "/api/wasm.js" , get ( || async { WithContentType ( "application/javascript" , js) } ) )
66- . route ( "/api/wasm.wasm" , get ( serve_wasm ) )
66+ . route ( "/api/wasm.wasm" , get ( || async { WithContentType ( "application/wasm" , wasm ) } ) )
6767 . route ( "/api/version" , get ( move || async { version } ) )
68- . nest (
69- "/api/snippets" ,
70- get ( |uri : Uri | async move {
71- match get_snippet_source ( & uri , & local_modules, & snippets) {
68+ . route (
69+ "/api/snippets/*rest " ,
70+ get ( |Path ( path ) : Path < String > | async move {
71+ match get_snippet_source ( & path , & local_modules, & snippets) {
7272 Ok ( source) => Ok ( WithContentType ( "application/javascript" , source) ) ,
7373 Err ( e) => {
74- tracing:: error!( "failed to serve snippet `{uri }`: {e}" ) ;
74+ tracing:: error!( "failed to serve snippet `{path }`: {e}" ) ;
7575 Err ( e)
7676 }
7777 }
7878 } ) ,
7979 )
80- . fallback ( serve_dir)
80+ . fallback_service ( serve_dir)
8181 . layer ( middleware_stack) ;
8282
8383 let mut address_string = options. address ;
@@ -109,11 +109,10 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
109109}
110110
111111fn get_snippet_source (
112- uri : & Uri ,
112+ path : & str ,
113113 local_modules : & HashMap < String , String > ,
114114 snippets : & HashMap < String , Vec < String > > ,
115115) -> Result < String , & ' static str > {
116- let path = uri. path ( ) . trim_start_matches ( "/" ) ;
117116 if let Some ( module) = local_modules. get ( path) {
118117 return Ok ( module. clone ( ) ) ;
119118 } ;
@@ -122,9 +121,9 @@ fn get_snippet_source(
122121 let index = inline_snippet_name
123122 . strip_prefix ( "inline" )
124123 . and_then ( |path| path. strip_suffix ( ".js" ) )
125- . ok_or ( "invalid snippet name" ) ?;
124+ . ok_or ( "invalid snippet name in path " ) ?;
126125 let index: usize = index. parse ( ) . map_err ( |_| "invalid index" ) ?;
127- let snippet = snippets. get ( snippet) . unwrap ( ) . get ( index) . ok_or ( "snippet index out of bounds" ) ?;
126+ let snippet = snippets. get ( snippet) . ok_or ( "invalid snippet name" ) ? . get ( index) . ok_or ( "snippet index out of bounds" ) ?;
128127 Ok ( snippet. clone ( ) )
129128}
130129
@@ -137,7 +136,7 @@ impl<T: IntoResponse> IntoResponse for WithContentType<T> {
137136 }
138137}
139138
140- async fn internal_server_error ( error : std:: io :: Error ) -> impl IntoResponse {
139+ async fn internal_server_error ( error : impl std:: fmt :: Display ) -> impl IntoResponse {
141140 ( StatusCode :: INTERNAL_SERVER_ERROR , format ! ( "Unhandled internal error: {}" , error) )
142141}
143142
0 commit comments