@rivascva/dt-idl 1.1.80 → 1.1.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/go/log/log.go +2 -4
- package/go/log/utils.go +8 -3
- package/go/middlewares/middlewares.go +17 -6
- package/go/models/constants.go +4 -2
- package/go/utils/context.go +14 -5
- package/go.mod +4 -1
- package/go.sum +2 -0
- package/package.json +1 -1
package/go/log/log.go
CHANGED
|
@@ -62,11 +62,9 @@ func (l *Log) With(kvs ...any) Logger {
|
|
|
62
62
|
// add the new key-value pairs into the new map
|
|
63
63
|
for i := 0; i < len(kvs); i += 2 {
|
|
64
64
|
k := fmt.Sprintf("%v", kvs[i])
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
v = fmt.Sprintf("%v", kvs[i+1])
|
|
65
|
+
if k != "" && i+1 < len(kvs) {
|
|
66
|
+
newkvs[k] = kvs[i+1]
|
|
68
67
|
}
|
|
69
|
-
newkvs[k] = v
|
|
70
68
|
}
|
|
71
69
|
|
|
72
70
|
return &Log{
|
package/go/log/utils.go
CHANGED
|
@@ -36,7 +36,7 @@ func toJson(m any, kvs map[string]any, level string) string {
|
|
|
36
36
|
// kvsWithContext returns a new kvs map with added context metadata.
|
|
37
37
|
func kvsWithContext(ctx context.Context, kvs map[string]any) map[string]any {
|
|
38
38
|
// copy the existing key-value pairs into a new map
|
|
39
|
-
newkvs := make(map[string]any, len(kvs)+
|
|
39
|
+
newkvs := make(map[string]any, len(kvs)+3)
|
|
40
40
|
maps.Copy(newkvs, kvs)
|
|
41
41
|
|
|
42
42
|
// add the user id to the key-value pairs
|
|
@@ -45,8 +45,13 @@ func kvsWithContext(ctx context.Context, kvs map[string]any) map[string]any {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
// add the request path to the key-value pairs
|
|
48
|
-
if
|
|
49
|
-
newkvs["
|
|
48
|
+
if requestPath, err := utils.GetRequestPathFromContext(ctx); err == nil {
|
|
49
|
+
newkvs["requestPath"] = requestPath
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// add the request id to the key-value pairs
|
|
53
|
+
if requestId, err := utils.GetRequestIdFromContext(ctx); err == nil {
|
|
54
|
+
newkvs["requestId"] = requestId
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
return newkvs
|
|
@@ -10,6 +10,7 @@ import (
|
|
|
10
10
|
"github.com/RivasCVA/dt-idl/go/log"
|
|
11
11
|
"github.com/RivasCVA/dt-idl/go/models"
|
|
12
12
|
"github.com/RivasCVA/dt-idl/go/write"
|
|
13
|
+
"github.com/google/uuid"
|
|
13
14
|
)
|
|
14
15
|
|
|
15
16
|
// GetAuthMiddleware returns a middleware function that authenticates all requests, except for the login endpoints.
|
|
@@ -19,7 +20,7 @@ func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
|
|
|
19
20
|
|
|
20
21
|
return func(next http.Handler) http.Handler {
|
|
21
22
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
22
|
-
// check if the request is a login endpoint
|
|
23
|
+
// check if the request path is a login endpoint
|
|
23
24
|
if slices.Contains([]string{"/v1/login", "/v1/login/provider"}, r.URL.Path) {
|
|
24
25
|
next.ServeHTTP(w, r)
|
|
25
26
|
return
|
|
@@ -49,13 +50,23 @@ func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
|
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
//
|
|
53
|
-
func
|
|
53
|
+
// CommonContext adds shared values to the request context.
|
|
54
|
+
func CommonContext(next http.Handler) http.Handler {
|
|
54
55
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
55
|
-
//
|
|
56
|
-
ctx :=
|
|
57
|
-
|
|
56
|
+
// get the request context
|
|
57
|
+
ctx := r.Context()
|
|
58
|
+
|
|
59
|
+
// set the request id in the request context
|
|
60
|
+
requestId := r.Header.Get("X-Request-Id")
|
|
61
|
+
if requestId == "" {
|
|
62
|
+
requestId = uuid.New().String()
|
|
63
|
+
}
|
|
64
|
+
ctx = context.WithValue(ctx, models.RequestIdKey, requestId)
|
|
58
65
|
|
|
66
|
+
// set the request path in the request context
|
|
67
|
+
ctx = context.WithValue(ctx, models.RequestPathKey, r.URL.Path)
|
|
68
|
+
|
|
69
|
+
r = r.WithContext(ctx)
|
|
59
70
|
next.ServeHTTP(w, r)
|
|
60
71
|
})
|
|
61
72
|
}
|
package/go/models/constants.go
CHANGED
|
@@ -6,6 +6,8 @@ type ContextKey string
|
|
|
6
6
|
const (
|
|
7
7
|
// TokenKey is the key used to set/get the JWT token in the request context.
|
|
8
8
|
TokenKey = ContextKey("token")
|
|
9
|
-
//
|
|
10
|
-
|
|
9
|
+
// RequestPathKey is the key used to set/get the request path in the request context.
|
|
10
|
+
RequestPathKey = ContextKey("requestPath")
|
|
11
|
+
// RequestIdKey is the key used to set/get the request id in the request context.
|
|
12
|
+
RequestIdKey = ContextKey("requestId")
|
|
11
13
|
)
|
package/go/utils/context.go
CHANGED
|
@@ -38,11 +38,20 @@ func GetUserIdFromContext(ctx context.Context) (string, error) {
|
|
|
38
38
|
return userId, nil
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
//
|
|
42
|
-
func
|
|
43
|
-
|
|
41
|
+
// GetRequestPathFromContext retrieves the request path from the given context.
|
|
42
|
+
func GetRequestPathFromContext(ctx context.Context) (string, error) {
|
|
43
|
+
requestPath, ok := ctx.Value(models.RequestPathKey).(string)
|
|
44
44
|
if !ok {
|
|
45
|
-
return "", errors.New("path not found in the context")
|
|
45
|
+
return "", errors.New("request path not found in the context")
|
|
46
46
|
}
|
|
47
|
-
return
|
|
47
|
+
return requestPath, nil
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// GetRequestIdFromContext retrieves the request id from the given context.
|
|
51
|
+
func GetRequestIdFromContext(ctx context.Context) (string, error) {
|
|
52
|
+
requestId, ok := ctx.Value(models.RequestIdKey).(string)
|
|
53
|
+
if !ok {
|
|
54
|
+
return "", errors.New("request id not found in the context")
|
|
55
|
+
}
|
|
56
|
+
return requestId, nil
|
|
48
57
|
}
|
package/go.mod
CHANGED
package/go.sum
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
|
|
2
2
|
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
|
3
|
+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|
4
|
+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|