Skip to content

Commit

Permalink
Chore: Simplify JSON HTTP responses
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed May 5, 2024
1 parent ba0e40f commit ebf7bb6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 41 deletions.
54 changes: 26 additions & 28 deletions server/apiv1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions server/apiv1/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
6 changes: 3 additions & 3 deletions server/apiv1/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions server/apiv1/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
2 changes: 0 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit ebf7bb6

Please sign in to comment.