Skip to content

Commit 7514690

Browse files
update dependencies
1 parent 5eeb8d5 commit 7514690

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

Cargo.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1616

1717
wasm-bindgen-cli-support = "0.2"
1818

19-
axum = { version = "0.5", default-features = false, features = ["http1", "headers"] }
20-
axum-server = { version = "0.4", features = ["tls-rustls"] }
21-
axum-server-dual-protocol = "0.2"
19+
axum = { version = "0.6", default-features = false, features = ["http1", "headers"] }
20+
axum-server = { version = "0.5", features = ["tls-rustls"] }
21+
axum-server-dual-protocol = "0.5"
2222
tokio = { version = "1.11", default-features = false, features = ["rt-multi-thread"] }
23-
tower-http = { version = "0.3", features = ["compression-full", "fs", "set-header", "trace"] }
23+
tower-http = { version = "0.4", features = ["compression-full", "fs", "set-header", "trace"] }
2424
tower = "0.4"
25-
fastrand = "1.5"
26-
brotli = { version = "3", default-features = false, features = ["std"]}
27-
rcgen = { version = "0.9", default-features = false }
25+
fastrand = "2.0"
26+
rcgen = { version = "0.11", default-features = false }

src/server.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::collections::HashMap;
22
use std::net::SocketAddr;
33

4+
use axum::error_handling::HandleError;
5+
use axum::extract::Path;
46
use axum::headers::HeaderName;
5-
use axum::http::{HeaderValue, StatusCode, Uri};
7+
use axum::http::{HeaderValue, StatusCode};
68
use axum::response::{Html, IntoResponse, Response};
79
use axum::routing::{get, get_service};
810
use 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

111111
fn 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

Comments
 (0)