From 845ef70a77bd4c347099b21c7615fb9df67c0352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B0=E4=BA=AE?= Date: Sat, 16 Jan 2021 22:18:55 +0800 Subject: [PATCH] upgrade --- README.md | 5 +-- configs/configs.go | 4 +-- .../go_gin_api_repo/go_gin_api_test.go | 4 +-- internal/pkg/core/context.go | 4 +-- internal/pkg/core/core.go | 34 +++++++++---------- main.go | 2 +- pkg/httpclient/option.go | 9 ++--- 7 files changed, 29 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index ef1d30f..c34b535 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,8 @@ go run main.go -env fat ╚██████╔╝╚██████╔╝ ╚██████╔╝██║██║ ╚████║ ██║ ██║██║ ██║ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝ -* [register -env fat] +* [register port :9999] +* [register env fat] * [register cors] * [register rate] * [register panic notify] @@ -153,7 +154,7 @@ go run main.go -env fat * [register swagger] * [register prometheus] -// 启动成功后,可访问:http://127.0.0.1:9999/h/info +// 启动成功后,可访问:http://127.0.0.1:9999/system/health ``` #### swagger diff --git a/configs/configs.go b/configs/configs.go index e2f6162..df6f582 100644 --- a/configs/configs.go +++ b/configs/configs.go @@ -86,6 +86,6 @@ func ProjectName() string { return "go-gin-api" } -func ProjectVersion() string { - return "v2.0" +func ProjectPort() string { + return ":9999" } diff --git a/internal/api/repository/third_party_request/go_gin_api_repo/go_gin_api_test.go b/internal/api/repository/third_party_request/go_gin_api_repo/go_gin_api_test.go index 3cf45b4..00ffe81 100644 --- a/internal/api/repository/third_party_request/go_gin_api_repo/go_gin_api_test.go +++ b/internal/api/repository/third_party_request/go_gin_api_repo/go_gin_api_test.go @@ -18,7 +18,7 @@ func TestDemoGet(t *testing.T) { //httpclient.WithLogger(ctx.Logger()), httpclient.WithHeader("Authorization", authorization), httpclient.WithOnFailedRetry(3, time.Second*1, retryVerify), - httpclient.WithOnFailedAlarm("接口异常", new(third_party_request.AlarmEmail), alarmVerify), + httpclient.WithOnFailedAlarm("接口告警", new(third_party_request.AlarmEmail), alarmVerify), //httpclient.WithMock(MockDemoGet), ) @@ -63,7 +63,7 @@ func retryVerify(body []byte) (shouldRetry bool) { } // 设置告警规则 -func alarmVerify(body []byte) (shouldRetry bool) { +func alarmVerify(body []byte) (shouldAlarm bool) { if len(body) == 0 { return true } diff --git a/internal/pkg/core/context.go b/internal/pkg/core/context.go index 5d1a18b..c2a93b9 100644 --- a/internal/pkg/core/context.go +++ b/internal/pkg/core/context.go @@ -86,7 +86,7 @@ type Context interface { Logger() *zap.Logger setLogger(logger *zap.Logger) - GetPayload() errno.Error + getPayload() errno.Error SetPayload(payload errno.Error) Header() http.Header @@ -197,7 +197,7 @@ func (c *context) setLogger(logger *zap.Logger) { c.ctx.Set(_LoggerName, logger) } -func (c *context) GetPayload() errno.Error { +func (c *context) getPayload() errno.Error { payload, _ := c.ctx.Get(_PayloadName) return payload.(errno.Error) } diff --git a/internal/pkg/core/core.go b/internal/pkg/core/core.go index 9d5fe7d..80534a0 100644 --- a/internal/pkg/core/core.go +++ b/internal/pkg/core/core.go @@ -7,6 +7,7 @@ import ( "runtime/debug" "time" + "github.com/xinliangnote/go-gin-api/configs" _ "github.com/xinliangnote/go-gin-api/docs" "github.com/xinliangnote/go-gin-api/internal/api/code" "github.com/xinliangnote/go-gin-api/internal/pkg/trace" @@ -242,8 +243,8 @@ func New(logger *zap.Logger, options ...Option) (Mux, error) { } fmt.Println(color.Blue(_UI)) - - fmt.Println(color.Green(fmt.Sprintf("* [register -env %s]", env.Active().Value()))) + fmt.Println(color.Green(fmt.Sprintf("* [register port %s]", configs.ProjectPort()))) + fmt.Println(color.Green(fmt.Sprintf("* [register env %s]", env.Active().Value()))) // withoutLogPaths 这些请求,默认不记录日志 withoutTracePaths := map[string]bool{ @@ -263,8 +264,7 @@ func New(logger *zap.Logger, options ...Option) (Mux, error) { "/favicon.ico": true, - "/h/ping": true, - "/h/info": true, + "/system/health": true, } opt := new(option) @@ -365,7 +365,7 @@ func New(logger *zap.Logger, options ...Option) (Mux, error) { response = err } } else { - response = context.GetPayload() + response = context.getPayload() } if response != nil { @@ -440,22 +440,20 @@ func New(logger *zap.Logger, options ...Option) (Mux, error) { mux.engine.NoMethod(wrapHandlers(DisableTrace)...) mux.engine.NoRoute(wrapHandlers(DisableTrace)...) - - h := mux.Group("/h") + system := mux.Group("/system") { - h.GET("/ping", func(ctx Context) { - ctx.SetPayload(code.OK.WithData("pong")) - }) - - h.GET("/info", func(ctx Context) { + // 健康检查 + system.GET("/health", func(ctx Context) { resp := &struct { - Header interface{} `json:"header"` - Ts time.Time `json:"ts"` - Env string `json:"env"` + Timestamp time.Time `json:"timestamp"` + Environment string `json:"environment"` + Host string `json:"host"` + Status string `json:"status"` }{ - Header: ctx.Header(), - Ts: time.Now(), - Env: env.Active().Value(), + Timestamp: time.Now(), + Environment: env.Active().Value(), + Host: ctx.Host(), + Status: "ok", } ctx.SetPayload(code.OK.WithData(resp)) }) diff --git a/main.go b/main.go index 9895056..8e2a0b6 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,7 @@ func main() { } server := &http.Server{ - Addr: ":9999", + Addr: configs.ProjectPort(), Handler: mux, } diff --git a/pkg/httpclient/option.go b/pkg/httpclient/option.go index a9ea455..e3c8bbd 100644 --- a/pkg/httpclient/option.go +++ b/pkg/httpclient/option.go @@ -19,9 +19,6 @@ var ( } ) -// Trace 记录内部流转信息 -type Trace = trace.T - // Mock 定义接口Mock数据 type Mock func() (body []byte) @@ -82,7 +79,7 @@ func WithHeader(key, value string) Option { } // WithTrace 设置trace信息 -func WithTrace(t Trace) Option { +func WithTrace(t trace.T) Option { return func(opt *option) { if t != nil { opt.trace = t.(*trace.Trace) @@ -99,9 +96,9 @@ func WithLogger(logger *zap.Logger) Option { } // WithMock 设置 mock 数据 -func WithMock(mock Mock) Option { +func WithMock(m Mock) Option { return func(opt *option) { - opt.mock = mock + opt.mock = m } }