Add directory listing to server.go

Change-Id: I33a29bec13c559c01c9fa23266376285ee317029
This commit is contained in:
Dmitrii Filippov
2020-02-13 12:07:34 +01:00
parent 356fa8f399
commit b01945be9f

View File

@@ -90,7 +90,13 @@ func main() {
} }
redirects := readRedirects() redirects := readRedirects()
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { handleSrcRequest(redirects, w, req) })
dirListingMux := http.NewServeMux()
dirListingMux.Handle("/styles/", http.StripPrefix("/styles/", http.FileServer(http.Dir("app/styles"))))
dirListingMux.Handle("/elements/", http.StripPrefix("/elements/", http.FileServer(http.Dir("app/elements"))))
dirListingMux.Handle("/behaviors/", http.StripPrefix("/behaviors/", http.FileServer(http.Dir("app/behaviors"))))
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { handleSrcRequest(redirects, dirListingMux, w, req) })
http.Handle("/fonts/", http.Handle("/fonts/",
addDevHeadersMiddleware(http.FileServer(httpfs.New(zipfs.New(fontsArchive, "fonts"))))) addDevHeadersMiddleware(http.FileServer(httpfs.New(zipfs.New(fontsArchive, "fonts")))))
@@ -167,12 +173,16 @@ func getFinalPath(redirects []redirects, originalPath string) string {
return originalPath return originalPath
} }
func handleSrcRequest(redirects []redirects, writer http.ResponseWriter, originalRequest *http.Request) { func handleSrcRequest(redirects []redirects, dirListingMux *http.ServeMux, writer http.ResponseWriter, originalRequest *http.Request) {
parsedUrl, err := url.Parse(originalRequest.RequestURI) parsedUrl, err := url.Parse(originalRequest.RequestURI)
if err != nil { if err != nil {
writer.WriteHeader(500) writer.WriteHeader(500)
return return
} }
if (parsedUrl.Path != "/" && strings.HasSuffix(parsedUrl.Path, "/")) {
dirListingMux.ServeHTTP(writer, originalRequest)
return;
}
if parsedUrl.Path == "/bower_components/web-component-tester/browser.js" { if parsedUrl.Path == "/bower_components/web-component-tester/browser.js" {
http.Redirect(writer, originalRequest, "/bower_components/wct-browser-legacy/browser.js", 301); http.Redirect(writer, originalRequest, "/bower_components/wct-browser-legacy/browser.js", 301);
return return
@@ -204,17 +214,17 @@ func handleSrcRequest(redirects []redirects, writer http.ResponseWriter, origina
} }
func readFile(originalPath string, redirectedPath string) ([]byte, error) { func readFile(originalPath string, redirectedPath string) ([]byte, error) {
pathsToTry := [] string { "app" + redirectedPath } pathsToTry := [] string{"app" + redirectedPath}
bowerComponentsSuffix := "/bower_components/" bowerComponentsSuffix := "/bower_components/"
nodeModulesPrefix := "/node_modules/" nodeModulesPrefix := "/node_modules/"
if strings.HasPrefix(originalPath, bowerComponentsSuffix) { if strings.HasPrefix(originalPath, bowerComponentsSuffix) {
pathsToTry = append(pathsToTry, "node_modules/wct-browser-legacy/node_modules/" + originalPath[len(bowerComponentsSuffix):]) pathsToTry = append(pathsToTry, "node_modules/wct-browser-legacy/node_modules/"+originalPath[len(bowerComponentsSuffix):])
pathsToTry = append(pathsToTry, "node_modules/" + originalPath[len(bowerComponentsSuffix):]) pathsToTry = append(pathsToTry, "node_modules/"+originalPath[len(bowerComponentsSuffix):])
} }
if strings.HasPrefix(originalPath, nodeModulesPrefix) { if strings.HasPrefix(originalPath, nodeModulesPrefix) {
pathsToTry = append(pathsToTry, "node_modules/" + originalPath[len(nodeModulesPrefix):]) pathsToTry = append(pathsToTry, "node_modules/"+originalPath[len(nodeModulesPrefix):])
} }
for _, path := range pathsToTry { for _, path := range pathsToTry {