genhandlers.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package gogen
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  7. "github.com/tal-tech/go-zero/tools/goctl/config"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  10. "github.com/tal-tech/go-zero/tools/goctl/vars"
  11. )
  12. const handlerTemplate = `package handler
  13. import (
  14. "net/http"
  15. {{.ImportPackages}}
  16. )
  17. func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
  18. return func(w http.ResponseWriter, r *http.Request) {
  19. {{if .HasRequest}}var req types.{{.RequestType}}
  20. if err := httpx.Parse(r, &req); err != nil {
  21. httpx.Error(w, err)
  22. return
  23. }{{end}}
  24. l := logic.New{{.LogicType}}(r.Context(), ctx)
  25. {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}req{{end}})
  26. if err != nil {
  27. httpx.Error(w, err)
  28. } else {
  29. {{if .HasResp}}httpx.OkJson(w, resp){{else}}httpx.Ok(w){{end}}
  30. }
  31. }
  32. }
  33. `
  34. type handlerInfo struct {
  35. ImportPackages string
  36. HandlerName string
  37. RequestType string
  38. LogicType string
  39. Call string
  40. HasResp bool
  41. HasRequest bool
  42. }
  43. func genHandler(dir string, cfg *config.Config, group spec.Group, route spec.Route) error {
  44. handler := getHandlerName(route)
  45. if getHandlerFolderPath(group, route) != handlerDir {
  46. handler = strings.Title(handler)
  47. }
  48. parentPkg, err := getParentPackage(dir)
  49. if err != nil {
  50. return err
  51. }
  52. return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
  53. ImportPackages: genHandlerImports(group, route, parentPkg),
  54. HandlerName: handler,
  55. RequestType: util.Title(route.RequestTypeName()),
  56. LogicType: strings.Title(getLogicName(route)),
  57. Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
  58. HasResp: len(route.ResponseTypeName()) > 0,
  59. HasRequest: len(route.RequestTypeName()) > 0,
  60. })
  61. }
  62. func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
  63. route spec.Route, handleObj handlerInfo) error {
  64. filename, err := format.FileNamingFormat(cfg.NamingFormat, handler)
  65. if err != nil {
  66. return err
  67. }
  68. return genFile(fileGenConfig{
  69. dir: dir,
  70. subdir: getHandlerFolderPath(group, route),
  71. filename: filename + ".go",
  72. templateName: "handlerTemplate",
  73. category: category,
  74. templateFile: handlerTemplateFile,
  75. builtinTemplate: handlerTemplate,
  76. data: handleObj,
  77. })
  78. }
  79. func genHandlers(dir string, cfg *config.Config, api *spec.ApiSpec) error {
  80. for _, group := range api.Service.Groups {
  81. for _, route := range group.Routes {
  82. if err := genHandler(dir, cfg, group, route); err != nil {
  83. return err
  84. }
  85. }
  86. }
  87. return nil
  88. }
  89. func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
  90. var imports []string
  91. imports = append(imports, fmt.Sprintf("\"%s\"",
  92. util.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
  93. imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
  94. if len(route.RequestTypeName()) > 0 {
  95. imports = append(imports, fmt.Sprintf("\"%s\"\n", util.JoinPackages(parentPkg, typesDir)))
  96. }
  97. imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL))
  98. return strings.Join(imports, "\n\t")
  99. }
  100. func getHandlerBaseName(route spec.Route) (string, error) {
  101. handler := route.Handler
  102. handler = strings.TrimSpace(handler)
  103. handler = strings.TrimSuffix(handler, "handler")
  104. handler = strings.TrimSuffix(handler, "Handler")
  105. return handler, nil
  106. }
  107. func getHandlerFolderPath(group spec.Group, route spec.Route) string {
  108. folder := route.GetAnnotation(groupProperty)
  109. if len(folder) == 0 {
  110. folder = group.GetAnnotation(groupProperty)
  111. if len(folder) == 0 {
  112. return handlerDir
  113. }
  114. }
  115. folder = strings.TrimPrefix(folder, "/")
  116. folder = strings.TrimSuffix(folder, "/")
  117. return path.Join(handlerDir, folder)
  118. }
  119. func getHandlerName(route spec.Route) string {
  120. handler, err := getHandlerBaseName(route)
  121. if err != nil {
  122. panic(err)
  123. }
  124. return handler + "Handler"
  125. }
  126. func getLogicName(route spec.Route) string {
  127. handler, err := getHandlerBaseName(route)
  128. if err != nil {
  129. panic(err)
  130. }
  131. return handler + "Logic"
  132. }