diff options
author | Steve <nilslice@gmail.com> | 2016-12-14 10:04:42 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-14 10:04:42 -0800 |
commit | 372bd05522a7730891778082c0e0875d47445d59 (patch) | |
tree | 8775b01a283c32dce3297dbddad1ddf14a114c33 /system/api/handlers.go | |
parent | 8e1269385b2ea6bb8a115030a4a6f4c12fa24868 (diff) | |
parent | b3aa9440f62db5a530397c525a42b4fca7b27bab (diff) |
Merge pull request #22 from bosssauce/ponzu-dev
[core] Add a index for content slugs allowing quicker lookup via slug
Diffstat (limited to 'system/api/handlers.go')
-rw-r--r-- | system/api/handlers.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/system/api/handlers.go b/system/api/handlers.go index c238ca9..7a2073d 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -91,6 +91,12 @@ func contentHandler(res http.ResponseWriter, req *http.Request) { q := req.URL.Query() id := q.Get("id") t := q.Get("type") + slug := q.Get("slug") + + if slug != "" { + contentHandlerBySlug(res, req) + return + } if _, ok := content.Types[t]; !ok { res.WriteHeader(http.StatusNotFound) @@ -117,6 +123,26 @@ func contentHandler(res http.ResponseWriter, req *http.Request) { sendData(res, j, http.StatusOK) } +func contentHandlerBySlug(res http.ResponseWriter, req *http.Request) { + slug := req.URL.Query().Get("slug") + + // lookup type:id by slug key in __contentIndex + post, err := db.ContentBySlug(slug) + if err != nil { + log.Println("Error finding content by slug:", slug, err) + res.WriteHeader(http.StatusInternalServerError) + return + } + + j, err := fmtJSON(json.RawMessage(post)) + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + + sendData(res, j, http.StatusOK) +} + func fmtJSON(data ...json.RawMessage) ([]byte, error) { var msg = []json.RawMessage{} for _, d := range data { |