-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdecorators.go
69 lines (56 loc) · 1.44 KB
/
decorators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"errors"
"fmt"
"strings"
"time"
)
type loggingDecorator[C any] struct {
base CommandHandler[C]
logger Logger
}
func (d loggingDecorator[C]) Handle(ctx context.Context, cmd C) (err error) {
d.logger.Println("Executing command", commandName(cmd), cmd)
defer func() {
if err == nil {
d.logger.Println("Command executed successfully")
} else {
d.logger.Println("Failed to execute command:", err)
}
}()
return d.base.Handle(ctx, cmd)
}
type metricsDecorator[C any] struct {
base CommandHandler[C]
client MetricsClient
}
func (d metricsDecorator[C]) Handle(ctx context.Context, cmd C) (err error) {
start := time.Now()
defer func() {
end := time.Since(start)
d.client.Inc(fmt.Sprintf("commands.%s.duration", commandName(cmd)), int(end.Seconds()))
if err == nil {
d.client.Inc(fmt.Sprintf("commands.%s.success", commandName(cmd)), 1)
} else {
d.client.Inc(fmt.Sprintf("commands.%s.failure", commandName(cmd)), 1)
}
}()
return d.base.Handle(ctx, cmd)
}
type authorizationDecorator[C any] struct {
base CommandHandler[C]
}
func (d authorizationDecorator[C]) Handle(ctx context.Context, cmd C) error {
user, err := UserFromContext(ctx)
if err != nil {
return err
}
if !user.Active {
return errors.New("the user's account is not active")
}
return d.base.Handle(ctx, cmd)
}
func commandName(cmd any) string {
return strings.ToLower(strings.Split(fmt.Sprintf("%T", cmd), ".")[1])
}