aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2020-10-27 12:21:51 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2020-10-27 12:21:51 +0100
commitd90eed44990f34a87c286ee21f5579506143040d (patch)
treedcae92b13e27e16c5d264caaaa89f9cd0f83a203
parent214b8725f92c928383fd306f9074caf493ac6e93 (diff)
moved common json-write code into a helper
-rw-r--r--handler.go41
-rw-r--r--reqres.go14
2 files changed, 24 insertions, 31 deletions
diff --git a/handler.go b/handler.go
index e165a8f..33c6979 100644
--- a/handler.go
+++ b/handler.go
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
- "encoding/json"
"net/http"
"github.com/golang/glog"
@@ -103,18 +102,12 @@ func getEntries(ctx context.Context, i *instance, w http.ResponseWriter, r *http
}
// TODO: use the returned root for tree_size santity checking against start?
- w.Header().Set("Content-Type", "application/json")
- data, err := NewGetEntriesResponse(trillianResponse.Leaves)
+ response, err := NewGetEntriesResponse(trillianResponse.Leaves)
if err != nil {
- return http.StatusInternalServerError, fmt.Errorf("failed createing GetEntriesResponse: %v", err)
+ return http.StatusInternalServerError, fmt.Errorf("failed creating GetEntriesResponse: %v", err)
}
- json, err := json.Marshal(&data)
- if err != nil {
- return http.StatusInternalServerError, fmt.Errorf("failed json-encoding GetEntriesResponse: %v", err)
- }
- _, err = w.Write(json)
- if err != nil {
- return http.StatusInternalServerError, fmt.Errorf("failed writing get-entries response: %v", err)
+ if err := WriteJsonResponse(response, w); err != nil {
+ return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
@@ -123,15 +116,8 @@ func getEntries(ctx context.Context, i *instance, w http.ResponseWriter, r *http
func getAnchors(_ context.Context, i *instance, w http.ResponseWriter, _ *http.Request) (int, error) {
glog.Info("in getAnchors")
data := NewGetAnchorsResponse(i.anchorsPool.RawCertificates())
- json, err := json.Marshal(&data)
- if err != nil {
- return http.StatusInternalServerError, fmt.Errorf("failed json-encoding GetAnchorsResponse: %v", err)
- }
-
- w.Header().Set("Content-Type", "application/json")
- _, err = w.Write(json)
- if err != nil {
- return http.StatusInternalServerError, fmt.Errorf("failed writing get-anchors response: %v", err)
+ if err := WriteJsonResponse(data, w); err != nil {
+ return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
@@ -162,21 +148,14 @@ func getProofByHash(ctx context.Context, i *instance, w http.ResponseWriter, r *
}
// TODO: verify that proof is valid?
- w.Header().Set("Content-Type", "application/json")
- data, err := NewGetProofByHashResponse(uint64(request.TreeSize), trillianResponse.Proof[0])
+ response, err := NewGetProofByHashResponse(uint64(request.TreeSize), trillianResponse.Proof[0])
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed creating get-proof-by-hash response: %v", err)
}
- json, err := json.Marshal(data)
- if err != nil {
- return http.StatusInternalServerError, fmt.Errorf("failed json-encoding GetProofByHashResponse: %v", err)
- }
- _, err = w.Write(json)
- if err != nil {
- return http.StatusInternalServerError, fmt.Errorf("failed writing get-entries response: %v", err)
+ if err := WriteJsonResponse(response, w); err != nil {
+ return http.StatusInternalServerError, err
}
-
- return http.StatusOK, nil // TODO
+ return http.StatusOK, nil
}
// getConsistencyProof provides a consistency proof between two STHs
diff --git a/reqres.go b/reqres.go
index d8d88f2..5d4c1bc 100644
--- a/reqres.go
+++ b/reqres.go
@@ -241,3 +241,17 @@ func UnpackJsonPost(r *http.Request, unpack interface{}) error {
}
return nil
}
+
+func WriteJsonResponse(response interface{}, w http.ResponseWriter) error {
+ json, err := json.Marshal(&response)
+ if err != nil {
+ return fmt.Errorf("json-encoding failed: %v", err)
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ _, err = w.Write(json)
+ if err != nil {
+ return fmt.Errorf("failed writing json response: %v", err)
+ }
+ return nil
+}