Skip to content

Commit 84c9b58

Browse files
committed
save
2 parents 9569727 + a58f8f6 commit 84c9b58

File tree

11 files changed

+613
-15
lines changed

11 files changed

+613
-15
lines changed

go-kit/v2/v2_transport/ZapLogErrorHandler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ func NewZapLogErrorHandler(logger *zap.Logger) *LogErrorHandler {
1818
}
1919

2020
func (h *LogErrorHandler) Handle(ctx context.Context, err error) {
21-
h.logger.Warn(fmt.Sprint(ctx.Value(v2_service.ContextReqUUid), zap.Error(err)))
21+
if err != nil {
22+
h.logger.Warn(fmt.Sprint(ctx.Value(v2_service.ContextReqUUid), zap.Error(err)))
23+
ctx.Done()
24+
}
2225
}

go-kit/v2/v2_transport/transport.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"learning_tools/go-kit/v2/v2_service"
1313
"net/http"
1414
"strconv"
15+
"time"
1516
)
1617

1718
func NewHttpHandler(endpoint v2_endpoint.EndPointServer, log *zap.Logger) http.Handler {
@@ -21,13 +22,19 @@ func NewHttpHandler(endpoint v2_endpoint.EndPointServer, log *zap.Logger) http.H
2122
w.WriteHeader(http.StatusOK)
2223
json.NewEncoder(w).Encode(errorWrapper{Error: err.Error()})
2324
}), //程序中的全部报错都会走这里面
24-
//httptransport.ServerErrorHandler(NewZapLogErrorHandler(log)),
25+
httptransport.ServerErrorHandler(NewZapLogErrorHandler(log)),
2526
httptransport.ServerBefore(func(ctx context.Context, request *http.Request) context.Context {
27+
ctx, _ = context.WithDeadline(ctx, time.Now().Add(1*time.Second))
2628
UUID := uuid.NewV5(uuid.Must(uuid.NewV4(), nil), "req_uuid").String()
2729
log.Debug("给请求添加uuid", zap.Any("UUID", UUID))
2830
ctx = context.WithValue(ctx, v2_service.ContextReqUUid, UUID)
2931
return ctx
3032
}),
33+
34+
httptransport.ServerAfter(func(ctx context.Context, resp http.ResponseWriter) context.Context {
35+
fmt.Println("hpj---------value", ctx.Value(v2_service.ContextReqUUid))
36+
return context.WithValue(ctx, 12, 3223)
37+
}),
3138
}
3239
m := http.NewServeMux()
3340
m.Handle("/sum", httptransport.NewServer(
@@ -46,6 +53,7 @@ func decodeHTTPADDRequest(ctx context.Context, r *http.Request) (interface{}, er
4653
)
4754
in.A, err = strconv.Atoi(r.FormValue("a"))
4855
in.B, err = strconv.Atoi(r.FormValue("b"))
56+
//err = errors.New("测试encode 出错------")
4957
if err != nil {
5058
return in, err
5159
}

go-kit/v3/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module learning_tools/go-kit/v3
2+
3+
go 1.14
4+
5+
require (
6+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
7+
github.com/go-kit/kit v0.10.0
8+
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
9+
github.com/satori/go.uuid v1.2.0
10+
go.uber.org/zap v1.16.0
11+
learning_tools/all_packaged_library v0.0.0-00010101000000-000000000000
12+
)
13+
14+
replace learning_tools/all_packaged_library => ../../all_packaged_library

go-kit/v3/go.sum

Lines changed: 361 additions & 0 deletions
Large diffs are not rendered by default.

go-kit/v3/v3_endpoint/endpoint.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ func NewEndPointServer(svc v3_service.Service, log *zap.Logger) EndPointServer {
2727
return EndPointServer{AddEndPoint: addEndPoint, LoginEndPoint: loginEndPoint}
2828
}
2929

30-
func (s EndPointServer) Add(ctx context.Context, in v3_service.Add) v3_service.AddAck {
31-
res, _ := s.AddEndPoint(ctx, in)
32-
return res.(v3_service.AddAck)
33-
}
30+
//func (s EndPointServer) Add(ctx context.Context, in v3_service.Add) v3_service.AddAck {
31+
// res, _ := s.AddEndPoint(ctx, in)
32+
// return res.(v3_service.AddAck)
33+
//}
3434

3535
func MakeAddEndPoint(s v3_service.Service) endpoint.Endpoint {
3636
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
@@ -40,10 +40,10 @@ func MakeAddEndPoint(s v3_service.Service) endpoint.Endpoint {
4040
}
4141
}
4242

43-
func (s EndPointServer) Login(ctx context.Context, in v3_service.Login) (v3_service.LoginAck, error) {
44-
res, err := s.LoginEndPoint(ctx, in)
45-
return res.(v3_service.LoginAck), err
46-
}
43+
//func (s EndPointServer) Login(ctx context.Context, in v3_service.Login) (v3_service.LoginAck, error) {
44+
// res, err := s.LoginEndPoint(ctx, in)
45+
// return res.(v3_service.LoginAck), err
46+
//}
4747

4848
func MakeLoginEndPoint(s v3_service.Service) endpoint.Endpoint {
4949
return func(ctx context.Context, request interface{}) (response interface{}, err error) {

go-kit/v3/v3_service/middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func NewLogMiddlewareServer(log *zap.Logger) NewMiddlewareServer {
2525
}
2626

2727
func (l logMiddlewareServer) TestAdd(ctx context.Context, in Add) (out AddAck) {
28+
2829
defer func() {
2930
l.logger.Debug(fmt.Sprint(ctx.Value(ContextReqUUid)), zap.Any("调用 Login logMiddlewareServer", "TestAdd"), zap.Any("req", in), zap.Any("res", out))
3031
}()

go-kit/v3/v3_service/model.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package v3_service
22

3+
type ComRespone struct {
4+
Code int `json:"code"`
5+
}
6+
37
type Add struct {
48
A int `json:"a"`
59
B int `json:"b"`
610
}
711

812
type AddAck struct {
13+
ComRespone
914
Res int `json:"res"`
1015
}
1116

@@ -15,5 +20,6 @@ type Login struct {
1520
}
1621

1722
type LoginAck struct {
23+
ComRespone
1824
Token string `json:"token"`
1925
}

go-kit/v3/v3_transport/transport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func NewHttpHandler(endpoint v3_endpoint.EndPointServer, log *zap.Logger) http.H
2222
json.NewEncoder(w).Encode(errorWrapper{Error: err.Error()})
2323
}), //程序中的全部报错都会走这里面
2424
httptransport.ServerBefore(func(ctx context.Context, request *http.Request) context.Context {
25-
UUID := uuid.NewV5(uuid.Must(uuid.NewV4()), "req_uuid").String()
25+
UUID := uuid.NewV5(uuid.Must(uuid.NewV4(), nil), "req_uuid").String()
2626
log.Debug("给请求添加uuid", zap.Any("UUID", UUID))
2727
ctx = context.WithValue(ctx, v3_service.ContextReqUUid, UUID)
2828
ctx = context.WithValue(ctx, utils.JWT_CONTEXT_KEY, request.Header.Get("Authorization"))

0 commit comments

Comments
 (0)