Skip to content

Commit 3401478

Browse files
authoredSep 21, 2023
fix SPA handler in README.md (#733)
<!-- For Work In Progress Pull Requests, please use the Draft PR feature, see https://github.blog/2019-02-14-introducing-draft-pull-requests/ for further details. For a timely review/response, please avoid force-pushing additional commits if your PR already received reviews or comments. Before submitting a Pull Request, please ensure that you have: - 📖 Read the Contributing guide: https://github.com/gorilla/.github/blob/main/CONTRIBUTING.md - 📖 Read the Code of Conduct: https://github.com/gorilla/.github/blob/main/CODE_OF_CONDUCT.md - Provide tests for your changes. - Use descriptive commit messages. - Comment your code where appropriate. - Squash your commits - Update any related documentation. - Add gorilla/pull-request-reviewers as a Reviewer --> ## What type of PR is this? (check all applicable) - [ ] Refactor - [ ] Feature - [ ] Bug Fix - [ ] Optimization - [X] Documentation Update - [ ] Go Version Update - [ ] Dependency Update ## Description Changed the SPA handler example in README.md in two areas. First, made sure to actually include the requested path in the call to `filepath.Join`. Secondly, if the requested path hits a directory, I think it would be beneficial to also serve the `indexPath` file, and not list the directory contents. I also edited the comments in the `README.md` file accordingly. ## Related Tickets & Documents <!-- For pull requests that relate or close an issue, please include them below. We like to follow [Github's guidance on linking issues to pull requests](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue). For example having the text: "closes #1234" would connect the current pull request to issue 1234. And when we merge the pull request, Github will automatically close the issue. --> - Related Issue # - Closes # ## Added/updated tests? - [ ] Yes - [X] No, and this is why: I only changed the `README.md`, if any tests are necessary please let me know - [ ] I need help with writing tests ## Run verifications and test - [ ] `make verify` is passing - [ ] `make test` is passing
1 parent 4a671cb commit 3401478

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed
 

‎README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,25 @@ type spaHandler struct {
247247
// file located at the index path on the SPA handler will be served. This
248248
// is suitable behavior for serving an SPA (single page application).
249249
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
250-
// Join internally call path.Clean to prevent directory traversal
251-
path := filepath.Join(h.staticPath, path)
250+
// Join internally call path.Clean to prevent directory traversal
251+
path := filepath.Join(h.staticPath, r.URL.Path)
252252

253-
// check whether a file exists at the given path
254-
_, err := os.Stat(path)
255-
if os.IsNotExist(err) {
256-
// file does not exist, serve index.html
253+
// check whether a file exists or is a directory at the given path
254+
fi, err := os.Stat(path)
255+
if os.IsNotExist(err) || fi.IsDir() {
256+
// file does not exist or path is a directory, serve index.html
257257
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
258258
return
259-
} else if err != nil {
260-
// if we got an error (that wasn't that the file doesn't exist) stating the
261-
// file, return a 500 internal server error and stop
259+
}
260+
261+
if err != nil {
262+
// if we got an error (that wasn't that the file doesn't exist) stating the
263+
// file, return a 500 internal server error and stop
262264
http.Error(w, err.Error(), http.StatusInternalServerError)
263-
return
265+
return
264266
}
265267

266-
// otherwise, use http.FileServer to serve the static dir
268+
// otherwise, use http.FileServer to serve the static file
267269
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
268270
}
269271

0 commit comments

Comments
 (0)
Please sign in to comment.