-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event): add external event broker
- Loading branch information
Showing
6 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package eventbroker | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/url" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
var instance Broker | ||
|
||
// Broker is the event broker interface | ||
type Broker interface { | ||
Send(payload io.Reader) error | ||
} | ||
|
||
// Configure the data event Broker regarding the configuration URI | ||
func Configure(uri string) (Broker, error) { | ||
if uri == "" { | ||
return nil, nil | ||
} | ||
u, err := url.ParseRequestURI(uri) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid configuration URI: %s", uri) | ||
} | ||
|
||
switch u.Scheme { | ||
case "http", "https": | ||
instance, err = newHTTPBroker(u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
log.Info().Str("component", "broker").Str("uri", u.String()).Msg("using HTTP event broker") | ||
default: | ||
return nil, fmt.Errorf("unsuported event broker: %s", u.Scheme) | ||
} | ||
return instance, nil | ||
} | ||
|
||
// Lookup returns the global event broker | ||
func Lookup() Broker { | ||
if instance != nil { | ||
return instance | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package eventbroker | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// HTTPBroker structure | ||
type HTTPBroker struct { | ||
uri *url.URL | ||
} | ||
|
||
func newHTTPBroker(uri *url.URL) (Broker, error) { | ||
return &HTTPBroker{ | ||
uri: uri, | ||
}, nil | ||
} | ||
|
||
// Send the payload to the event broker | ||
func (hb *HTTPBroker) Send(payload io.Reader) error { | ||
resp, err := http.Post(hb.uri.String(), "application/json; charset=utf-8", payload) | ||
if err != nil { | ||
return err | ||
} else if resp.StatusCode >= 300 { | ||
return fmt.Errorf("bad status code: %d", resp.StatusCode) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package event | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
eventbroker "github.com/ncarlier/reader/pkg/event-broker" | ||
"github.com/ncarlier/reader/pkg/model" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
const errorMsg = "Unable to send new user external event broker" | ||
|
||
func init() { | ||
bus.Subscribe(CreateUser, func(payload ...interface{}) { | ||
if user, ok := payload[0].(model.User); ok { | ||
broker := eventbroker.Lookup() | ||
if broker == nil { | ||
log.Debug().Err(fmt.Errorf("event broker not configured")).Uint("uid", *user.ID).Msg(errorMsg) | ||
return | ||
} | ||
b := new(bytes.Buffer) | ||
json.NewEncoder(b).Encode(user) | ||
if err := broker.Send(b); err != nil { | ||
log.Error().Err(err).Uint("uid", *user.ID).Msg(errorMsg) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters