-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from spotlibs/feat/enhancement-ctx
feat: enhancement ctx for queue and request header
- Loading branch information
Showing
3 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ctx | ||
|
||
import "net/http" | ||
|
||
func SetHTTPRequestHeader(r *http.Request) { | ||
mt := Get(r.Context()) | ||
r.Header.Set("X-Request-ID", mt.ReqId) | ||
r.Header.Set("X-Request-User", mt.ReqUser) | ||
r.Header.Set("X-Api-Key", mt.ApiKey) | ||
r.Header.Set("Authorization", mt.Authorization) | ||
r.Header.Set("X-Path-Gateway", mt.PathGateway) | ||
r.Header.Set("X-Request-Kode-Region", mt.ReqKodeRegion) | ||
r.Header.Set("X-Request-Kode-MainUker", mt.ReqKodeMainUker) | ||
r.Header.Set("X-Request-Jenis-Uker", mt.ReqJenisUker) | ||
r.Header.Set("X-Request-Nama-Uker", mt.ReqNamaUker) | ||
r.Header.Set("X-Request-Kode-Uker", mt.ReqKodeUker) | ||
r.Header.Set("X-Request-Nama-Jabatan", mt.ReqNamaJabatan) | ||
r.Header.Set("X-Request-Kode-Jabatan", mt.ReqKodeJabatan) | ||
r.Header.Set("X-Request-Nama", mt.ReqNama) | ||
r.Header.Set("X-Request-Tags", mt.ReqTags) | ||
r.Header.Set("X-Version-App", mt.VersionApp) | ||
r.Header.Set("X-App", mt.App) | ||
r.Header.Set("X-Device-ID", mt.DeviceId) | ||
r.Header.Set("X-Request-From", mt.RequestFrom) | ||
r.Header.Set("X-Forwarded-For", mt.ForwardedFor) | ||
r.Header.Set("Cache-Control", mt.CacheControl) | ||
r.Header.Set("User-Agent", mt.UserAgent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ctx | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/goravel/framework/contracts/queue" | ||
) | ||
|
||
// NewFromWorker return new context that has given signature and request id as | ||
// the metadata inside the context. | ||
func NewFromWorker(sig, reqId string) context.Context { | ||
var meta Metadata | ||
meta.SignaturePath = sig | ||
meta.ReqId = reqId | ||
return context.WithValue(context.Background(), contextKey, meta) | ||
} | ||
|
||
// ToQueue append request id from given context to queue.Arg. | ||
func ToQueue(c context.Context) (out []queue.Arg) { | ||
out = append(out, queue.Arg{Type: "string", Value: Get(c).ReqId}) | ||
return | ||
} | ||
|
||
// NewFromQueue capture request/task id from given queue value in job, also | ||
// set given signature as signature path in metadata context then return the | ||
// context. | ||
// | ||
// This function expect the request id is in the first argument, and can be | ||
// safely used if sending the job to queue using ToQueue. | ||
func NewFromQueue(sig string, q ...any) context.Context { | ||
var meta Metadata | ||
if len(q) > 0 { | ||
if v, ok := q[0].(string); ok { | ||
meta.ReqId = v | ||
} | ||
} | ||
meta.SignaturePath = sig | ||
return context.WithValue(context.Background(), contextKey, meta) | ||
} |