Skip to content

Commit

Permalink
调试模式优化
Browse files Browse the repository at this point in the history
  • Loading branch information
panco95 committed Dec 28, 2021
1 parent 29f97b7 commit 82a281d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 48 deletions.
79 changes: 37 additions & 42 deletions core/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,56 @@ import (
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/opentracing/opentracing-go"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)

func (g *Garden) ginListen(listenAddress string, route func(r *gin.Engine), auth func() gin.HandlerFunc) error {
// init
gin.SetMode("release")
server := gin.Default()

// log
if err := createDir(g.cfg.RuntimePath); err != nil {
return err
}
file, err := os.Create(fmt.Sprintf("%s/gin.log", g.cfg.RuntimePath))
if err != nil {
return err
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
engine.Use(gin.Logger(), gin.Recovery())
if g.cfg.Service.Debug {
if err := createDir(g.cfg.RuntimePath); err != nil {
return err
}
file, err := os.Create(fmt.Sprintf("%s/gin.log", g.cfg.RuntimePath))
if err != nil {
return err
}
gin.DefaultWriter = file
engine.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage)
}))
pprof.Register(engine)
} else {
gin.DefaultWriter = ioutil.Discard
}
gin.DefaultWriter = file

// middlewares
server.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage)
}))
server.Use(gin.Recovery())
server.Use(g.openTracingMiddleware())

engine.Use(g.openTracingMiddleware())
if g.cfg.Service.AllowCors {
server.Use(g.cors)
engine.Use(cors)
}

// monitoring
g.prometheus(server)
pprof.Register(server)

g.prometheus(engine)
if auth != nil {
server.Use(auth())
engine.Use(auth())
}
notFound(engine)
route(engine)

// routes
notFound(server)
route(server)

// run
g.Log(InfoLevel, "http", fmt.Sprintf("listen on: %s", listenAddress))
return server.Run(listenAddress)
return engine.Run(listenAddress)
}

// GatewayRoute create gateway service type to use this
Expand Down Expand Up @@ -95,7 +90,7 @@ func (g *Garden) prometheus(r *gin.Engine) {
})
}

func (g *Garden) cors(ctx *gin.Context) {
func cors(ctx *gin.Context) {
method := ctx.Request.Method
ctx.Header("Access-Control-Allow-Origin", "*")
ctx.Header("Access-Control-Allow-Headers", "*")
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ garden new my-gateway gateway

| 字段 | 说明 |
| ---------------------- | --------------------------------------------------------------- |
| service->debug | 调试模式开关(true:日志打印和文件存储;false:日志仅文件存储不打印|
| service->debug | 调试模式(true:gin日志、打印日志、写入文件;false:日志仅写入文件|
| service->serviceName | 服务名称 |
| service->serviceIp | 服务器内网IP(如服务调用不正常可配置此项) |
| service->serviceIp | 服务器内网IP(如服务之间调用不正常需配置此项) |
| service->httpOut | http端口是否允许外网访问:true允许,false不允许 |
| service->httpPort | http监听端口 |
| service->allowCors | http是否允许跨域 |
Expand Down Expand Up @@ -681,7 +681,7 @@ global.Garden.Log(core.FatalLevel, "test", "info")

### 二十. 服务监控与警报

1、集成pprof性能监控,路由:/debug/pprof
1、集成pprof性能监控,路由:/debug/pprof(需要开启debug调试模式,因为安全问题不建议生产环境开启)

2、支持[Prometheus](https://prometheus.io)

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github地址:https://github.com/panco95/go-garden

```
// 安装项目脚手架
go get -u github.com/panco95/[email protected].1
go install github.com/panco95/go-garden/tools/[email protected].1
go get -u github.com/panco95/[email protected].2
go install github.com/panco95/go-garden/tools/[email protected].2
// 创建项目
garden new my-gateway gateway
Expand Down
2 changes: 1 addition & 1 deletion tools/garden/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 安装

`go install github.com/panco95/go-garden/tools/[email protected].1`
`go install github.com/panco95/go-garden/tools/[email protected].2`

## 创建项目

Expand Down

0 comments on commit 82a281d

Please sign in to comment.