genhandlers.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package gogen
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/api/spec"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/config"
  8. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  9. "git.i2edu.net/i2/go-zero/tools/goctl/util/format"
  10. "git.i2edu.net/i2/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, rootPkg 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. return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
  49. ImportPackages: genHandlerImports(group, route, rootPkg),
  50. HandlerName: handler,
  51. RequestType: util.Title(route.RequestTypeName()),
  52. LogicType: strings.Title(getLogicName(route)),
  53. Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
  54. HasResp: len(route.ResponseTypeName()) > 0,
  55. HasRequest: len(route.RequestTypeName()) > 0,
  56. })
  57. }
  58. func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
  59. route spec.Route, handleObj handlerInfo) error {
  60. filename, err := format.FileNamingFormat(cfg.NamingFormat, handler)
  61. if err != nil {
  62. return err
  63. }
  64. return genFile(fileGenConfig{
  65. dir: dir,
  66. subdir: getHandlerFolderPath(group, route),
  67. filename: filename + ".go",
  68. templateName: "handlerTemplate",
  69. category: category,
  70. templateFile: handlerTemplateFile,
  71. builtinTemplate: handlerTemplate,
  72. data: handleObj,
  73. })
  74. }
  75. func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
  76. for _, group := range api.Service.Groups {
  77. for _, route := range group.Routes {
  78. if err := genHandler(dir, rootPkg, cfg, group, route); err != nil {
  79. return err
  80. }
  81. }
  82. }
  83. return nil
  84. }
  85. func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
  86. var imports []string
  87. imports = append(imports, fmt.Sprintf("\"%s\"",
  88. util.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
  89. imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
  90. if len(route.RequestTypeName()) > 0 {
  91. imports = append(imports, fmt.Sprintf("\"%s\"\n", util.JoinPackages(parentPkg, typesDir)))
  92. }
  93. imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL))
  94. return strings.Join(imports, "\n\t")
  95. }
  96. func getHandlerBaseName(route spec.Route) (string, error) {
  97. handler := route.Handler
  98. handler = strings.TrimSpace(handler)
  99. handler = strings.TrimSuffix(handler, "handler")
  100. handler = strings.TrimSuffix(handler, "Handler")
  101. return handler, nil
  102. }
  103. func getHandlerFolderPath(group spec.Group, route spec.Route) string {
  104. folder := route.GetAnnotation(groupProperty)
  105. if len(folder) == 0 {
  106. folder = group.GetAnnotation(groupProperty)
  107. if len(folder) == 0 {
  108. return handlerDir
  109. }
  110. }
  111. folder = strings.TrimPrefix(folder, "/")
  112. folder = strings.TrimSuffix(folder, "/")
  113. return path.Join(handlerDir, folder)
  114. }
  115. func getHandlerName(route spec.Route) string {
  116. handler, err := getHandlerBaseName(route)
  117. if err != nil {
  118. panic(err)
  119. }
  120. return handler + "Handler"
  121. }
  122. func getLogicName(route spec.Route) string {
  123. handler, err := getHandlerBaseName(route)
  124. if err != nil {
  125. panic(err)
  126. }
  127. return handler + "Logic"
  128. }