diff options
author | Steve <nilslice@gmail.com> | 2017-01-30 10:22:20 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-30 10:22:20 -0800 |
commit | d8b1975b036e800d85fd2808458f51902b27ad5a (patch) | |
tree | 779fcf2bd1eaf1ff914c3acbdcd21c218e579fe0 /system/api/external.go | |
parent | 16a159acec94fd391e840fab061ed08cf894369f (diff) |
[core] Return data from api.Externalable requests for client to use in app (#55)
* add json response to externalable requests so client can use data in apps
* bump version
Diffstat (limited to 'system/api/external.go')
-rw-r--r-- | system/api/external.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/system/api/external.go b/system/api/external.go index 168575a..78f6578 100644 --- a/system/api/external.go +++ b/system/api/external.go @@ -2,6 +2,7 @@ package api import ( "context" + "encoding/json" "fmt" "log" "net/http" @@ -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 } @@ -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 + } + } |