Skip to content

Commit

Permalink
chore(utis): mv setReqIdHeader to getUID
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 dfc0836 commit 1d93b1d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (t *Teler) Analyze(w http.ResponseWriter, r *http.Request) error {

// If threat detected, set teler request ID to the header
if err != nil {
setReqIdHeader(w)
setCustomHeader(w, xTelerReqId, getUID())
}

return err
Expand Down
7 changes: 4 additions & 3 deletions teler.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,16 @@ func (t *Teler) postAnalyze(w http.ResponseWriter, r *http.Request, k threat.Thr
return
}

// Set teler request ID to the header
id := setReqIdHeader(w)
// Get unique ID
id := getUID()

// Get the error message & convert to string as a message
msg := err.Error()

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

// Send the logs
t.sendLogs(r, k, id, msg)
Expand Down
7 changes: 2 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,11 @@ func setCustomHeader(w http.ResponseWriter, key, value string) {
w.Header().Set(key, value)
}

// setReqIdHeader to set teler request ID header response and return it
func setReqIdHeader(w http.ResponseWriter) string {
// getUID to get unique ID
func getUID() 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.
setCustomHeader(w, xTelerReqId, id.String())

return id.String()
}

Expand Down

0 comments on commit 1d93b1d

Please sign in to comment.