Skip to content

Commit

Permalink
chore(utils): refactor & enhance custom header handling
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <me@dw1.io>
  • Loading branch information
dwisiswant0 committed Oct 24, 2023
1 parent a39d234 commit cdf919e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
5 changes: 3 additions & 2 deletions teler.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ func (t *Teler) postAnalyze(w http.ResponseWriter, r *http.Request, k threat.Thr
// Get the error message & convert to string as a message
msg := err.Error()

// Set custom headers
setCustomHeaders(w, msg, k)
// Set custom headers ("X-Teler-Msg" and "X-Teler-Threat")
setCustomHeader(w, xTelerMsg, msg)
setCustomHeader(w, xTelerThreat, k.String())

// Send the logs
t.sendLogs(r, k, id, msg)
Expand Down
12 changes: 5 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,18 @@ func normalizeRawStringReader(raw string) *strings.Reader {
return strings.NewReader(builder.String())
}

// setCustomHeader such as message and threat category to the header response
func setCustomHeaders(w http.ResponseWriter, msg string, cat threat.Threat) {
// Set the "X-Teler-Msg" and "X-Teler-Threat" header in the response
w.Header().Set(xTelerMsg, msg)
w.Header().Set(xTelerThreat, cat.String())
// setCustomHeader to set custom header to the response writer
func setCustomHeader(w http.ResponseWriter, key, value string) {
w.Header().Set(key, value)
}

// setReqIdHeader to set teler request ID header response
// setReqIdHeader to set teler request ID header response and return it
func setReqIdHeader(w http.ResponseWriter) string {
// Generate a unique ID using the gouid package.
id := gouid.Bytes(10)

// Set the "X-Teler-Req-Id" header in the response with the unique ID.
w.Header().Set(xTelerReqId, id.String())
setCustomHeader(w, xTelerReqId, id.String())

return id.String()
}
Expand Down

0 comments on commit cdf919e

Please sign in to comment.