upgrade
This commit is contained in:
@@ -86,14 +86,38 @@ func (d *Demo) Post() core.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
type request struct {
|
||||
Name string `uri:"name"`
|
||||
}
|
||||
|
||||
type response []struct {
|
||||
Name string `json:"name"` //用户名
|
||||
Job string `json:"job"` //工作
|
||||
}
|
||||
|
||||
// Get 获取用户信息
|
||||
// @Summary 获取用户信息
|
||||
// @Description 获取用户信息
|
||||
// @Tags Demo
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param name path string true "用户名"
|
||||
// @Success 200 {object} response "用户信息"
|
||||
// @Router /demo/user/{name} [get]
|
||||
func (d *Demo) User() core.HandlerFunc {
|
||||
|
||||
type response []struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
return func(c core.Context) {
|
||||
body1, err1 := httpclient.Get("http://127.0.0.1:9999/demo/get/Tom", nil,
|
||||
req := new(request)
|
||||
if err := c.ShouldBindURI(req); err != nil {
|
||||
c.SetPayload(errno.ErrParam)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Name != "Tom" {
|
||||
c.SetPayload(errno.ErrUser)
|
||||
return
|
||||
}
|
||||
|
||||
body1, err1 := httpclient.Get("http://127.0.0.1:9999/demo/get/"+req.Name, nil,
|
||||
httpclient.WithTTL(time.Second*2),
|
||||
httpclient.WithJournal(c.Journal()),
|
||||
httpclient.WithLogger(c.Logger()),
|
||||
@@ -114,8 +138,14 @@ func (d *Demo) User() core.HandlerFunc {
|
||||
}
|
||||
|
||||
data := &response{
|
||||
{Name: jsonparse.Get(string(body1), "data.name").(string)},
|
||||
{Name: jsonparse.Get(string(body2), "data.name").(string)},
|
||||
{
|
||||
Name: jsonparse.Get(string(body1), "data.name").(string),
|
||||
Job: jsonparse.Get(string(body1), "data.job").(string),
|
||||
},
|
||||
{
|
||||
Name: jsonparse.Get(string(body2), "data.name").(string),
|
||||
Job: jsonparse.Get(string(body2), "data.job").(string),
|
||||
},
|
||||
}
|
||||
|
||||
c.SetPayload(errno.OK.WithData(data))
|
||||
@@ -123,7 +153,6 @@ func (d *Demo) User() core.HandlerFunc {
|
||||
}
|
||||
|
||||
func (d *Demo) RsaTest() core.HandlerFunc {
|
||||
|
||||
return func(c core.Context) {
|
||||
startTime := time.Now()
|
||||
encryptStr := "param_1=xxx¶m_2=xxx&ak=xxx&ts=1111111111"
|
||||
|
||||
@@ -18,7 +18,6 @@ func NewHTTPMux(logger *zap.Logger) (core.Mux, error) {
|
||||
mux, err := core.New(logger,
|
||||
core.WithEnableCors(),
|
||||
core.WithEnableRate(),
|
||||
core.WithDisablePProf(),
|
||||
core.WithPanicNotify(notify.Email),
|
||||
)
|
||||
|
||||
@@ -33,7 +32,7 @@ func NewHTTPMux(logger *zap.Logger) (core.Mux, error) {
|
||||
|
||||
d := mux.Group("/demo")
|
||||
{
|
||||
d.GET("user", demoHandler.User())
|
||||
d.GET("user/:name", demoHandler.User())
|
||||
|
||||
// 模拟数据
|
||||
d.GET("get/:name", demoHandler.Get(), core.DisableJournal)
|
||||
|
||||
@@ -28,7 +28,6 @@ type Config struct {
|
||||
func init() {
|
||||
viper.SetConfigName(env.Active().Value() + "_configs")
|
||||
viper.SetConfigType("toml")
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath("./configs")
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
_ "github.com/xinliangnote/go-gin-api/docs"
|
||||
"github.com/xinliangnote/go-gin-api/internal/pkg/errno"
|
||||
"github.com/xinliangnote/go-gin-api/internal/pkg/journal"
|
||||
"github.com/xinliangnote/go-gin-api/pkg/color"
|
||||
@@ -16,6 +17,8 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
cors "github.com/rs/cors/wrapper/gin"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"github.com/swaggo/gin-swagger/swaggerFiles"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
@@ -35,6 +38,7 @@ type Option func(*option)
|
||||
|
||||
type option struct {
|
||||
disablePProf bool
|
||||
disableSwagger bool
|
||||
disablePrometheus bool
|
||||
panicNotify OnPanicNotify
|
||||
enableCors bool
|
||||
@@ -51,6 +55,13 @@ func WithDisablePProf() Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithDisableSwagger 禁用 swagger
|
||||
func WithDisableSwagger() Option {
|
||||
return func(opt *option) {
|
||||
opt.disableSwagger = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithDisableproPrometheus 禁用prometheus
|
||||
func WithDisableproPrometheus() Option {
|
||||
return func(opt *option) {
|
||||
@@ -224,6 +235,10 @@ func New(logger *zap.Logger, options ...Option) (Mux, error) {
|
||||
pprof.Register(engine) // register pprof to gin
|
||||
}
|
||||
|
||||
if !opt.disableSwagger {
|
||||
mux.engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // register swagger
|
||||
}
|
||||
|
||||
if !opt.disablePrometheus {
|
||||
mux.engine.GET("/metrics", gin.WrapH(promhttp.Handler())) // register prometheus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user