diff --git a/japicore/fidRouteHandling.go b/japicore/fidRouteHandling.go index e0198e2..cd677ab 100644 --- a/japicore/fidRouteHandling.go +++ b/japicore/fidRouteHandling.go @@ -116,7 +116,7 @@ func (j JApiCore) DeleteByFidHandler() bunrouter.HandlerFunc { // message := createJsonResponse("Deletion complete") message := createJsonResponse("Deletion Not Implemented") - condensedWriteJSON(w, message) + jutils.SimpleWriteJSON(w, message) return nil } } diff --git a/japicore/miscRouteHandling.go b/japicore/miscRouteHandling.go index 3685968..becbfdd 100644 --- a/japicore/miscRouteHandling.go +++ b/japicore/miscRouteHandling.go @@ -36,7 +36,7 @@ func (j JApiCore) MethodNotAllowedHandler() bunrouter.HandlerFunc { func (j JApiCore) VersionHandler() bunrouter.HandlerFunc { return func(w http.ResponseWriter, req bunrouter.Request) error { message := createJsonResponse("") - condensedWriteJSON(w, message) + jutils.SimpleWriteJSON(w, message) return nil } } diff --git a/japicore/pathRouteHandling.go b/japicore/pathRouteHandling.go index c818489..4d858dc 100644 --- a/japicore/pathRouteHandling.go +++ b/japicore/pathRouteHandling.go @@ -84,7 +84,7 @@ func (j JApiCore) deleteByPathCore(operatingRoot string, delFunc func(num int64) } message := createJsonResponse("Deletion complete") - condensedWriteJSON(w, message) + jutils.SimpleWriteJSON(w, message) return nil } } @@ -118,7 +118,7 @@ func (j JApiCore) ImportHandler() bunrouter.HandlerFunc { wg.Wait() message := createJsonResponse("Import complete") - condensedWriteJSON(w, message) + jutils.SimpleWriteJSON(w, message) return nil } } @@ -187,7 +187,7 @@ func (j JApiCore) UploadByPathHandler() bunrouter.HandlerFunc { } message := createJsonResponse("Upload complete") - condensedWriteJSON(w, message) + jutils.SimpleWriteJSON(w, message) return nil } } diff --git a/japicore/utils.go b/japicore/utils.go index 29051d0..624ad65 100644 --- a/japicore/utils.go +++ b/japicore/utils.go @@ -1,9 +1,6 @@ package japicore import ( - "bytes" - "encoding/json" - "fmt" "net/http" "sync" @@ -43,21 +40,6 @@ func processUpload(w http.ResponseWriter, fileIo *file_io_handler.FileIoHandler, return m.Fid() } -func condensedWriteJSON(w http.ResponseWriter, respVal interface{}) { - var buf bytes.Buffer - if err := json.NewEncoder(&buf).Encode(respVal); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - fmt.Printf("json.NewEncoder.Encode: %v", err) - return - } - w.Header().Set("Content-Type", "application/json") - written, err := w.Write(buf.Bytes()) - if err != nil { - fmt.Printf("Written bytes: %d\n", written) - fmt.Println(err) - } -} - func readUniquePath(req bunrouter.Request) string { uniquePath, ok := req.Context().Value(jutils.ReqUniquePath{}).(string) if !ok { diff --git a/jutils/data.go b/jutils/data.go index 9bd7cc0..8c63cf8 100644 --- a/jutils/data.go +++ b/jutils/data.go @@ -2,6 +2,9 @@ package jutils import ( "bytes" + "encoding/json" + "fmt" + "net/http" ) func CloneBytes(reader *bytes.Reader) []byte { @@ -39,3 +42,17 @@ func CloneByteSlice(source []byte) ([]byte, []byte, error) { return firstSlice, SecondSlice, nil } + +func SimpleWriteJSON(w http.ResponseWriter, respVal interface{}) { + var buf bytes.Buffer + if err := json.NewEncoder(&buf).Encode(respVal); err != nil { + ProcessHttpError("SimpleWriteJSON", err, 500, w) + return + } + w.Header().Set("Content-Type", "application/json") + written, err := w.Write(buf.Bytes()) + if err != nil { + fmt.Printf("Written bytes: %d\n", written) + ProcessError("SimpleWriteJSON", err) + } +}