Skip to content

Commit

Permalink
Merge pull request #25 from spotlibs/feat/enhancement-ctx
Browse files Browse the repository at this point in the history
feat: enhancement ctx for queue and request header
  • Loading branch information
mdanialr authored Oct 25, 2024
2 parents 1120f09 + a29473e commit a84d776
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
28 changes: 28 additions & 0 deletions ctx/api_requester.go
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)
}
6 changes: 3 additions & 3 deletions ctx/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions ctx/queue.go
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)
}

0 comments on commit a84d776

Please sign in to comment.