Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #41 from ryandeivert/ryandeivert-io-writestring-re…
Browse files Browse the repository at this point in the history
…sponse

[api] using io.WriteString for writing responses instead of http.ResponseWriter.Write(...)

Aww yis yissss!  🎉
  • Loading branch information
mattjane-okta authored Feb 28, 2018
2 parents fb2947e + 586f15d commit 4e0d3c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion handlers/distributed/distributed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
Expand Down Expand Up @@ -62,7 +63,7 @@ func DistributedQueryRead(respWriter http.ResponseWriter, request *http.Request)
return errors.New("no queries in list: %s")
}

respWriter.Write([]byte(distributedQuery.ToJSON()))
io.WriteString(respWriter, distributedQuery.ToJSON())
err = dyndb.DeleteDistributedQuery(distributedQuery, dynSvc)
if err != nil {
return fmt.Errorf("could not delete query: %s", err)
Expand Down
35 changes: 24 additions & 11 deletions handlers/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package response
import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/oktasecuritylabs/sgt/logger"
)

const (
Expand All @@ -22,32 +25,42 @@ type sgtBaseResponse struct {
// storing custom endpoint response values
type SGTCustomResponse map[string]interface{}

func getResponseJSON(response interface{}) []byte {
func writeResponseJSON(respWriter http.ResponseWriter, response interface{}) {

// Set the header type
respWriter.Header().Set("Content-Type", "application/json")

respJSON, err := json.Marshal(response)
if err != nil {
// If there is an error marshaling the interface, return a basic error
// If there is an error marshaling the interface, write a basic error
errString := fmt.Sprintf("response failed to marshal to json: %s", err)
newResp := sgtBaseResponse{Message: errString, Status: errString}
respJSON, _ = json.Marshal(newResp)
logger.Error(errString)
http.Error(respWriter, errString, http.StatusInternalServerError)
return
}

// Write the response to the http.ResponseWriter using io.WriteString
_, err = io.WriteString(respWriter, string(respJSON))

if err != nil {
// If there is an error writing the repsonse, write an error
errString := fmt.Sprintf("failed to write response: %s", err)
logger.Error(errString)
http.Error(respWriter, errString, http.StatusInternalServerError)
}
return respJSON
}

// WriteError will write the passed error to the http response writer
func WriteError(respWriter http.ResponseWriter, errorString string) {
respWriter.Header().Set("Content-Type", "application/json")
respWriter.Write(getResponseJSON(sgtBaseResponse{Message: errorString, Status: statusError}))
writeResponseJSON(respWriter, sgtBaseResponse{Message: errorString, Status: statusError})
}

// WriteSuccess will write the a success status and optional message to the http response writer
func WriteSuccess(respWriter http.ResponseWriter, optionalMessage string) {
respWriter.Header().Set("Content-Type", "application/json")
respWriter.Write(getResponseJSON(sgtBaseResponse{Message: optionalMessage, Status: statusSuccess}))
writeResponseJSON(respWriter, sgtBaseResponse{Message: optionalMessage, Status: statusSuccess})
}

// WriteCustomJSON will write the custom response to the http response writer as json
func WriteCustomJSON(respWriter http.ResponseWriter, resp interface{}) {
respWriter.Header().Set("Content-Type", "application/json")
respWriter.Write(getResponseJSON(resp))
writeResponseJSON(respWriter, resp)
}

0 comments on commit 4e0d3c2

Please sign in to comment.