Skip to content

Commit

Permalink
Support for index.html on root
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Mørk Hegnhøj <[email protected]>
  • Loading branch information
mikkelhegn committed Nov 11, 2022
1 parent 3be1550 commit a4e73b6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
File renamed without changes.
7 changes: 7 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>

<body>
This is index in root
</body>

</html>
4 changes: 3 additions & 1 deletion spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ trigger = {type = "http", base = "/" }
[[component]]
source = "target/wasm32-wasi/release/spin_static_fs.wasm"
id = "fs"
files = ["content/**/*"]
files = [{ source = "", destination = "/" }]
[component.trigger]
route = "/..."
[component.build]
command = "make"
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ fn serve(req: Request) -> Result<Response> {
.map(|h| h.to_str())
.unwrap_or(Ok(""))?;

let path = match path {
"/" => "index.html",
_ => path,
};

let body = match FileServer::read(path, &enc) {
Ok(b) => Some(b),
Err(e) => {
Expand Down Expand Up @@ -267,4 +272,17 @@ mod tests {
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
assert_eq!(rsp.status, 404);
}

#[test]
fn test_serve_index() {
let req = spin_http::Request {
method: spin_http::Method::Get,
uri: "http://thisistest.com".to_string(),
headers: vec![(PATH_INFO_HEADER.to_string(), "/".to_string())],
params: vec![],
body: None,
};
let rsp = <super::SpinHttp as spin_http::SpinHttp>::handle_http_request(req);
assert_eq!(rsp.status, 200);
}
}

0 comments on commit a4e73b6

Please sign in to comment.