From fd842a5ee513ab19d9e9b464eadb76e6cdaece2a Mon Sep 17 00:00:00 2001 From: Danial Date: Fri, 25 Oct 2024 10:01:26 +0700 Subject: [PATCH 1/3] feat: append metadata context to request --- ctx/api_requester.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ctx/api_requester.go diff --git a/ctx/api_requester.go b/ctx/api_requester.go new file mode 100644 index 0000000..c8eaf8b --- /dev/null +++ b/ctx/api_requester.go @@ -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) +} From 98986f1c949e713f9761e1d4c76ef7a53e9caacb Mon Sep 17 00:00:00 2001 From: Danial Date: Fri, 25 Oct 2024 10:01:40 +0700 Subject: [PATCH 2/3] ref: change key context --- ctx/metadata.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ctx/metadata.go b/ctx/metadata.go index 9fd029b..57b14cd 100644 --- a/ctx/metadata.go +++ b/ctx/metadata.go @@ -9,7 +9,7 @@ import ( // keyMetadata custom type that can prevent collision. type keyMetadata struct{} -var keyCtx keyMetadata +var contextKey keyMetadata // Metadata holds any request-scoped shared data within brispot microservice. type Metadata struct { @@ -40,7 +40,7 @@ type Metadata struct { // Get retrieve Metadata from given context with key from this pkg. func Get(ctx context.Context) Metadata { - if mt, ok := ctx.Value(keyCtx).(Metadata); ok { + if mt, ok := ctx.Value(contextKey).(Metadata); ok { return mt } return Metadata{} @@ -73,7 +73,7 @@ func SetFromRequestHeader(c http.Context) { PathGateway: c.Request().Header("X-Path-Gateway"), UrlPath: c.Request().Path(), } - c.WithValue(keyCtx, mt) + c.WithValue(contextKey, mt) } // GetReqId extract request id from given context. This is a shortcut for Get From a29473edfa72d840a420023285573300df2d69cb Mon Sep 17 00:00:00 2001 From: Danial Date: Fri, 25 Oct 2024 10:02:12 +0700 Subject: [PATCH 3/3] feat: ctx metadata for queue job --- ctx/queue.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 ctx/queue.go diff --git a/ctx/queue.go b/ctx/queue.go new file mode 100644 index 0000000..a6bd033 --- /dev/null +++ b/ctx/queue.go @@ -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) +}