Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(utis): mv setReqIdHeader to getUID #117

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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