httppush_source.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package http
  15. import (
  16. "fmt"
  17. "net/http"
  18. "strings"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/internal/io/http/httpserver"
  21. "github.com/lf-edge/ekuiper/internal/io/memory/pubsub"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. "github.com/lf-edge/ekuiper/pkg/infra"
  25. )
  26. type PushConf struct {
  27. Method string `json:"method"`
  28. ContentType string `json:"contentType"`
  29. BufferLength int `json:"bufferLength"`
  30. Endpoint string `json:"endpoint"`
  31. }
  32. type PushSource struct {
  33. conf *PushConf
  34. }
  35. func (hps *PushSource) Configure(endpoint string, props map[string]interface{}) error {
  36. cfg := &PushConf{
  37. Method: http.MethodPost,
  38. ContentType: "application/json",
  39. BufferLength: 1024,
  40. }
  41. err := cast.MapToStruct(props, cfg)
  42. if err != nil {
  43. return err
  44. }
  45. if cfg.Method != http.MethodPost && cfg.Method != http.MethodPut {
  46. return fmt.Errorf("method %s is not supported, must be POST or PUT", cfg.Method)
  47. }
  48. if cfg.ContentType != "application/json" {
  49. return fmt.Errorf("property `contentType` must be application/json")
  50. }
  51. if !strings.HasPrefix(endpoint, "/") {
  52. return fmt.Errorf("property `endpoint` must start with /")
  53. }
  54. cfg.Endpoint = endpoint
  55. hps.conf = cfg
  56. conf.Log.Debugf("Initialized with configurations %#v.", cfg)
  57. return nil
  58. }
  59. func (hps *PushSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  60. t, done, err := httpserver.RegisterEndpoint(hps.conf.Endpoint, hps.conf.Method, hps.conf.ContentType)
  61. if err != nil {
  62. infra.DrainError(ctx, err, errCh)
  63. return
  64. }
  65. defer httpserver.UnregisterEndpoint(hps.conf.Endpoint)
  66. ch := pubsub.CreateSub(t, nil, fmt.Sprintf("%s_%s_%d", ctx.GetRuleId(), ctx.GetOpId(), ctx.GetInstanceId()), hps.conf.BufferLength)
  67. defer pubsub.CloseSourceConsumerChannel(t, fmt.Sprintf("%s_%s_%d", ctx.GetRuleId(), ctx.GetOpId(), ctx.GetInstanceId()))
  68. for {
  69. select {
  70. case <-done: // http data server error
  71. infra.DrainError(ctx, fmt.Errorf("http data server shutdown"), errCh)
  72. return
  73. case v, opened := <-ch:
  74. if !opened {
  75. return
  76. }
  77. consumer <- v
  78. case <-ctx.Done():
  79. return
  80. }
  81. }
  82. }
  83. func (hps *PushSource) Close(ctx api.StreamContext) error {
  84. logger := ctx.GetLogger()
  85. logger.Infof("Closing HTTP push source")
  86. return nil
  87. }