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
|
||||
}
|
||||
|
||||
api.Service = api.Service.JoinPrefix()
|
||||
if !strings.HasSuffix(dir, "/") {
|
||||
dir = dir + "/"
|
||||
}
|
||||
|
||||
@@ -42,14 +42,15 @@ func DocCommand(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, path := range files {
|
||||
api, err := parser.Parse(path)
|
||||
for _, p := range files {
|
||||
api, err := parser.Parse(p)
|
||||
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):])),
|
||||
strings.Replace(path[len(filepath.Dir(path)):], ".api", ".md", 1))
|
||||
api.Service = api.Service.JoinPrefix()
|
||||
err = genDoc(api, filepath.Dir(filepath.Join(outputDir, p[len(dir):])),
|
||||
strings.Replace(p[len(filepath.Dir(p)):], ".api", ".md", 1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
`
|
||||
routesAdditionTemplate = `
|
||||
engine.AddRoutes(
|
||||
{{.routes}} {{.jwt}}{{.signature}}
|
||||
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}}
|
||||
)
|
||||
`
|
||||
)
|
||||
@@ -54,6 +54,7 @@ type (
|
||||
signatureEnabled bool
|
||||
authName string
|
||||
middlewares []string
|
||||
prefix string
|
||||
}
|
||||
route struct {
|
||||
method string
|
||||
@@ -87,10 +88,14 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
|
||||
if g.jwtEnabled {
|
||||
jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.Config.%s.AccessSecret),", g.authName)
|
||||
}
|
||||
var signature string
|
||||
var signature, prefix string
|
||||
if g.signatureEnabled {
|
||||
signature = "\n rest.WithSignature(serverCtx.Config.Signature),"
|
||||
}
|
||||
if len(g.prefix) > 0 {
|
||||
prefix = fmt.Sprintf(`
|
||||
rest.WithPrefix("%s"),`, g.prefix)
|
||||
}
|
||||
|
||||
var routes string
|
||||
if len(g.middlewares) > 0 {
|
||||
@@ -111,6 +116,7 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
|
||||
"routes": routes,
|
||||
"jwt": jwt,
|
||||
"signature": signature,
|
||||
"prefix": prefix,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -200,6 +206,11 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
|
||||
groupedRoutes.middlewares = append(groupedRoutes.middlewares,
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ func JavaCommand(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
api.Service = api.Service.JoinPrefix()
|
||||
packetName := strings.TrimSuffix(api.Service.Name, "-api")
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
logx.Must(genPacket(dir, packetName, api))
|
||||
|
||||
@@ -27,6 +27,7 @@ func KtCommand(c *cli.Context) error {
|
||||
return e
|
||||
}
|
||||
|
||||
api.Service = api.Service.JoinPrefix()
|
||||
e = genBase(dir, pkg, api)
|
||||
if e != nil {
|
||||
return e
|
||||
|
||||
@@ -2,6 +2,7 @@ package spec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
@@ -17,6 +18,23 @@ const (
|
||||
|
||||
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
|
||||
func (s Service) Routes() []Route {
|
||||
var result []Route
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package spec
|
||||
|
||||
const RoutePrefixKey = "prefix"
|
||||
|
||||
type (
|
||||
// Doc describes document
|
||||
Doc []string
|
||||
|
||||
@@ -32,6 +32,7 @@ func TsCommand(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
api.Service = api.Service.JoinPrefix()
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
|
||||
logx.Must(genComponents(dir, api))
|
||||
|
||||
Reference in New Issue
Block a user