From dfc0836b1177803693da9e05b4224d7f68f9de47 Mon Sep 17 00:00:00 2001 From: Dwi Siswanto Date: Tue, 24 Oct 2023 15:30:32 +0700 Subject: [PATCH] chore(utils): refactor & enhance custom header handling (#116) Signed-off-by: Dwi Siswanto --- teler.go | 5 +++-- utils.go | 12 +++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/teler.go b/teler.go index 9f42452e..45f8596e 100644 --- a/teler.go +++ b/teler.go @@ -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) diff --git a/utils.go b/utils.go index cb8701eb..a6b36f8f 100644 --- a/utils.go +++ b/utils.go @@ -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() }