-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
建立中间件 请求uuid log集成zap方便扩展 project-layout目录结构初步设定
- Loading branch information
Showing
20 changed files
with
389 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"type": "go", | ||
"name": "Go configuration", | ||
"goExecPath": "/usr/local/go/bin/go", | ||
"buildParams": ["$PROJECT_DIR$/main.go"], | ||
}, | ||
|
||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package endpoint | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"kit-study/internal/pkg/log" | ||
"time" | ||
|
||
"kit-study/internal/iam/service" | ||
|
||
"github.com/go-kit/kit/endpoint" | ||
) | ||
|
||
func logMiddleware() endpoint.Middleware { | ||
return func(next endpoint.Endpoint) endpoint.Endpoint { | ||
return func(ctx context.Context, request interface{}) (response interface{}, err error) { | ||
// 记录请求耗时 | ||
defer func(begin time.Time) { | ||
log.Debugw(fmt.Sprint(ctx.Value(service.ContextReqUUid)), "调用endpoint层logMiddleware", "处理完请求", "耗时毫秒", time.Since(begin).Milliseconds()) | ||
}(time.Now()) | ||
return next(ctx, request) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"kit-study/internal/pkg/log" | ||
) | ||
|
||
// 抽象:对应 Service 安装中间件 (serivce加一层装饰) | ||
// 1. 日志中间件 | ||
|
||
const ContextReqUUid = "req_uuid" | ||
|
||
type NewMiddleware func(Service) Service | ||
|
||
type logMiddleware struct { | ||
next Service | ||
} | ||
|
||
func NewLogMiddlewareServer() NewMiddleware { | ||
return func(service Service) Service { | ||
return &logMiddleware{ | ||
next: service, | ||
} | ||
} | ||
} | ||
|
||
func (l *logMiddleware) Health(ctx context.Context) (out string, err error) { | ||
// log 装饰 记录调用结果 | ||
defer func() { | ||
log.Debugw(fmt.Sprint(ctx.Value(ContextReqUUid)), "调用service层logMiddleware", "service:health", "res", out) | ||
}() | ||
out, err = l.next.Health(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
return out, nil | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
14 changes: 12 additions & 2 deletions
14
transport/transport.go → internal/iam/transport/transport.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package known | ||
|
||
const ( | ||
// XRequestIDKey 用来定义 Gin 上下文中的键,代表请求的 uuid. | ||
XRequestIDKey = "X-Request-ID" | ||
|
||
// XUsernameKey 用来定义 Gin 上下文的键,代表请求的所有者. | ||
XUsernameKey = "X-Username" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package log | ||
|
||
// 后续会加入viper 读取配置 | ||
|
||
func LogOptions() *Options { | ||
return &Options{ | ||
DisableCaller: false, | ||
DisableStacktrace: false, | ||
Level: "debug", | ||
Format: "console", | ||
OutputPaths: []string{"stdout"}, | ||
} | ||
} |
Oops, something went wrong.