# README
Hook
It is a package that send in one way term any payload to one or more targets. It works by subscription and scope.
subscribe a http target
package main
import (
"context"
"os"
"github.com/w6d-io/hook"
"github.com/w6d-io/x/logx"
)
type payload struct {
Name string `json:"name"`
Kind string `json:"kind"`
}
func main() {
ctx := context.Background()
log := logx.WithName(ctx, "Main")
URL := "http://localhost:8080/test"
// add a target for the payload for all scope
if err := hook.Subscribe(ctx, URL, "test"); err != nil {
log.Error(err, "Subscription failed", "target", URL)
os.Exit(1)
}
kafka := "kafka://localhost:9092?topic=TEST&messagekey=msgid"
// add a target for the payload for all scope
if err := hook.Subscribe(ctx, kafka, ".*"); err != nil {
log.Error(err, "Subscription failed", "target", kafka)
os.Exit(1)
}
p := &payload{
Name: "payload",
Kind: "test",
}
// Send payload with test as scope the payload is send to http and kafka
if err := hook.Send(ctx, p, "test"); err != nil {
log.Error(err, "Send failed")
os.Exit(1)
}
// Send payload with failed as scope. Only sends to kafka due to the scope
if err := hook.Send(ctx, p, "failed"); err != nil {
log.Error(err, "Send failed")
os.Exit(1)
}
}
The subscribe function gets the scope in regex format
# Packages
Copyright 2020 WILDCARD
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Copyright 2020 WILDCARD
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
No description provided by the author
# Functions
AddProvider adds the protocol Send function to the suppliers list.
CleanSubscriber cleans the list of subscriber.
DoSend loops into all the subscribers url.
ParseMultiHostURL returns a slice of URL split by host
Example:
exampleURL := "kafka://rpk-0.rpk.kafka.svc.cluster.local:9092,rpk-1.rpk.kafka.svc.cluster.local:9092,rpk-2.rpk.kafka.svc.cluster.local:9092/?topic=PIPELINE_EVENTS&groupid=cicd" parsedURLs := parseMultiHostURL(exampleURL) for _, url := range parsedURLs { fmt.Println(url) }
Output:
kafka://rpk-0.rpk.kafka.svc.cluster.local:9092/?topic=PIPELINE_EVENTS&groupid=cicd kafka://rpk-1.rpk.kafka.svc.cluster.local:9092/?topic=PIPELINE_EVENTS&groupid=cicd kafka://rpk-2.rpk.kafka.svc.cluster.local:9092/?topic=PIPELINE_EVENTS&groupid=cicd
Warning:
the latest trailing `/` at the end of the host[s] is mandatory for that function works properly
param: url return: []url.
ResolveUrl from payload content.
No description provided by the author
Subscribe recorder the suppliers and its scope in subscribers.
# Interfaces
No description provided by the author