support custom maxBytes in API file (#2822)

master
Xiaoju Jiang 2 years ago committed by GitHub
parent 35b9568657
commit 413ee919e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ import (
"os" "os"
"path" "path"
"sort" "sort"
"strconv"
"strings" "strings"
"text/template" "text/template"
"time" "time"
@ -36,7 +37,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
` `
routesAdditionTemplate = ` routesAdditionTemplate = `
server.AddRoutes( server.AddRoutes(
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}} {{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}} {{.maxBytes}}
) )
` `
timeoutThreshold = time.Millisecond timeoutThreshold = time.Millisecond
@ -64,6 +65,7 @@ type (
middlewares []string middlewares []string
prefix string prefix string
jwtTrans string jwtTrans string
maxBytes string
} }
route struct { route struct {
method string method string
@ -127,10 +129,20 @@ rest.WithPrefix("%s"),`, g.prefix)
return fmt.Errorf("timeout should not less than 1ms, now %v", duration) 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 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 var routes string
if len(g.middlewares) > 0 { if len(g.middlewares) > 0 {
gbuilder.WriteString("\n}...,") gbuilder.WriteString("\n}...,")
@ -152,6 +164,7 @@ rest.WithPrefix("%s"),`, g.prefix)
"signature": signature, "signature": signature,
"prefix": prefix, "prefix": prefix,
"timeout": timeout, "timeout": timeout,
"maxBytes": maxBytes,
}); err != nil { }); err != nil {
return err return err
} }
@ -230,6 +243,7 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
} }
groupedRoutes.timeout = g.GetAnnotation("timeout") groupedRoutes.timeout = g.GetAnnotation("timeout")
groupedRoutes.maxBytes = g.GetAnnotation("maxBytes")
jwt := g.GetAnnotation("jwt") jwt := g.GetAnnotation("jwt")
if len(jwt) > 0 { if len(jwt) > 0 {

Loading…
Cancel
Save