diff --git a/README.md b/README.md index 04334b81740921bfb7cb537496151a75e112d169..6abc18295a3dfb159cb7585f4cd697e77ac48bec 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,18 @@ This repo is not be used standalone. It's used by the gateway code in go-ipfs. I 1. Make changes to _both_ dir-index.html and dir-index-uncat.html. 2. Follow the instructions in [go-ipfs](https://github.com/ipfs/go-ipfs/tree/master/assets#updating-dir-index-html) for updating the directory index. +## Testing + +1. Install [go](https://golang.org/dl/). +2. Run the test server: + +```bash +> cd test +> go run . +``` + +This will listen on `localhost:3000` and re-load the template every time you refresh the page. + ## Contribute Feel free to join in. All welcome. A good place to start is [the issues](https://github.com/ipfs/dir-index-html/issues). diff --git a/test/go.mod b/test/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..272b57a1459c0018ad0718c03de3f0812cfb32a9 --- /dev/null +++ b/test/go.mod @@ -0,0 +1,3 @@ +module github.com/ipfs/dir-index-html/test + +go 1.14 diff --git a/test/main.go b/test/main.go new file mode 100644 index 0000000000000000000000000000000000000000..d33fc27fd8c0a62058030f546e9b5c91f8e92d27 --- /dev/null +++ b/test/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "fmt" + "net/http" + "net/url" + "os" + "text/template" +) + +const templateFile = "../dir-index.html" + +// Copied from go-ipfs/core/corehttp/gateway_indexPage.go +type listingTemplateData struct { + Listing []directoryItem + Path string + BackLink string + Hash string +} + +type directoryItem struct { + Size string + Name string + Path string +} + +var testData = listingTemplateData{ + Listing: []directoryItem{{ + Size: "25 MiB", + Name: "short-film.mov", + Path: "short-film.mov", + }, { + Size: "1 KiB", + Name: "this-piece-of-papers-got-47-words-37-sentences-58-words-we-wanna-know.txt", + Path: "this-piece-of-papers-got-47-words-37-sentences-58-words-we-wanna-know.txt", + }}, + Path: "/ipfs/QmFoo", + BackLink: "/..", + Hash: "QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm", +} + +func main() { + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.Error(w, "Ha-ha, tricked you! There are no files here!", http.StatusNotFound) + return + } + listingTemplate, err := template.New("dir-index.html").Funcs(template.FuncMap{ + "iconFromExt": func(name string) string { + return "ipfs-_blank" // place-holder + }, + "urlEscape": func(rawUrl string) string { + pathUrl := url.URL{Path: rawUrl} + return pathUrl.String() + }, + }).ParseFiles(templateFile) + if err != nil { + http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError) + return + } + err = listingTemplate.Execute(w, &testData) + if err != nil { + http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) + }) + if _, err := os.Stat(templateFile); err != nil { + wd, _ := os.Getwd() + fmt.Printf("could not open template file %q, relative to %q: %s\n", templateFile, wd, err) + os.Exit(1) + } + fmt.Printf("listening on localhost:3000\n") + http.ListenAndServe("localhost:3000", mux) +}