Skip to content

Commit 3cb129c

Browse files
authored
Merge pull request #24 from mikkelhegn/default_index
Support for index.html on root
2 parents ae9d2f6 + a3938ba commit 3cb129c

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed
File renamed without changes.

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
3+
<body>
4+
This is index in root
5+
</body>
6+
7+
</html>

spin.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ trigger = {type = "http", base = "/" }
99
[[component]]
1010
source = "target/wasm32-wasi/release/spin_static_fs.wasm"
1111
id = "fs"
12-
files = ["content/**/*"]
12+
files = [{ source = "", destination = "/" }]
1313
[component.trigger]
1414
route = "/..."
15+
[component.build]
16+
command = "make"

src/lib.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ fn serve(req: Request) -> Result<Response> {
7272
.map(|h| h.to_str())
7373
.unwrap_or(Ok(""))?;
7474

75+
let path = match path {
76+
"/" => "index.html",
77+
_ => path,
78+
};
79+
7580
let body = match FileServer::read(path, &enc) {
7681
Ok(b) => Some(b),
7782
Err(e) => {
@@ -200,10 +205,7 @@ mod tests {
200205
let req = spin_http::Request {
201206
method: spin_http::Method::Get,
202207
uri: "http://thisistest.com".to_string(),
203-
headers: vec![(
204-
PATH_INFO_HEADER.to_string(),
205-
"./content/hello-test.txt".to_string(),
206-
)],
208+
headers: vec![(PATH_INFO_HEADER.to_string(), "./hello-test.txt".to_string())],
207209
params: vec![],
208210
body: None,
209211
};
@@ -217,10 +219,7 @@ mod tests {
217219
method: spin_http::Method::Get,
218220
uri: "http://thisistest.com".to_string(),
219221
headers: vec![
220-
(
221-
PATH_INFO_HEADER.to_string(),
222-
"./content/hello-test.txt".to_string(),
223-
),
222+
(PATH_INFO_HEADER.to_string(), "./hello-test.txt".to_string()),
224223
(
225224
IF_NONE_MATCH.to_string(),
226225
"13946318585003701156".to_string(),
@@ -239,10 +238,7 @@ mod tests {
239238
method: spin_http::Method::Get,
240239
uri: "http://thisistest.com".to_string(),
241240
headers: vec![
242-
(
243-
PATH_INFO_HEADER.to_string(),
244-
"./content/hello-test.txt".to_string(),
245-
),
241+
(PATH_INFO_HEADER.to_string(), "./hello-test.txt".to_string()),
246242
(IF_NONE_MATCH.to_string(), "".to_string()),
247243
],
248244
params: vec![],
@@ -267,4 +263,17 @@ mod tests {
267263
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
268264
assert_eq!(rsp.status, 404);
269265
}
266+
267+
#[test]
268+
fn test_serve_index() {
269+
let req = spin_http::Request {
270+
method: spin_http::Method::Get,
271+
uri: "http://thisistest.com".to_string(),
272+
headers: vec![(PATH_INFO_HEADER.to_string(), "/".to_string())],
273+
params: vec![],
274+
body: None,
275+
};
276+
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
277+
assert_eq!(rsp.status, 200);
278+
}
270279
}

0 commit comments

Comments
 (0)