mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-07 07:42:45 +01:00
36
bus/request.go
Normal file
36
bus/request.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package bus
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/arangodb/go-driver"
|
||||
emitter "github.com/emitter-io/go/v2"
|
||||
)
|
||||
|
||||
const ChannelRequest = "request"
|
||||
|
||||
type RequestMsg struct {
|
||||
IDs []driver.DocumentID `json:"ids"`
|
||||
Function string `json:"function"`
|
||||
User string `json:"user"`
|
||||
}
|
||||
|
||||
func (b *Bus) PublishRequest(user, f string, ids []driver.DocumentID) error {
|
||||
return b.jsonPublish(&RequestMsg{
|
||||
User: user,
|
||||
Function: f,
|
||||
IDs: ids,
|
||||
}, ChannelRequest, b.config.requestKey)
|
||||
}
|
||||
|
||||
func (b *Bus) SubscribeRequest(f func(msg *RequestMsg)) error {
|
||||
return b.safeSubscribe(b.config.requestKey, ChannelRequest, func(c *emitter.Client, m emitter.Message) {
|
||||
var msg RequestMsg
|
||||
if err := json.Unmarshal(m.Payload(), &msg); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
go f(&msg)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user