Skip to content

Commit

Permalink
Up ping with log
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Sep 8, 2024
1 parent e34df0a commit d7d9503
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
5 changes: 3 additions & 2 deletions storage/ping/pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func (p *pinger) Stream(options ...any) (stream.Streamer, error) {
return nil, err
}
strm := &pingStream{
url: patternkey.PatternKeyFromTemplate(gocast.Or(strmConf.URL, p.URL)),
method: gocast.Or(strmConf.Method, p.Method),
url: patternkey.PatternKeyFromTemplate(gocast.Or(strmConf.URL, p.URL)),
method: gocast.Or(strmConf.Method, p.Method),
contentType: gocast.Or(strmConf.ContentType, "application/json"),
}
cond, err := conf.Condition()
if err != nil {
Expand Down
29 changes: 24 additions & 5 deletions storage/ping/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ import (
"github.com/geniusrabbit/eventstream"
"github.com/geniusrabbit/eventstream/internal/message"
"github.com/geniusrabbit/eventstream/internal/patternkey"
"github.com/geniusrabbit/eventstream/internal/zlogger"
"go.uber.org/zap"
)

type pingStreamConfig struct {
URL string `json:"url"`
Method string `json:"method"`
URL string `json:"url"`
Method string `json:"method"`
ContentType string `json:"content_type"`
}

type pingStream struct {
id string

url *patternkey.PatternKey
method string
url *patternkey.PatternKey
method string
contentType string

httpClient http.Client
}
Expand All @@ -36,11 +40,26 @@ func (p *pingStream) ID() string { return p.id }
// Put implements stream.Streamer.
func (p *pingStream) Put(ctx context.Context, msg message.Message) error {
nUrl := p.url.Prepare(msg)
if strings.HasPrefix(nUrl, "//") {
nUrl = "http:" + nUrl
}
if p.method == http.MethodGet {
_, err := p.httpClient.Get(nUrl)
return err
}
_, err := p.httpClient.Post(nUrl, "application/json", strings.NewReader(msg.JSON()))
resp, err := p.httpClient.Post(nUrl, p.contentType, strings.NewReader(msg.JSON()))
if err == nil {
_ = resp.Body.Close()
zlogger.FromContext(ctx).Info("ping",
zap.String("method", p.method),
zap.String("url", nUrl),
zap.Int("status", resp.StatusCode))
} else {
zlogger.FromContext(ctx).Error("ping",
zap.String("method", p.method),
zap.String("url", nUrl),
zap.Error(err))
}
return err
}

Expand Down

0 comments on commit d7d9503

Please sign in to comment.