/
middlewares.go
59 lines (49 loc) · 1.68 KB
/
middlewares.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
package dieded
import (
"context"
"time"
"github.com/go-kit/log"
)
type Middleware func(Service) Service
func LoggingMiddleware(logger log.Logger) Middleware {
return func(next Service) Service {
return &loggingMiddleware{
next: next,
logger: logger,
}
}
}
type loggingMiddleware struct {
next Service
logger log.Logger
}
func (mw loggingMiddleware) CreateProfile(ctx context.Context, f ProfileForm) (p *Profile, err error) {
defer func(begin time.Time) {
mw.logger.Log("method", "CreateProfile", "name", f.Name, "took", time.Since(begin), "err", err)
}(time.Now())
return mw.next.CreateProfile(ctx, f)
}
func (mw loggingMiddleware) GetProfile(ctx context.Context, id int) (p *Profile, err error) {
defer func(begin time.Time) {
mw.logger.Log("method", "GetProfile", "id", id, "took", time.Since(begin), "err", err)
}(time.Now())
return mw.next.GetProfile(ctx, id)
}
func (mw loggingMiddleware) QueryProfile(ctx context.Context, q Query) (p *Profile, err error) {
defer func(begin time.Time) {
mw.logger.Log("method", "QueryProfile", "name", q.Name, "took", time.Since(begin), "err", err)
}(time.Now())
return mw.next.QueryProfile(ctx, q)
}
func (mw loggingMiddleware) DieProfile(ctx context.Context, id int) (p *Profile, err error) {
defer func(begin time.Time) {
mw.logger.Log("method", "DieProfile", "id", id, "took", time.Since(begin), "err", err)
}(time.Now())
return mw.next.DieProfile(ctx, id)
}
func (mw loggingMiddleware) DeleteProfile(ctx context.Context, id int) (err error) {
defer func(begin time.Time) {
mw.logger.Log("method", "DeleteProfile", "id", id, "took", time.Since(begin), "err", err)
}(time.Now())
return mw.next.DeleteProfile(ctx, id)
}