From b01945be9f00472f97cd21c17ad40b65e68b2ea4 Mon Sep 17 00:00:00 2001 From: Dmitrii Filippov Date: Thu, 13 Feb 2020 12:07:34 +0100 Subject: [PATCH] Add directory listing to server.go Change-Id: I33a29bec13c559c01c9fa23266376285ee317029 --- polygerrit-ui/server.go | 66 ++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/polygerrit-ui/server.go b/polygerrit-ui/server.go index 72380b4a54..7fa6eb23e4 100644 --- a/polygerrit-ui/server.go +++ b/polygerrit-ui/server.go @@ -90,7 +90,13 @@ func main() { } 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/", addDevHeadersMiddleware(http.FileServer(httpfs.New(zipfs.New(fontsArchive, "fonts"))))) @@ -131,10 +137,10 @@ func addDevHeaders(writer http.ResponseWriter) { } func getFinalPath(redirects []redirects, originalPath string) string { - testComponentsPrefix := "/components/"; - if strings.HasPrefix(originalPath, testComponentsPrefix) { - return "/../node_modules/" + originalPath[len(testComponentsPrefix):] - } + testComponentsPrefix := "/components/"; + if strings.HasPrefix(originalPath, testComponentsPrefix) { + return "/../node_modules/" + originalPath[len(testComponentsPrefix):] + } for _, redirect := range redirects { fromDir := redirect.From @@ -167,16 +173,20 @@ func getFinalPath(redirects []redirects, originalPath string) string { 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) if err != nil { writer.WriteHeader(500) return } + if (parsedUrl.Path != "/" && strings.HasSuffix(parsedUrl.Path, "/")) { + dirListingMux.ServeHTTP(writer, originalRequest) + return; + } if parsedUrl.Path == "/bower_components/web-component-tester/browser.js" { - http.Redirect(writer, originalRequest, "/bower_components/wct-browser-legacy/browser.js", 301); - return - } + http.Redirect(writer, originalRequest, "/bower_components/wct-browser-legacy/browser.js", 301); + return + } requestPath := getFinalPath(redirects, parsedUrl.Path) @@ -186,8 +196,8 @@ func handleSrcRequest(redirects []redirects, writer http.ResponseWriter, origina data, err := readFile(parsedUrl.Path, requestPath) if err != nil { - writer.WriteHeader(404) - return + writer.WriteHeader(404) + return } if strings.HasSuffix(requestPath, ".js") { r := regexp.MustCompile("(?m)^(import.*)'([^/.].*)';$") @@ -204,27 +214,27 @@ func handleSrcRequest(redirects []redirects, writer http.ResponseWriter, origina } func readFile(originalPath string, redirectedPath string) ([]byte, error) { - pathsToTry := [] string { "app" + redirectedPath } - bowerComponentsSuffix := "/bower_components/" - nodeModulesPrefix := "/node_modules/" + pathsToTry := [] string{"app" + redirectedPath} + bowerComponentsSuffix := "/bower_components/" + nodeModulesPrefix := "/node_modules/" - if strings.HasPrefix(originalPath, bowerComponentsSuffix) { - pathsToTry = append(pathsToTry, "node_modules/wct-browser-legacy/node_modules/" + originalPath[len(bowerComponentsSuffix):]) - pathsToTry = append(pathsToTry, "node_modules/" + originalPath[len(bowerComponentsSuffix):]) - } + if strings.HasPrefix(originalPath, bowerComponentsSuffix) { + pathsToTry = append(pathsToTry, "node_modules/wct-browser-legacy/node_modules/"+originalPath[len(bowerComponentsSuffix):]) + pathsToTry = append(pathsToTry, "node_modules/"+originalPath[len(bowerComponentsSuffix):]) + } - if strings.HasPrefix(originalPath, nodeModulesPrefix) { - pathsToTry = append(pathsToTry, "node_modules/" + originalPath[len(nodeModulesPrefix):]) - } + if strings.HasPrefix(originalPath, nodeModulesPrefix) { + pathsToTry = append(pathsToTry, "node_modules/"+originalPath[len(nodeModulesPrefix):]) + } - for _, path := range pathsToTry { - data, err := ioutil.ReadFile(path) - if err == nil { - return data, nil - } - } + for _, path := range pathsToTry { + data, err := ioutil.ReadFile(path) + if err == nil { + return data, nil + } + } - return nil, errors.New("File not found") + return nil, errors.New("File not found") } func openDataArchive(path string) (*zip.ReadCloser, error) {