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()
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) {