Add directory listing to server.go
Change-Id: I33a29bec13c559c01c9fa23266376285ee317029
This commit is contained in:
@@ -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")))))
|
||||||
@@ -131,10 +137,10 @@ func addDevHeaders(writer http.ResponseWriter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getFinalPath(redirects []redirects, originalPath string) string {
|
func getFinalPath(redirects []redirects, originalPath string) string {
|
||||||
testComponentsPrefix := "/components/";
|
testComponentsPrefix := "/components/";
|
||||||
if strings.HasPrefix(originalPath, testComponentsPrefix) {
|
if strings.HasPrefix(originalPath, testComponentsPrefix) {
|
||||||
return "/../node_modules/" + originalPath[len(testComponentsPrefix):]
|
return "/../node_modules/" + originalPath[len(testComponentsPrefix):]
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, redirect := range redirects {
|
for _, redirect := range redirects {
|
||||||
fromDir := redirect.From
|
fromDir := redirect.From
|
||||||
@@ -167,16 +173,20 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
requestPath := getFinalPath(redirects, parsedUrl.Path)
|
requestPath := getFinalPath(redirects, parsedUrl.Path)
|
||||||
|
|
||||||
@@ -186,8 +196,8 @@ func handleSrcRequest(redirects []redirects, writer http.ResponseWriter, origina
|
|||||||
|
|
||||||
data, err := readFile(parsedUrl.Path, requestPath)
|
data, err := readFile(parsedUrl.Path, requestPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writer.WriteHeader(404)
|
writer.WriteHeader(404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(requestPath, ".js") {
|
if strings.HasSuffix(requestPath, ".js") {
|
||||||
r := regexp.MustCompile("(?m)^(import.*)'([^/.].*)';$")
|
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) {
|
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 {
|
||||||
data, err := ioutil.ReadFile(path)
|
data, err := ioutil.ReadFile(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return data, 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) {
|
func openDataArchive(path string) (*zip.ReadCloser, error) {
|
||||||
|
Reference in New Issue
Block a user