Added support for setting the parameter size accepted by the interface and custom timeout and maxbytes in API file (#1713)

* Added support for setting the parameter size accepted by the interface

* support custom timeout and maxbytes in API file

* support timeout used unit

* remove goctl maxBytes
This commit is contained in:
Xiaoju Jiang
2022-03-31 20:20:00 +08:00
committed by GitHub
parent 500bd87c85
commit 321dc2d410
5 changed files with 74 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import (
"sort"
"strings"
"text/template"
"time"
"github.com/zeromicro/go-zero/core/collection"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
@@ -34,7 +35,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
`
routesAdditionTemplate = `
server.AddRoutes(
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}}
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}}
)
`
)
@@ -57,6 +58,8 @@ type (
jwtEnabled bool
signatureEnabled bool
authName string
timeout string
timeoutEnable bool
middlewares []string
prefix string
jwtTrans string
@@ -110,6 +113,15 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
rest.WithPrefix("%s"),`, g.prefix)
}
var timeout string
if g.timeoutEnable {
duration, err := time.ParseDuration(g.timeout)
if err != nil {
panic(err)
}
timeout = fmt.Sprintf("rest.WithTimeout(%d),", duration)
}
var routes string
if len(g.middlewares) > 0 {
gbuilder.WriteString("\n}...,")
@@ -130,6 +142,7 @@ rest.WithPrefix("%s"),`, g.prefix)
"jwt": jwt,
"signature": signature,
"prefix": prefix,
"timeout": timeout,
}); err != nil {
return err
}
@@ -205,6 +218,13 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
})
}
timeout := g.GetAnnotation("timeout")
if len(timeout) > 0 {
groupedRoutes.timeoutEnable = true
groupedRoutes.timeout = timeout
}
jwt := g.GetAnnotation("jwt")
if len(jwt) > 0 {
groupedRoutes.authName = jwt