Skip to content

Commit

Permalink
refactor: simplify assets testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed May 12, 2023
1 parent 70dd84f commit 9de6836
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 199 deletions.
18 changes: 7 additions & 11 deletions gateway/assets/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Required Assets for the Gateway

> DAG and Directory HTML for HTTP gateway
> DAG, Directory and Error HTML for HTTP gateway
## Updating

Expand All @@ -11,17 +11,13 @@ When making updates to the templates, please note the following:

## Testing

1. Make sure you have [Go](https://golang.org/dl/) installed
2. Start the test server, which lives in its own directory:
1. Make sure you have [Go](https://golang.org/dl/) installed.
2. From the `assets/` directory, start the test server: `go run test/main.go`.

```bash
> cd test
> go run .
```

This will listen on [`localhost:3000`](http://localhost:3000/) and reload the template every time you refresh the page. Here you have two pages:
This will listen on [`localhost:3000`](http://localhost:3000/) and reload the template every time you refresh the page. Here you have three pages:

- [`localhost:3000/dag`](http://localhost:3000/dag) for the DAG template preview; and
- [`localhost:3000/directory`](http://localhost:3000/directory) for the Directory template preview.
- [`localhost:3000/directory`](http://localhost:3000/directory) for the Directory template preview; and
- [`localhost:3000/error?code=500`](http://localhost:3000/error?status=500) for the Error template preview, you can replace `500` by a different status code.

If you get a "no such file or directory" error upon trying `go run .`, make sure you ran `go generate .` to generate the minified artifact that the test is looking for.
Please make sure you run `go generate .` to generate the minified artifact that the test server is looking for every time you make a change.
12 changes: 9 additions & 3 deletions gateway/assets/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import (
"os"
)

var templates = []string{
"directory-index.html",
"dag-index.html",
"error.html",
}

func main() {
buildTemplate("directory-index.html")
buildTemplate("dag-index.html")
buildTemplate("error.html")
for _, tpl := range templates {
buildTemplate(tpl)
}
}

func loadStyles() []byte {
Expand Down
102 changes: 51 additions & 51 deletions gateway/assets/dag-index.html

Large diffs are not rendered by default.

122 changes: 61 additions & 61 deletions gateway/assets/directory-index.html

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions gateway/assets/error.html

Large diffs are not rendered by default.

81 changes: 37 additions & 44 deletions gateway/assets/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import (
"html/template"
"net/http"
"net/url"
"os"
"strconv"

"github.com/ipfs/boxo/gateway/assets"
)

const (
directoryTemplateFile = "../directory-index.html"
dagTemplateFile = "../dag-index.html"

testPath = "/ipfs/QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7/a/b/c"
)

Expand Down Expand Up @@ -67,60 +64,56 @@ var dagTestData = assets.DagTemplateData{
CodecHex: "0x129",
}

var funcMap = 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()
},
}

func runTemplate(w http.ResponseWriter, filename string, data interface{}) {
tpl, err := template.New(filename).Funcs(funcMap).ParseFiles(filename)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = tpl.Execute(w, data)
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
return
}
}

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/dag":
dagTemplate, err := template.New("dag-index.html").ParseFiles(dagTemplateFile)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = dagTemplate.Execute(w, &dagTestData)
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
return
}
runTemplate(w, "dag-index.html", dagTestData)
case "/directory":
directoryTemplate, err := template.New("directory-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(directoryTemplateFile)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = directoryTemplate.Execute(w, &directoryTestData)
runTemplate(w, "directory-index.html", directoryTestData)
case "/error":
statusCode, err := strconv.Atoi(r.URL.Query().Get("code"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
return
statusCode = 500
}
runTemplate(w, "error.html", &assets.ErrorTemplateData{
Path: "/ipfs/baguqeerabn4wonmz6icnk7dfckuizcsf4e4igua2ohdboecku225xxmujepa",
StatusCode: statusCode,
StatusText: http.StatusText(statusCode),
Error: "this is just an example error",
})
case "/":
html := `<p>Test paths: <a href="/dag">DAG</a>, <a href="/directory">Directory</a>.`
html := `<p>Test paths: <a href="/dag">DAG</a>, <a href="/directory">Directory</a>, <a href="/error?code=500">Error</a>.`
_, _ = w.Write([]byte(html))
default:
http.Redirect(w, r, "/", http.StatusSeeOther)
}
})

if _, err := os.Stat(directoryTemplateFile); err != nil {
wd, _ := os.Getwd()
fmt.Printf("could not open template file %q, relative to %q: %s\n", directoryTemplateFile, wd, err)
os.Exit(1)
}

if _, err := os.Stat(dagTemplateFile); err != nil {
wd, _ := os.Getwd()
fmt.Printf("could not open template file %q, relative to %q: %s\n", dagTemplateFile, wd, err)
os.Exit(1)
}

fmt.Printf("listening on localhost:3000\n")
fmt.Printf("listening on http://localhost:3000/\n")
_ = http.ListenAndServe("localhost:3000", mux)
}

0 comments on commit 9de6836

Please sign in to comment.