Skip to content

Commit

Permalink
support custom maxBytes in API file
Browse files Browse the repository at this point in the history
  • Loading branch information
jiang4869 committed Jan 26, 2023
1 parent ab9eeff commit a12ff89
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"sort"
"strconv"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -36,7 +37,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
`
routesAdditionTemplate = `
server.AddRoutes(
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}}
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}} {{.maxBytes}}
)
`
timeoutThreshold = time.Millisecond
Expand Down Expand Up @@ -64,6 +65,7 @@ type (
middlewares []string
prefix string
jwtTrans string
maxBytes string
}
route struct {
method string
Expand Down Expand Up @@ -127,10 +129,20 @@ rest.WithPrefix("%s"),`, g.prefix)
return fmt.Errorf("timeout should not less than 1ms, now %v", duration)
}

timeout = fmt.Sprintf("rest.WithTimeout(%d * time.Millisecond),", duration/time.Millisecond)
timeout = fmt.Sprintf("\n rest.WithTimeout(%d * time.Millisecond),", duration/time.Millisecond)
hasTimeout = true
}

var maxBytes string
if len(g.maxBytes) > 0 {
_, err := strconv.ParseInt(g.maxBytes, 10, 64)
if err != nil {
return fmt.Errorf("maxBytes %s parse error,it is an invalid number", g.maxBytes)
}

maxBytes = fmt.Sprintf("\n rest.WithMaxBytes(%s),", g.maxBytes)
}

var routes string
if len(g.middlewares) > 0 {
gbuilder.WriteString("\n}...,")
Expand All @@ -152,6 +164,7 @@ rest.WithPrefix("%s"),`, g.prefix)
"signature": signature,
"prefix": prefix,
"timeout": timeout,
"maxBytes": maxBytes,
}); err != nil {
return err
}
Expand Down Expand Up @@ -230,6 +243,7 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
}

groupedRoutes.timeout = g.GetAnnotation("timeout")
groupedRoutes.maxBytes = g.GetAnnotation("maxBytes")

jwt := g.GetAnnotation("jwt")
if len(jwt) > 0 {
Expand Down

0 comments on commit a12ff89

Please sign in to comment.