From c20a51dfff92d129b3564066b580f631f90e4bd3 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 8 Mar 2016 01:31:49 -0500 Subject: [PATCH] Add public houses endpoint T280 --- app.go | 1 + construction.go | 13 +++++++++++++ models.go | 18 +++++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app.go b/app.go index 9b29c25..7c2783a 100644 --- a/app.go +++ b/app.go @@ -56,6 +56,7 @@ func (app *app) initRouter() { api := app.router.PathPrefix("/⌂/").Subrouter() api.HandleFunc("/create", app.handler(createHouse)).Methods("POST").Name("create") api.HandleFunc("/{house:[A-Za-z0-9.-]{8}}", app.handler(renovateHouse)).Methods("POST").Name("update") + api.HandleFunc("/public", app.handler(getPublicHousesData)).Methods("GET").Name("browse-api") admin := app.router.PathPrefix("/admin/").Subrouter() admin.HandleFunc("/unpublish", app.handler(banHouse)).Methods("POST").Name("unpublish") diff --git a/construction.go b/construction.go index 388c452..fd4d368 100644 --- a/construction.go +++ b/construction.go @@ -198,6 +198,19 @@ func getHouseHTML(app *app, houseID string) (string, error) { return html, nil } +func getPublicHousesData(app *app, w http.ResponseWriter, r *http.Request) error { + houses, err := getPublicHouses(app) + if err != nil { + return err + } + + for i := range *houses { + (*houses)[i].process(app) + } + + return impart.WriteSuccess(w, houses, http.StatusOK) +} + // regular expressions for extracting data var ( htmlReg = regexp.MustCompile("") diff --git a/models.go b/models.go index 2a49fd9..b3d0b5d 100644 --- a/models.go +++ b/models.go @@ -1,10 +1,15 @@ package htmlhouse +import ( + "fmt" +) + type ( PublicHouse struct { - ID string - Title string - ThumbURL string + ID string `json:"id"` + Title string `json:"title"` + URL string `json:"url"` + ThumbURL string `json:"thumb_url"` } HouseStats struct { @@ -17,3 +22,10 @@ type ( Label string } ) + +func (h *PublicHouse) process(app *app) { + h.URL = fmt.Sprintf("%s/%s.html", app.cfg.HostName, h.ID) + if h.ThumbURL != "" { + h.ThumbURL = "https://peeper.html.house/" + h.ThumbURL + } +}