@rivascva/dt-idl 1.1.74 → 1.1.75
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 +28 -0
- package/go/middlewares/middlewares.go +11 -0
- package/go/models/constants.go +2 -0
- package/go/utils/context.go +9 -0
- package/package.json +1 -1
package/go/log/log.go
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
package log
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"context"
|
|
4
5
|
"fmt"
|
|
5
6
|
"log"
|
|
6
7
|
"maps"
|
|
7
8
|
"os"
|
|
9
|
+
|
|
10
|
+
"github.com/RivasCVA/dt-idl/go/utils"
|
|
8
11
|
)
|
|
9
12
|
|
|
10
13
|
type Logger interface {
|
|
@@ -18,6 +21,8 @@ type Logger interface {
|
|
|
18
21
|
Fatal(m any)
|
|
19
22
|
// With returns a new log with the given key-value pairs.
|
|
20
23
|
With(kvs ...any) Logger
|
|
24
|
+
// WithContext returns a new log with the given context.
|
|
25
|
+
WithContext(ctx context.Context) Logger
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
type Log struct {
|
|
@@ -76,5 +81,28 @@ func (l *Log) With(kvs ...any) Logger {
|
|
|
76
81
|
}
|
|
77
82
|
}
|
|
78
83
|
|
|
84
|
+
func (l *Log) WithContext(ctx context.Context) Logger {
|
|
85
|
+
// copy the existing key-value pairs into a new map
|
|
86
|
+
newkvs := make(map[string]any, len(l.kvs)+10)
|
|
87
|
+
maps.Copy(newkvs, l.kvs)
|
|
88
|
+
|
|
89
|
+
// add the user id to the key-value pairs
|
|
90
|
+
if userId, err := utils.GetUserIdFromContext(ctx); err == nil {
|
|
91
|
+
newkvs["userId"] = userId
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// add the request path to the key-value pairs
|
|
95
|
+
if path, err := utils.GetPathFromContext(ctx); err == nil {
|
|
96
|
+
newkvs["path"] = path
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return &Log{
|
|
100
|
+
kvs: newkvs,
|
|
101
|
+
info: l.info,
|
|
102
|
+
warn: l.warn,
|
|
103
|
+
error: l.error,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
79
107
|
//exhaustruct:ignore - check for interface implementation
|
|
80
108
|
var _ Logger = &Log{}
|
|
@@ -50,6 +50,17 @@ func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
// ContextMiddleware attaches helpful information to the request context.
|
|
54
|
+
func ContextMiddleware(next http.Handler) http.Handler {
|
|
55
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
56
|
+
// set the path in the request context
|
|
57
|
+
ctx := context.WithValue(r.Context(), models.PathKey, r.URL.Path)
|
|
58
|
+
r = r.WithContext(ctx)
|
|
59
|
+
|
|
60
|
+
next.ServeHTTP(w, r)
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
53
64
|
// CommonHeaders adds shared headers to all responses.
|
|
54
65
|
func CommonHeaders(next http.Handler) http.Handler {
|
|
55
66
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
package/go/models/constants.go
CHANGED
package/go/utils/context.go
CHANGED
|
@@ -37,3 +37,12 @@ func GetUserIdFromContext(ctx context.Context) (string, error) {
|
|
|
37
37
|
|
|
38
38
|
return userId, nil
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
// GetPathFromContext retrieves the request path from the given context.
|
|
42
|
+
func GetPathFromContext(ctx context.Context) (string, error) {
|
|
43
|
+
path, ok := ctx.Value(models.PathKey).(string)
|
|
44
|
+
if !ok {
|
|
45
|
+
return "", errors.New("path not found in the context")
|
|
46
|
+
}
|
|
47
|
+
return path, nil
|
|
48
|
+
}
|