Skip to content

Commit

Permalink
[core] Return data from api.Externalable requests for client to use i…
Browse files Browse the repository at this point in the history
…n app (#55)

* add json response to externalable requests so client can use data in apps

* bump version
  • Loading branch information
nilslice authored Jan 30, 2017
1 parent 16a159a commit d8b1975
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/ponzu/ponzu.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.8.1"
}
"version": "0.8.2"
}
39 changes: 39 additions & 0 deletions system/api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -162,6 +163,7 @@ func externalContentHandler(res http.ResponseWriter, req *http.Request) {
id, err := db.SetContent(t+spec+":-1", req.PostForm)
if err != nil {
log.Println("[External] error calling SetContent:", err)
res.WriteHeader(http.StatusInternalServerError)
return
}

Expand All @@ -175,4 +177,41 @@ func externalContentHandler(res http.ResponseWriter, req *http.Request) {
return
}

// create JSON response to send data back to client
var data map[string]interface{}
if spec != "" {
spec = strings.TrimPrefix(spec, "__")
data = map[string]interface{}{
"status": spec,
"type": t,
}
} else {
spec = "public"
data = map[string]interface{}{
"id": id,
"status": spec,
"type": t,
}
}

resp := map[string]interface{}{
"data": []map[string]interface{}{
data,
},
}

j, err := json.Marshal(resp)
if err != nil {
log.Println("[External] error marshalling response to JSON:", err)
res.WriteHeader(http.StatusInternalServerError)
return
}

res.Header().Set("Content-Type", "application/json")
_, err = res.Write(j)
if err != nil {
log.Println("[External] error writing response:", err)
return
}

}

0 comments on commit d8b1975

Please sign in to comment.