search.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package api_management
  2. import (
  3. "adm-management/errors"
  4. "adm-management/model"
  5. "adm-management/parser"
  6. v1 "adm-management/pb/v1"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "git.getensh.com/common/gopkgsv2/database"
  11. "git.getensh.com/common/gopkgsv2/logger"
  12. "go.uber.org/zap"
  13. "google.golang.org/grpc/status"
  14. "gorm.io/gorm"
  15. )
  16. func AllAPI(ctx context.Context, req *v1.AllAPIRequest) (reply *v1.AllAPIReply, err error) {
  17. reply = &v1.AllAPIReply{}
  18. // 捕获各个task中的异常并返回给调用者
  19. defer func() {
  20. if r := recover(); r != nil {
  21. err = fmt.Errorf("%+v", r)
  22. e := &status.Status{}
  23. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  24. logger.Error("err",
  25. zap.String("system_err", err.Error()),
  26. zap.Stack("stacktrace"))
  27. }
  28. }
  29. }()
  30. if req.AllApi == "" {
  31. pagination := model.NewPagination(0, 100000, 0).GetLimitOffset()
  32. list, err := model.NewAPIList().List(database.DB().Where("api_no is NOT NULL").Order("api_no"), pagination)
  33. if err != nil && err != gorm.ErrRecordNotFound {
  34. return reply, errors.SystemError
  35. }
  36. if err == gorm.ErrRecordNotFound {
  37. return reply, nil
  38. }
  39. reply.List = make([]string, 0, len(list))
  40. for _, v := range list {
  41. reply.Host = parser.Conf.Rpc.ADMManagement.Host
  42. reply.List = append(reply.List, v.ApiNo)
  43. }
  44. } else {
  45. pagination := model.NewPagination(0, 100000, 0).GetLimitOffset()
  46. list, err := model.NewAPIList().List(database.DB().Where("api_no like ?", "%"+req.AllApi+"%").Order("api_no"), pagination)
  47. if err != nil && err != gorm.ErrRecordNotFound {
  48. return reply, errors.SystemError
  49. }
  50. if err == gorm.ErrRecordNotFound {
  51. return reply, nil
  52. }
  53. reply.List = make([]string, 0, len(list))
  54. for _, v := range list {
  55. reply.Host = parser.Conf.Rpc.ADMManagement.Host
  56. reply.List = append(reply.List, v.ApiNo)
  57. }
  58. }
  59. return reply, nil
  60. }
  61. func AllKey(ctx context.Context, req *v1.AllKeyRequest) (reply *v1.AllKeyReply, err error) {
  62. reply = &v1.AllKeyReply{}
  63. // 捕获各个task中的异常并返回给调用者
  64. defer func() {
  65. if r := recover(); r != nil {
  66. err = fmt.Errorf("%+v", r)
  67. e := &status.Status{}
  68. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  69. logger.Error("err",
  70. zap.String("system_err", err.Error()),
  71. zap.Stack("stacktrace"))
  72. }
  73. }
  74. }()
  75. if req.AllKey == "" {
  76. pagination := model.NewPagination(0, 100000, 0).GetLimitOffset()
  77. list, err := model.NewKeyList().List(database.DB().Where("`key` is NOT NULL"), pagination)
  78. if err != nil && err != gorm.ErrRecordNotFound {
  79. return reply, errors.SystemError
  80. }
  81. if err == gorm.ErrRecordNotFound {
  82. return reply, nil
  83. }
  84. reply.List = make([]string, 0, len(list))
  85. for _, v := range list {
  86. reply.List = append(reply.List, v.Key)
  87. }
  88. }
  89. return reply, nil
  90. }