grpc.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package http
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. "go-common/app/admin/ep/melloi/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/binding"
  10. )
  11. func getProto(c *bm.Context) {
  12. v := new(struct {
  13. Path string `form:"path"`
  14. })
  15. if err := c.Bind(v); err != nil {
  16. return
  17. }
  18. path, fileName := filepath.Split(v.Path)
  19. var (
  20. res = make(map[string]interface{})
  21. err error
  22. )
  23. if res, err = srv.ProtoParsing(path, fileName); err != nil {
  24. log.Error("parser grpc error(%v)", err)
  25. c.JSON(nil, err)
  26. return
  27. }
  28. if err = srv.CreateGRPCImportDir(res, path); err != nil {
  29. c.JSON(nil, err)
  30. return
  31. }
  32. c.JSON(res, err)
  33. }
  34. func createDependencyPath(c *bm.Context) {
  35. protoPath := &model.ProtoPathModel{}
  36. if err := c.BindWith(protoPath, binding.JSON); err != nil {
  37. c.JSON(nil, ecode.RequestErr)
  38. return
  39. }
  40. c.JSON(nil, srv.CreateProtoImportDir(protoPath))
  41. }
  42. func grpcQuickStart(c *bm.Context) {
  43. var (
  44. userName *http.Cookie
  45. qsReq = &model.GRPCQuickStartRequest{}
  46. err error
  47. cookie string
  48. )
  49. cookie = c.Request.Header.Get("Cookie")
  50. if err = c.BindWith(qsReq, binding.JSON); err != nil {
  51. c.JSON(nil, err)
  52. return
  53. }
  54. if userName, err = c.Request.Cookie("username"); err != nil {
  55. c.JSON(nil, ecode.AccessKeyErr)
  56. return
  57. }
  58. c.JSON(srv.GRPCQuickStart(c, qsReq, userName.Value, cookie))
  59. }
  60. func saveGrpc(c *bm.Context) {
  61. var (
  62. qsReq = &model.GRPCQuickStartRequest{}
  63. err error
  64. )
  65. if err = c.BindWith(qsReq, binding.JSON); err != nil {
  66. c.JSON(nil, err)
  67. return
  68. }
  69. c.JSON(nil, srv.SaveGRPCQuickStart(c, qsReq))
  70. }
  71. func runGrpc(c *bm.Context) {
  72. var (
  73. grpc = model.GRPC{}
  74. err error
  75. userName *http.Cookie
  76. cookie string
  77. )
  78. cookie = c.Request.Header.Get("Cookie")
  79. if err = c.BindWith(&grpc, binding.JSON); nil != err {
  80. c.JSON(nil, err)
  81. return
  82. }
  83. if userName, err = c.Request.Cookie("username"); err != nil {
  84. c.JSON(nil, ecode.AccessKeyErr)
  85. return
  86. }
  87. c.JSON(srv.GRPCRunByModel(c, &grpc, userName.Value, cookie))
  88. }
  89. func runGrpcByScriptId(c *bm.Context) {
  90. var (
  91. userName *http.Cookie
  92. err error
  93. grpcExeReq = &model.GRPCExecuteScriptRequest{}
  94. cookie string
  95. )
  96. cookie = c.Request.Header.Get("Cookie")
  97. if err = c.BindWith(grpcExeReq, binding.JSON); err != nil {
  98. return
  99. }
  100. if userName, err = c.Request.Cookie("username"); err != nil {
  101. c.JSON(nil, ecode.AccessKeyErr)
  102. return
  103. }
  104. c.JSON(srv.GRPCRunByScriptID(c, grpcExeReq, userName.Value, cookie))
  105. }
  106. func grpcAddScript(c *bm.Context) {
  107. grpcReq := model.GRPCAddScriptRequest{}
  108. if err := c.BindWith(&grpcReq, binding.JSON); err != nil {
  109. c.JSON(nil, err)
  110. return
  111. }
  112. c.JSON(srv.GRPCAddScript(c, &grpcReq))
  113. }
  114. func queryGrpc(c *bm.Context) {
  115. qgr := model.QueryGRPCRequest{}
  116. if err := c.BindWith(&qgr, binding.Form); err != nil {
  117. c.JSON(nil, err)
  118. }
  119. if err := qgr.Verify(); err != nil {
  120. c.JSON(nil, err)
  121. return
  122. }
  123. sessionID, err := c.Request.Cookie("_AJSESSIONID")
  124. if err != nil {
  125. c.JSON(nil, err)
  126. return
  127. }
  128. c.JSON(srv.QueryGrpc(c, sessionID.Value, &qgr))
  129. }
  130. func deleteGrpc(c *bm.Context) {
  131. v := new(struct {
  132. ID int `form:"id"`
  133. })
  134. if err := c.Bind(v); err != nil {
  135. c.JSON(nil, ecode.RequestErr)
  136. return
  137. }
  138. c.JSON(nil, srv.DeleteGrpc(v.ID))
  139. }
  140. func updateGrpc(c *bm.Context) {
  141. grpc := model.GRPC{}
  142. if err := c.BindWith(&grpc, binding.JSON); err != nil {
  143. c.JSON(nil, err)
  144. return
  145. }
  146. qgr := &model.GRPCAddScriptRequest{}
  147. qgr.ThreadsSum = grpc.ThreadsSum
  148. qgr.RampUp = grpc.RampUp
  149. qgr.Loops = grpc.Loops
  150. qgr.LoadTime = grpc.LoadTime
  151. qgr.HostName = grpc.HostName
  152. qgr.Port = grpc.Port
  153. qgr.ServiceName = grpc.ServiceName
  154. qgr.ProtoClassName = grpc.ProtoClassName
  155. qgr.PkgPath = grpc.PkgPath
  156. qgr.RequestType = grpc.RequestType
  157. qgr.ResponseType = grpc.ResponseType
  158. qgr.ScriptPath = grpc.ScriptPath
  159. qgr.RequestMethod = grpc.RequestMethod
  160. qgr.RequestContent = grpc.RequestContent
  161. qgr.TaskName = grpc.TaskName
  162. qgr.ParamFilePath = grpc.ParamFilePath
  163. qgr.ParamNames = grpc.ParamNames
  164. qgr.ParamDelimiter = grpc.ParamDelimiter
  165. g, err := srv.CreateJmx(c, qgr)
  166. if err != nil {
  167. c.JSON(nil, err)
  168. return
  169. }
  170. grpc.JmxPath = g.JmxPath
  171. grpc.JtlLog = g.JtlLog
  172. grpc.JmxLog = g.JmxLog
  173. c.JSON(nil, srv.UpdateGrpc(&grpc))
  174. }
  175. func queryGrpcSnap(c *bm.Context) {
  176. v := new(struct {
  177. ID int `form:"id"`
  178. })
  179. if err := c.Bind(v); err != nil {
  180. c.JSON(nil, ecode.RequestErr)
  181. return
  182. }
  183. c.JSON(srv.QueryGRPCSnapByID(v.ID))
  184. }