Unverified Commit 2d4c67ee authored by Jessica Schilling's avatar Jessica Schilling Committed by GitHub

Merge pull request #31 from ipfs/feat/test-server

feat: add a test server
parents b08a78ab 4624f092
......@@ -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).
......
module github.com/ipfs/dir-index-html/test
go 1.14
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)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment