Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[question] Gorilla/Mux SPA not working #588

Closed
AstroNik opened this issue Aug 29, 2020 · 7 comments · Fixed by #678
Closed

[question] Gorilla/Mux SPA not working #588

AstroNik opened this issue Aug 29, 2020 · 7 comments · Fixed by #678
Labels

Comments

@AstroNik
Copy link

AstroNik commented Aug 29, 2020

Problem

So I'm not really sure if this is a bug or anything, and this is also my first bug report so I apologize for my lack of knowledge on correctly reporting a bug.

I was trying to create a SPA using gorilla/mux and kept running into issues with displaying my webpage. I knew my code was correct but it had something to do with the file paths inside the SPA handler itself because when I was running my code I was getting a StatusInternalServerError. All my file paths were correct so it had to be something with the ServeHTTP function itself. I later found it to be an issue with the path as shown below.

Versions

go version go1.13.4 windows/amd64

github.com/gorilla/mux v1.7.4

main.go

type spaHandler struct {
	staticPath string
	indexPath  string
}

func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	path, err := filepath.Abs(r.URL.Path)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	path = filepath.Join(h.staticPath, path) //this is the part of the code I changed to fix my issue

	_, err = os.Stat(path)
	if os.IsNotExist(err) {
		http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
		return
	} else if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}

func main() {
	router := mux.NewRouter()
	spa := spaHandler{staticPath: "./admin/build", indexPath: "index.html"}
	router.PathPrefix("/").Handler(spa)
	log.Fatal(http.ListenAndServe(":8080", router))
}

Code was changed to below, and started to work perfectly fine.

path = filepath.Join(h.staticPath, r.URL.Path) 

I don't know whether or not to report this as a fix to the actual public code or not. Still new to contributing.

@AstroNik AstroNik changed the title [question] Gorilla/Mux SPA not working Aug 29, 2020
@AstroNik AstroNik changed the title Gorilla/Mux SPA not working [question] Gorilla/Mux SPA not working Sep 1, 2020
@DJviolin
Copy link

DJviolin commented Nov 10, 2020

Confirming I have the same problem with the example SPA code (using Windows).

	// get the absolute path to prevent directory traversal
	path, err := filepath.Abs(r.URL.Path)
	if err != nil {
		// if we failed to get the absolute path respond with a 400 bad request
		// and stop
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	fmt.Println(path) // returns: D:\

	// prepend the path with the path to the static directory

	path = filepath.Join(h.StaticPath, path)
	fmt.Println(path) // returns: build\D:

	path = filepath.Join(h.StaticPath, r.URL.Path)
	fmt.Println(path) // returns: build

@stale
Copy link

stale bot commented Jun 9, 2021

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@stale stale bot added stale and removed stale labels Jun 9, 2021
@DJviolin
Copy link

DJviolin commented Jun 9, 2021

Not so stale.

@LucaFilitz
Copy link

is there an update on this?

@thibaultleblanc
Copy link

This problem appears on windows.
For an unknown reason filepath.abs returns the storage letter, so the path begins with "C:/" (or "D:/", or whatever letter followed by ":/"), just replace "C:/" by "/" and it will works great !

path, err := filepath.Abs(r.URL.Path)
path = strings.Replace(path, `C:\`, "/", 1)

Solution found on reddit :
https://www.reddit.com/r/golang/comments/pvltgk/having_trouble_getting_the_gorillamux_single_page/

I'm beginner in golang (even more with windows stuff) so i don't feel comfortable to make a pull request but at least here is quick and dirty hotfix !

@stale
Copy link

stale bot commented May 1, 2022

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@amustaque97
Copy link
Contributor

I can finally triage this issue. Will work on fixing docs and add a new test case for the same.

@coreydaley coreydaley moved this to 🏗 In progress in Gorilla Web Toolkit Jul 23, 2023
coreydaley added a commit that referenced this issue Aug 16, 2023
Fixes #588

**Summary of Changes**

1. Add test case to validate proposed fix (both negative and positive
test case).
2. Update `README.md` file.

PS: If you want to verify how I'm able to reproduce the issue and tried
my fix. Here is a link of my local PR where I have configured `Github
Action` to execute same test cases on all the platforms i.e. `ubuntu`,
`macos` and `windows`.

PR link: amustaque97#1

> PS: Make sure your PR includes/updates tests! If you need help with
this part, just ask!

---------

Co-authored-by: Corey Daley <cdaley@redhat.com>
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Gorilla Web Toolkit Aug 16, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

5 participants