Generate route with prefix (#1200)
* Generate route with prefix * Update api convert * Remove TrimSpace * Update path join * Format code * Format code Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
@@ -25,6 +25,7 @@ func DartCommand(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
api.Service = api.Service.JoinPrefix()
|
||||||
if !strings.HasSuffix(dir, "/") {
|
if !strings.HasSuffix(dir, "/") {
|
||||||
dir = dir + "/"
|
dir = dir + "/"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,14 +42,15 @@ func DocCommand(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range files {
|
for _, p := range files {
|
||||||
api, err := parser.Parse(path)
|
api, err := parser.Parse(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse file: %s, err: %s", path, err.Error())
|
return fmt.Errorf("parse file: %s, err: %s", p, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
err = genDoc(api, filepath.Dir(filepath.Join(outputDir, path[len(dir):])),
|
api.Service = api.Service.JoinPrefix()
|
||||||
strings.Replace(path[len(filepath.Dir(path)):], ".api", ".md", 1))
|
err = genDoc(api, filepath.Dir(filepath.Join(outputDir, p[len(dir):])),
|
||||||
|
strings.Replace(p[len(filepath.Dir(p)):], ".api", ".md", 1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
|
|||||||
`
|
`
|
||||||
routesAdditionTemplate = `
|
routesAdditionTemplate = `
|
||||||
engine.AddRoutes(
|
engine.AddRoutes(
|
||||||
{{.routes}} {{.jwt}}{{.signature}}
|
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}}
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
@@ -54,6 +54,7 @@ type (
|
|||||||
signatureEnabled bool
|
signatureEnabled bool
|
||||||
authName string
|
authName string
|
||||||
middlewares []string
|
middlewares []string
|
||||||
|
prefix string
|
||||||
}
|
}
|
||||||
route struct {
|
route struct {
|
||||||
method string
|
method string
|
||||||
@@ -87,10 +88,14 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
|
|||||||
if g.jwtEnabled {
|
if g.jwtEnabled {
|
||||||
jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.Config.%s.AccessSecret),", g.authName)
|
jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.Config.%s.AccessSecret),", g.authName)
|
||||||
}
|
}
|
||||||
var signature string
|
var signature, prefix string
|
||||||
if g.signatureEnabled {
|
if g.signatureEnabled {
|
||||||
signature = "\n rest.WithSignature(serverCtx.Config.Signature),"
|
signature = "\n rest.WithSignature(serverCtx.Config.Signature),"
|
||||||
}
|
}
|
||||||
|
if len(g.prefix) > 0 {
|
||||||
|
prefix = fmt.Sprintf(`
|
||||||
|
rest.WithPrefix("%s"),`, g.prefix)
|
||||||
|
}
|
||||||
|
|
||||||
var routes string
|
var routes string
|
||||||
if len(g.middlewares) > 0 {
|
if len(g.middlewares) > 0 {
|
||||||
@@ -111,6 +116,7 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
|
|||||||
"routes": routes,
|
"routes": routes,
|
||||||
"jwt": jwt,
|
"jwt": jwt,
|
||||||
"signature": signature,
|
"signature": signature,
|
||||||
|
"prefix": prefix,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -200,6 +206,11 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
|
|||||||
groupedRoutes.middlewares = append(groupedRoutes.middlewares,
|
groupedRoutes.middlewares = append(groupedRoutes.middlewares,
|
||||||
strings.Split(middleware, ",")...)
|
strings.Split(middleware, ",")...)
|
||||||
}
|
}
|
||||||
|
prefix := g.GetAnnotation(spec.RoutePrefixKey)
|
||||||
|
prefix = strings.TrimSpace(prefix)
|
||||||
|
prefix = strings.ReplaceAll(prefix, `"`, "")
|
||||||
|
prefix = path.Join("/", prefix)
|
||||||
|
groupedRoutes.prefix = prefix
|
||||||
routes = append(routes, groupedRoutes)
|
routes = append(routes, groupedRoutes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ func JavaCommand(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
api.Service = api.Service.JoinPrefix()
|
||||||
packetName := strings.TrimSuffix(api.Service.Name, "-api")
|
packetName := strings.TrimSuffix(api.Service.Name, "-api")
|
||||||
logx.Must(util.MkdirIfNotExist(dir))
|
logx.Must(util.MkdirIfNotExist(dir))
|
||||||
logx.Must(genPacket(dir, packetName, api))
|
logx.Must(genPacket(dir, packetName, api))
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ func KtCommand(c *cli.Context) error {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
api.Service = api.Service.JoinPrefix()
|
||||||
e = genBase(dir, pkg, api)
|
e = genBase(dir, pkg, api)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return e
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package spec
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/stringx"
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
@@ -17,6 +18,23 @@ const (
|
|||||||
|
|
||||||
var definedKeys = []string{bodyTagKey, formTagKey, pathTagKey}
|
var definedKeys = []string{bodyTagKey, formTagKey, pathTagKey}
|
||||||
|
|
||||||
|
func (s Service) JoinPrefix() Service {
|
||||||
|
var groups []Group
|
||||||
|
for _, g := range s.Groups {
|
||||||
|
prefix := strings.TrimSpace(g.GetAnnotation(RoutePrefixKey))
|
||||||
|
prefix = strings.ReplaceAll(prefix, `"`, "")
|
||||||
|
var routes []Route
|
||||||
|
for _, r := range g.Routes {
|
||||||
|
r.Path = path.Join("/", prefix, r.Path)
|
||||||
|
routes = append(routes, r)
|
||||||
|
}
|
||||||
|
g.Routes = routes
|
||||||
|
groups = append(groups, g)
|
||||||
|
}
|
||||||
|
s.Groups = groups
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
// Routes returns all routes in api service
|
// Routes returns all routes in api service
|
||||||
func (s Service) Routes() []Route {
|
func (s Service) Routes() []Route {
|
||||||
var result []Route
|
var result []Route
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package spec
|
package spec
|
||||||
|
|
||||||
|
const RoutePrefixKey = "prefix"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Doc describes document
|
// Doc describes document
|
||||||
Doc []string
|
Doc []string
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ func TsCommand(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
api.Service = api.Service.JoinPrefix()
|
||||||
logx.Must(util.MkdirIfNotExist(dir))
|
logx.Must(util.MkdirIfNotExist(dir))
|
||||||
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
|
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
|
||||||
logx.Must(genComponents(dir, api))
|
logx.Must(genComponents(dir, api))
|
||||||
|
|||||||
Reference in New Issue
Block a user