From 01786c5e635feb6bae5b570be639f9ac40507630 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Wed, 3 Nov 2021 20:57:03 +0800 Subject: [PATCH] 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 --- tools/goctl/api/dartgen/gen.go | 1 + tools/goctl/api/docgen/gen.go | 11 ++++++----- tools/goctl/api/gogen/genroutes.go | 15 +++++++++++++-- tools/goctl/api/javagen/gen.go | 1 + tools/goctl/api/ktgen/cmd.go | 1 + tools/goctl/api/spec/fn.go | 18 ++++++++++++++++++ tools/goctl/api/spec/spec.go | 2 ++ tools/goctl/api/tsgen/gen.go | 1 + 8 files changed, 43 insertions(+), 7 deletions(-) diff --git a/tools/goctl/api/dartgen/gen.go b/tools/goctl/api/dartgen/gen.go index f2bbc8dc..972e02c1 100644 --- a/tools/goctl/api/dartgen/gen.go +++ b/tools/goctl/api/dartgen/gen.go @@ -25,6 +25,7 @@ func DartCommand(c *cli.Context) error { return err } + api.Service = api.Service.JoinPrefix() if !strings.HasSuffix(dir, "/") { dir = dir + "/" } diff --git a/tools/goctl/api/docgen/gen.go b/tools/goctl/api/docgen/gen.go index f8ca43e8..09f23de1 100644 --- a/tools/goctl/api/docgen/gen.go +++ b/tools/goctl/api/docgen/gen.go @@ -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 } diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index 531c5426..8b273b8f 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -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) } diff --git a/tools/goctl/api/javagen/gen.go b/tools/goctl/api/javagen/gen.go index 2152d373..83ca6209 100644 --- a/tools/goctl/api/javagen/gen.go +++ b/tools/goctl/api/javagen/gen.go @@ -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)) diff --git a/tools/goctl/api/ktgen/cmd.go b/tools/goctl/api/ktgen/cmd.go index 88adeb7c..39e20fcf 100644 --- a/tools/goctl/api/ktgen/cmd.go +++ b/tools/goctl/api/ktgen/cmd.go @@ -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 diff --git a/tools/goctl/api/spec/fn.go b/tools/goctl/api/spec/fn.go index ef533178..54b648c1 100644 --- a/tools/goctl/api/spec/fn.go +++ b/tools/goctl/api/spec/fn.go @@ -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 diff --git a/tools/goctl/api/spec/spec.go b/tools/goctl/api/spec/spec.go index 1399b6b0..199e2251 100644 --- a/tools/goctl/api/spec/spec.go +++ b/tools/goctl/api/spec/spec.go @@ -1,5 +1,7 @@ package spec +const RoutePrefixKey = "prefix" + type ( // Doc describes document Doc []string diff --git a/tools/goctl/api/tsgen/gen.go b/tools/goctl/api/tsgen/gen.go index 01e2d395..950e9265 100644 --- a/tools/goctl/api/tsgen/gen.go +++ b/tools/goctl/api/tsgen/gen.go @@ -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))