-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.go
68 lines (57 loc) · 1.89 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
60
61
62
63
64
65
66
67
68
package session
import (
"fmt"
"time"
"golang.org/x/net/context"
"github.com/go-kit/kit/log"
)
// Middleware describes a service (as opposed to endpoint) middleware.
type Middleware func(Service) Service
//LoggingMiddleware for logging services execution
func LoggingMiddleware(logger log.Logger) Middleware {
fmt.Println("Logging middleware")
return func(next Service) Service {
return &loggingMiddleware{
next: next,
logger: logger,
}
}
}
type loggingMiddleware struct {
next Service
logger log.Logger
}
func (mw loggingMiddleware) login(ctx context.Context, r LoginRequest) (resp LoginResponse, err error) {
defer func(begin time.Time) {
mw.logger.Log("Host", r.httpreq.Host, "Url", r.httpreq.URL)
mw.logger.Log("method", "login", "took", time.Since(begin), "err", err)
}(time.Now())
fmt.Println("login middleware called")
resp, err = mw.next.login(ctx, r)
return
}
func (mw loggingMiddleware) logout(ctx context.Context, r LogoutRequest) (resp LogoutResponse, err error) {
defer func(begin time.Time) {
mw.logger.Log("Host", r.httpreq.Host, "Url", r.httpreq.URL)
mw.logger.Log("method", "Logout", "took", time.Since(begin), "err", err)
}(time.Now())
fmt.Println("logout middleware called")
resp, err = mw.next.logout(ctx, r)
return
}
func (mw loggingMiddleware) validateapp(ctx context.Context, r validateAppRequest) (resp LoginResponse, err error) {
defer func(begin time.Time) {
mw.logger.Log("Host", r.httpreq.Host, "Url", r.httpreq.URL)
mw.logger.Log("method", "validateapp", "took", time.Since(begin), "err", err)
}(time.Now())
fmt.Println("validateapp middleware called")
resp, err = mw.next.validateapp(ctx, r)
return
}
func (mw loggingMiddleware) apiprocess(ctx context.Context, r apiRequest) (resp interface{}, err error) {
defer func(begin time.Time) {
}(time.Now())
fmt.Println("api process middleware called")
resp, err = mw.next.apiprocess(ctx, r)
return
}