From ebf7bb63483e06310df45c3c70bc59d7e468ad58 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sun, 5 May 2024 12:25:26 +1200 Subject: [PATCH] Chore: Simplify JSON HTTP responses --- server/apiv1/api.go | 54 +++++++++++++++++++++---------------------- server/apiv1/info.go | 8 +++---- server/apiv1/send.go | 6 ++--- server/apiv1/webui.go | 6 ++--- server/server.go | 2 -- 5 files changed, 35 insertions(+), 41 deletions(-) diff --git a/server/apiv1/api.go b/server/apiv1/api.go index 8f995e10b..8f3fee5a4 100644 --- a/server/apiv1/api.go +++ b/server/apiv1/api.go @@ -73,9 +73,10 @@ func GetMessages(w http.ResponseWriter, r *http.Request) { res.Tags = stats.Tags res.MessagesCount = stats.Total - bytes, _ := json.Marshal(res) w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(res); err != nil { + httpError(w, err.Error()) + } } // Search returns the latest messages as JSON @@ -144,9 +145,10 @@ func Search(w http.ResponseWriter, r *http.Request) { res.Unread = stats.Unread res.Tags = stats.Tags - bytes, _ := json.Marshal(res) w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(res); err != nil { + httpError(w, err.Error()) + } } // DeleteSearch will delete all messages matching a search @@ -238,9 +240,10 @@ func GetMessage(w http.ResponseWriter, r *http.Request) { return } - bytes, _ := json.Marshal(msg) w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(msg); err != nil { + httpError(w, err.Error()) + } } // DownloadAttachment (method: GET) returns the attachment data @@ -347,14 +350,10 @@ func GetHeaders(w http.ResponseWriter, r *http.Request) { return } - bytes, err := json.Marshal(m.Header) - if err != nil { + w.Header().Add("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(m.Header); err != nil { httpError(w, err.Error()) - return } - - w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) } // DownloadRaw (method: GET) returns the full email source as plain text @@ -541,16 +540,10 @@ func GetAllTags(w http.ResponseWriter, _ *http.Request) { // 200: ArrayResponse // default: ErrorResponse - tags := storage.GetAllTags() - - data, err := json.Marshal(tags) - if err != nil { + w.Header().Add("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(storage.GetAllTags()); err != nil { httpError(w, err.Error()) - return } - - w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(data) } // SetMessageTags (method: PUT) will set the tags for all provided IDs @@ -777,9 +770,10 @@ func HTMLCheck(w http.ResponseWriter, r *http.Request) { return } - bytes, _ := json.Marshal(checks) w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(checks); err != nil { + httpError(w, err.Error()) + } } // LinkCheck returns a summary of links in the email @@ -827,9 +821,10 @@ func LinkCheck(w http.ResponseWriter, r *http.Request) { return } - bytes, _ := json.Marshal(summary) w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(summary); err != nil { + httpError(w, err.Error()) + } } // SpamAssassinCheck returns a summary of SpamAssassin results (if enabled) @@ -877,9 +872,10 @@ func SpamAssassinCheck(w http.ResponseWriter, r *http.Request) { return } - bytes, _ := json.Marshal(summary) w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(summary); err != nil { + httpError(w, err.Error()) + } } // FourOFour returns a basic 404 message @@ -908,9 +904,11 @@ func httpJSONError(w http.ResponseWriter, msg string) { e := JSONErrorMessage{ Error: msg, } - bytes, _ := json.Marshal(e) + w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(e); err != nil { + httpError(w, err.Error()) + } } // Get the start and limit based on query params. Defaults to 0, 50 diff --git a/server/apiv1/info.go b/server/apiv1/info.go index 06bd9d967..579feeb28 100644 --- a/server/apiv1/info.go +++ b/server/apiv1/info.go @@ -24,10 +24,8 @@ func AppInfo(w http.ResponseWriter, _ *http.Request) { // 200: InfoResponse // default: ErrorResponse - info := stats.Load() - - bytes, _ := json.Marshal(info) - w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(stats.Load()); err != nil { + httpError(w, err.Error()) + } } diff --git a/server/apiv1/send.go b/server/apiv1/send.go index 773338033..54f72a1b0 100644 --- a/server/apiv1/send.go +++ b/server/apiv1/send.go @@ -157,10 +157,10 @@ func SendMessageHandler(w http.ResponseWriter, r *http.Request) { return } - bytes, _ := json.Marshal(SendMessageConfirmation{ID: id}) - w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(SendMessageConfirmation{ID: id}); err != nil { + httpError(w, err.Error()) + } } // Send will validate the message structure and attempt to send to Mailpit. diff --git a/server/apiv1/webui.go b/server/apiv1/webui.go index 40015f8cc..d3c9a2d12 100644 --- a/server/apiv1/webui.go +++ b/server/apiv1/webui.go @@ -65,8 +65,8 @@ func WebUIConfig(w http.ResponseWriter, _ *http.Request) { conf.SpamAssassin = config.EnableSpamAssassin != "" conf.DuplicatesIgnored = config.IgnoreDuplicateIDs - bytes, _ := json.Marshal(conf) - w.Header().Add("Content-Type", "application/json") - _, _ = w.Write(bytes) + if err := json.NewEncoder(w).Encode(conf); err != nil { + httpError(w, err.Error()) + } } diff --git a/server/server.go b/server/server.go index bc0e0587e..8ea160db9 100644 --- a/server/server.go +++ b/server/server.go @@ -324,8 +324,6 @@ func index(w http.ResponseWriter, _ *http.Request) { panic(err) } - buff.Bytes() - w.Header().Add("Content-Type", "text/html") _, _ = w.Write(buff.Bytes()) }