diff --git a/directory/file.go b/directory/file.go index 8596b287..c98b2e79 100644 --- a/directory/file.go +++ b/directory/file.go @@ -1,6 +1,7 @@ package directory import ( + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -225,6 +226,16 @@ func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Co listing.ItemsLimitedTo = limit } + if strings.Contains(r.Header.Get("Accept"), "application/json") { + marsh, err := json.Marshal(listing) + if err != nil { + return http.StatusInternalServerError, err + } + + w.Header().Set("Content-Type", "application/json; charset=utf-8") + return w.Write(marsh) + } + page := &p.Page{ Info: &p.Info{ Name: listing.Name, @@ -239,10 +250,6 @@ func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Co page.Minimal = true } - if strings.Contains(r.Header.Get("Accept"), "application/json") { - return page.PrintAsJSON(w) - } - return page.PrintAsHTML(w, "listing") }