kbus_http.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // package kbus_http -- шина сообщений поверх HTTP.
  2. package kbus_http
  3. import (
  4. "fmt"
  5. "net/http"
  6. "sync"
  7. "github.com/gofiber/fiber/v3"
  8. mL1 "gitp78su.ipnodns.ru/svi/kern/v4/lev1"
  9. "gitp78su.ipnodns.ru/svi/kern/v4/lev1/comp_spec"
  10. "gitp78su.ipnodns.ru/svi/kern/v4/lev1/safe_bool"
  11. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kctx"
  12. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kserv_http"
  13. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/bus_ent"
  14. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/bus_ent/topic"
  15. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/bus_mock/mock_hand_sub_http"
  16. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/bus_spec"
  17. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/dot"
  18. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/kbus_base"
  19. )
  20. // kBusHttp -- шина данных поверх HTTP.
  21. type kBusHttp struct {
  22. *kbus_base.KBusBase
  23. log comp_spec.ILogBuf
  24. }
  25. var (
  26. Bus_ *kBusHttp
  27. block sync.Mutex
  28. )
  29. // GetKernelBusHttp -- возвращает шину HTTP.
  30. func GetKernelBusHttp() bus_spec.IKernelBus {
  31. block.Lock()
  32. defer block.Unlock()
  33. if Bus_ != nil {
  34. return Bus_
  35. }
  36. paramLogBuf := &mL1.LogBufParam{
  37. IsTerm_: safe_bool.NewSafeBool(true),
  38. Prefix_: "kBusHttp",
  39. }
  40. log := mL1.NewLogBuf(paramLogBuf)
  41. log.Debug("GetKernelBusHttp(): new")
  42. kCtx := kctx.GetKernelCtx()
  43. kBus := kbus_base.GetKernelBusBase()
  44. sf := &kBusHttp{
  45. KBusBase: kBus,
  46. log: log,
  47. }
  48. kServHttp := kserv_http.GetKernelServHttp()
  49. fibApp := kServHttp.Fiber()
  50. fibApp.Post("/bus/sub", sf.postSub) // Топик подписки, IN
  51. fibApp.Post("/bus/unsub", sf.postUnsub) // Топик отписки, IN
  52. fibApp.Post("/bus/request", sf.postSendRequest) // Топик входящих запросов, IN
  53. fibApp.Post("/bus/pub", sf.postPublish) // Топик публикаций подписки, IN
  54. kCtx.Set("kernBus", sf, "GetKernelBusHttp(): http data bus")
  55. Bus_ = sf
  56. return Bus_
  57. }
  58. // Входящий запрос HTTP на подписку.
  59. func (sf *kBusHttp) postSub(ctx fiber.Ctx) error {
  60. sf.log.Debug("postSub()")
  61. ctx.Set("Content-type", "text/html; charset=utf8")
  62. ctx.Set("Content-type", "text/json")
  63. ctx.Set("Cache-Control", "no-cache")
  64. sf.log.Debug("postSub()")
  65. req := &dot.SubscribeReq{}
  66. err := ctx.Bind().Body(req)
  67. if err != nil {
  68. resp := &dot.SubscribeResp{
  69. Status_: fmt.Sprintf("kernelBusHttp.postSub(): in parse request, err=\n\t%v\n", err),
  70. Uuid_: req.Uuid_,
  71. }
  72. resp.SelfCheck()
  73. ctx.Response().SetStatusCode(http.StatusBadRequest)
  74. sf.log.Err("postSub(): in body parser, status=%q", resp.Status_)
  75. return ctx.JSON(resp)
  76. }
  77. resp := sf.processSubscribe(req)
  78. resp.SelfCheck()
  79. return ctx.JSON(resp)
  80. }
  81. // Процесс подписки веб-хука.
  82. func (sf *kBusHttp) processSubscribe(req *dot.SubscribeReq) *dot.SubscribeResp {
  83. req.SelfCheck()
  84. topic := bus_ent.ITopicFix(topic.NewTopic(req.Topic_))
  85. handler := mock_hand_sub_http.NewMockHandSubHttp(topic, req.WebHook_)
  86. resp := &dot.SubscribeResp{
  87. Status_: "ok",
  88. Uuid_: req.Uuid_,
  89. Name_: handler.Name().Get(),
  90. }
  91. res := sf.Subscribe(handler)
  92. if res.IsErr() {
  93. resp.Status_ = fmt.Sprintf("kernelBusHttp.processSubscribe(): err=\n\t%v", res.Err())
  94. return resp
  95. }
  96. return resp
  97. }
  98. // Входящая публикация.
  99. func (sf *kBusHttp) postPublish(ctx fiber.Ctx) error {
  100. sf.log.Debug("postPublish()")
  101. ctx.Set("Content-type", "text/html; charset=utf8")
  102. ctx.Set("Content-type", "text/json")
  103. ctx.Set("Cache-Control", "no-cache")
  104. req := &dot.PublishReq{}
  105. err := ctx.Bind().Body(req)
  106. if err != nil {
  107. resp := &dot.PublishResp{
  108. Status_: fmt.Sprintf("kernelBusHttp.postPublish(): in parse request, err=\n\t%v\n", err),
  109. Uuid_: req.Uuid_,
  110. }
  111. resp.SelfCheck()
  112. ctx.Response().SetStatusCode(http.StatusBadRequest)
  113. sf.log.Err("postPublish(): in body parser, status=%v", resp.Status_)
  114. return ctx.JSON(resp)
  115. }
  116. resp := sf.processPublish(req)
  117. resp.SelfCheck()
  118. return ctx.JSON(resp)
  119. }
  120. // Выполняет процесс публикации.
  121. func (sf *kBusHttp) processPublish(req *dot.PublishReq) *dot.PublishResp {
  122. req.SelfCheck()
  123. topic := bus_ent.ITopicFix(topic.NewTopic(req.Topic_))
  124. res := sf.Publish(topic, req.BinMsg_)
  125. resp := &dot.PublishResp{
  126. Status_: "ok",
  127. Uuid_: req.Uuid_,
  128. }
  129. if res.IsErr() {
  130. resp.Status_ = fmt.Sprintf("kernelBusHttp.processPublish(): err=\n\t%v", res.Err())
  131. return resp
  132. }
  133. return resp
  134. }
  135. // Входящий запрос.
  136. func (sf *kBusHttp) postSendRequest(ctx fiber.Ctx) error {
  137. sf.log.Debug("postSendRequest()")
  138. ctx.Set("Content-type", "text/html; charset=utf8")
  139. ctx.Set("Content-type", "text/json")
  140. ctx.Set("Cache-Control", "no-cache")
  141. req := &dot.ServeReq{}
  142. err := ctx.Bind().Body(req)
  143. if err != nil {
  144. resp := &dot.ServeResp{
  145. Status_: fmt.Sprintf("kernelBusHttp.postSendRequest(): err=\n\t%v", err),
  146. Uuid_: req.Uuid_,
  147. }
  148. resp.SelfCheck()
  149. ctx.Response().SetStatusCode(http.StatusBadRequest)
  150. sf.log.Err("postSendRequest(): in body parser, status=%v", resp.Status_)
  151. return ctx.JSON(resp)
  152. }
  153. resp := sf.processSendRequest(req)
  154. resp.SelfCheck()
  155. return ctx.JSON(resp)
  156. }
  157. // Обрабатывает входящий запрос.
  158. func (sf *kBusHttp) processSendRequest(req *dot.ServeReq) *dot.ServeResp {
  159. req.SelfCheck()
  160. topic := bus_ent.ITopicFix(topic.NewTopic(req.Topic_))
  161. res := sf.SendRequest(topic, req.BinReq_)
  162. resp := &dot.ServeResp{
  163. Status_: "ok",
  164. Uuid_: req.Uuid_,
  165. }
  166. if res.IsErr() {
  167. resp.Status_ = fmt.Sprintf("kernelBusHttp.processSendRequest(): err=\n\t%v", res.Err())
  168. return resp
  169. }
  170. resp.BinResp_ = res.Ok()
  171. return resp
  172. }
  173. // Входящая отписка от топика по HTTP.
  174. func (sf *kBusHttp) postUnsub(ctx fiber.Ctx) error {
  175. sf.log.Debug("postUnsub()")
  176. ctx.Set("Content-type", "text/html; charset=utf8")
  177. ctx.Set("Content-type", "text/json")
  178. ctx.Set("Cache-Control", "no-cache")
  179. req := &dot.UnsubReq{}
  180. err := ctx.Bind().Body(req)
  181. if err != nil {
  182. resp := &dot.ServeResp{
  183. Status_: fmt.Sprintf("kernelBusHttp.postSendRequest(): err=\n\t%v", err),
  184. Uuid_: req.Uuid_,
  185. }
  186. resp.SelfCheck()
  187. ctx.Response().SetStatusCode(http.StatusBadRequest)
  188. sf.log.Err("postUnsub(): in body_parser, status=%q", resp.Status_)
  189. return ctx.JSON(resp)
  190. }
  191. resp := sf.processUnsubRequest(req)
  192. resp.SelfCheck()
  193. return ctx.JSON(resp)
  194. }
  195. // Процесс отписки от топика.
  196. func (sf *kBusHttp) processUnsubRequest(req *dot.UnsubReq) *dot.UnsubResp {
  197. req.SelfCheck()
  198. resp := &dot.UnsubResp{
  199. Status_: "ok",
  200. Uuid_: req.Uuid_,
  201. }
  202. optHandler := sf.KCtx_.Get(string(req.HandlerName_))
  203. if optHandler.IsNone() {
  204. resp.Status_ = fmt.Sprintf("kernelBusHttp.processUnsubRequest(): not get handler(%v) from kernel ctx",
  205. req.HandlerName_)
  206. return resp
  207. }
  208. if optHandler == nil {
  209. resp.Status_ = fmt.Sprintf("kernelBusHttp.processUnsubRequest(): handler(%v) not exists", req.HandlerName_)
  210. return resp
  211. }
  212. hand := optHandler.Some().Val().(bus_spec.IBusHandlerSubscribe)
  213. sf.Unsubscribe(hand)
  214. return resp
  215. }