Last active
April 12, 2018 11:49
-
-
Save frozzare/8918dc0f60acaed688c69d23b1741328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// reactWrapper fix issues with dynamic routes created | |
// in `react-router` so we don't get a 404 page. | |
// | |
// usage: http.Handle("/", reactWrapper(http.FileServer("path/to/public"))) | |
func reactWrapper(fs http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | |
staticIndex := strings.Index(req.URL.Path, "/static/") | |
if staticIndex == -1 { | |
fsHandler := http.StripPrefix(req.URL.Path, fs) | |
fsHandler.ServeHTTP(w, req) | |
} else { | |
fs.ServeHTTP(w, req) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment