Skip to content

Commit

Permalink
chore(utils): refactor & enhance custom header handling (#116)
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 authored Oct 24, 2023
1 parent a39d234 commit dfc0836
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

1 comment on commit dfc0836

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: dfc0836 Previous: 26a3e5d Ratio
BenchmarkInitializeDefault - ns/op 1571483220 ns/op 51486071 ns/op 30.52

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.