This commit is contained in:
新亮
2021-01-16 22:18:55 +08:00
parent f32a8a1fb5
commit 845ef70a77
7 changed files with 29 additions and 33 deletions

View File

@@ -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

View File

@@ -86,6 +86,6 @@ func ProjectName() string {
return "go-gin-api"
}
func ProjectVersion() string {
return "v2.0"
func ProjectPort() string {
return ":9999"
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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))
})

View File

@@ -57,7 +57,7 @@ func main() {
}
server := &http.Server{
Addr: ":9999",
Addr: configs.ProjectPort(),
Handler: mux,
}

View File

@@ -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
}
}