@cryptotaxi247 / kubo / commits / 7ad16db02

feat: add a test server

fixes #30

Steven Allen committed May 4, 2020 at 11:19 UTC 7ad16db02b93acac309d6854927687d841578d52
3 files changed +91
README.md
+12
@@ -17,6 +17,18 @@ This repo is not be used standalone. It's used by the gateway code in go-ipfs. I
17 1. Make changes to _both_ dir-index.html and dir-index-uncat.html.
18 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.
19
20 +## Testing
21 +
22 +1. Install [go](https://golang.org/dl/).
23 +2. Run the test server:
24 +
25 +```bash
26 +> cd test
27 +> go run .
28 +```
29 +
30 +This will listen on `localhost:3000` and re-load the template every time you refresh the page.
31 +
32 ## Contribute
33
34 Feel free to join in. All welcome. A good place to start is [the issues](https://github.com/ipfs/dir-index-html/issues).
test/go.mod new
+3
@@ -0,0 +1,3 @@
1 +module github.com/ipfs/dir-index-html/test
2 +
3 +go 1.14
test/main.go new
+76
@@ -0,0 +1,76 @@
1 +package main
2 +
3 +import (
4 + "fmt"
5 + "net/http"
6 + "net/url"
7 + "os"
8 + "text/template"
9 +)
10 +
11 +const templateFile = "../dir-index.html"
12 +
13 +// Copied from go-ipfs/core/corehttp/gateway_indexPage.go
14 +type listingTemplateData struct {
15 + Listing []directoryItem
16 + Path string
17 + BackLink string
18 + Hash string
19 +}
20 +
21 +type directoryItem struct {
22 + Size string
23 + Name string
24 + Path string
25 +}
26 +
27 +var testData = listingTemplateData{
28 + Listing: []directoryItem{{
29 + Size: "25 MiB",
30 + Name: "short-film.mov",
31 + Path: "short-film.mov",
32 + }, {
33 + Size: "1 KiB",
34 + Name: "description.txt",
35 + Path: "description.txt",
36 + }},
37 + Path: "/ipfs/QmFoo",
38 + BackLink: "/..",
39 + Hash: "QmFoo",
40 +}
41 +
42 +func main() {
43 + mux := http.NewServeMux()
44 + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
45 + if r.URL.Path != "/" {
46 + http.Error(w, "Ha-ha, tricked you! There are no files here!", http.StatusNotFound)
47 + return
48 + }
49 + listingTemplate, err := template.New("dir-index.html").Funcs(template.FuncMap{
50 + "iconFromExt": func(name string) string {
51 + return "ipfs-_blank" // place-holder
52 + },
53 + "urlEscape": func(rawUrl string) string {
54 + pathUrl := url.URL{Path: rawUrl}
55 + return pathUrl.String()
56 + },
57 + }).ParseFiles(templateFile)
58 + if err != nil {
59 + http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
60 + return
61 + }
62 + err = listingTemplate.Execute(w, &testData)
63 + if err != nil {
64 + http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
65 + return
66 + }
67 + w.WriteHeader(http.StatusOK)
68 + })
69 + if _, err := os.Stat(templateFile); err != nil {
70 + wd, _ := os.Getwd()
71 + fmt.Printf("could not open template file %q, relative to %q: %s\n", templateFile, wd, err)
72 + os.Exit(1)
73 + }
74 + fmt.Printf("listening on localhost:3000\n")
75 + http.ListenAndServe("localhost:3000", mux)
76 +}