Skip to content

Commit

Permalink
建立中间件 请求uuid log集成zap方便扩展 project-layout目录结构初步设定
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleezesd committed Aug 22, 2023
1 parent 96d7e4f commit a05c01a
Show file tree
Hide file tree
Showing 20 changed files with 389 additions and 13 deletions.
11 changes: 11 additions & 0 deletions .fleet/run.json
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"],
},

]
}
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/kit-study.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@

# v1- kit base service & fx demo
建立 kit 基础服务 fx 依赖注入demo

# v2- middleware & request uuid & log & project-layout
建立中间件 请求uuid log集成zap方便扩展 project-layout目录结构初步设定


1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.20.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/go-kit/log v0.2.0/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBj
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
3 changes: 2 additions & 1 deletion endpoint/endpoint.go → internal/iam/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package endpoint
import (
"context"

"kit-study/service"
"kit-study/internal/iam/service"

"github.com/go-kit/kit/endpoint"
)
Expand All @@ -18,6 +18,7 @@ func NewEndPointServer(svc service.Service) EndPointServer {
var healthEndPoint endpoint.Endpoint
{
healthEndPoint = MakeHealthEndPoint(svc)
healthEndPoint = logMiddleware()(healthEndPoint)
}
return EndPointServer{
HealthEndPoint: healthEndPoint,
Expand Down
24 changes: 24 additions & 0 deletions internal/iam/endpoint/middleware.go
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)
}
}
}
38 changes: 38 additions & 0 deletions internal/iam/service/middleware.go
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.
11 changes: 7 additions & 4 deletions service/service.go → internal/iam/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ type Service interface {
}

func NewService() Service {
return &serviceServer{}
var server Service
server = &baseServer{}
server = NewLogMiddlewareServer()(server)
return server
}

type serviceServer struct {
type baseServer struct {
}

// 确保 baseServer 实现了接口
var _ Service = (*serviceServer)(nil)
var _ Service = (*baseServer)(nil)

func (s serviceServer) Health(ctx context.Context) (string, error) {
func (s baseServer) Health(ctx context.Context) (string, error) {
return fmt.Sprintln("health"), nil
}
File renamed without changes.
14 changes: 12 additions & 2 deletions transport/transport.go → internal/iam/transport/transport.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package transport

import (
"context"
"net/http"

e "kit-study/endpoint"
e "kit-study/internal/iam/endpoint"

"kit-study/internal/iam/service"
"kit-study/internal/pkg/log"

httptransport "github.com/go-kit/kit/transport/http"
uuid "github.com/satori/go.uuid"
)


// http Handler
func NewHttpHandler(endpoint e.EndPointServer) http.Handler {
options := []httptransport.ServerOption{
httptransport.ServerErrorEncoder(errorEncoder), //程序中的全部报错都会走这里面
httptransport.ServerBefore(func(ctx context.Context, request *http.Request) context.Context { // 添加middleware 增加请求的uuid
UUID := uuid.NewV5(uuid.NewV4(), "req_uuid").String()
log.Debugw("给请求添加uuid", "UUID", UUID)
ctx = context.WithValue(ctx, service.ContextReqUUid, UUID)
return ctx
}),
}

m := http.NewServeMux()
Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/known/known.go
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"
)
13 changes: 13 additions & 0 deletions internal/pkg/log/helper.go
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"},
}
}
Loading

0 comments on commit a05c01a

Please sign in to comment.