This commit is contained in:
Henrique Dias 2016-08-11 22:26:41 +01:00
parent d9c62e2428
commit 1f660b7ecf

View File

@ -1,6 +1,7 @@
package directory package directory
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -225,6 +226,16 @@ func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Co
listing.ItemsLimitedTo = limit 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{ page := &p.Page{
Info: &p.Info{ Info: &p.Info{
Name: listing.Name, Name: listing.Name,
@ -239,10 +250,6 @@ func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Co
page.Minimal = true page.Minimal = true
} }
if strings.Contains(r.Header.Get("Accept"), "application/json") {
return page.PrintAsJSON(w)
}
return page.PrintAsHTML(w, "listing") return page.PrintAsHTML(w, "listing")
} }