diff --git a/gateway/assets/test/main.go b/gateway/assets/test/main.go index d393c2a2d..3b955745b 100644 --- a/gateway/assets/test/main.go +++ b/gateway/assets/test/main.go @@ -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" ) @@ -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 := `

Test paths: DAG, Directory.` + html := `

Test paths: DAG, Directory, Error.` _, _ = 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) }