Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
116 lines
2.3 KiB
Go
116 lines
2.3 KiB
Go
package eventbus
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cortezaproject/corteza/server/pkg/auth"
|
|
"github.com/cortezaproject/corteza/server/pkg/sentry"
|
|
)
|
|
|
|
type (
|
|
HandlerFn func(ctx context.Context, ev Event) error
|
|
|
|
handler struct {
|
|
handler HandlerFn
|
|
resourceTypes map[string]bool
|
|
eventTypes map[string]bool
|
|
constraints constraintSet
|
|
weight int
|
|
}
|
|
|
|
// @todo add sorting interface
|
|
HandlerSet []*handler
|
|
|
|
HandlerRegOp func(t *handler)
|
|
|
|
eventInvokerSettable interface {
|
|
SetInvoker(auth.Identifiable)
|
|
}
|
|
)
|
|
|
|
// Match matches handler with resource event
|
|
func (t handler) Match(re Event) bool {
|
|
if re == nil {
|
|
return false
|
|
}
|
|
|
|
if len(re.ResourceType()) == 0 || !t.resourceTypes[re.ResourceType()] {
|
|
// Expecting to have valid resource type and match at least one
|
|
// defined resource on the handler
|
|
return false
|
|
}
|
|
|
|
if len(re.EventType()) == 0 || !t.eventTypes[re.EventType()] {
|
|
// Expecting to have valid event type and match at least one
|
|
// defined event on the handler
|
|
return false
|
|
}
|
|
|
|
for _, c := range t.constraints {
|
|
// Should match all constraints
|
|
if !re.Match(c) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (t handler) Handle(ctx context.Context, ev Event) error {
|
|
defer sentry.Recover()
|
|
|
|
if eis, ok := ev.(eventInvokerSettable); ok {
|
|
eis.SetInvoker(auth.GetIdentityFromContext(ctx))
|
|
}
|
|
|
|
return t.handler(ctx, ev)
|
|
}
|
|
|
|
func NewHandler(h HandlerFn, ops ...HandlerRegOp) *handler {
|
|
var t = &handler{
|
|
resourceTypes: make(map[string]bool),
|
|
eventTypes: make(map[string]bool),
|
|
handler: h,
|
|
}
|
|
|
|
for _, op := range ops {
|
|
op(t)
|
|
}
|
|
|
|
return t
|
|
}
|
|
|
|
func For(rr ...string) HandlerRegOp {
|
|
return func(t *handler) {
|
|
for _, r := range rr {
|
|
t.resourceTypes[r] = true
|
|
}
|
|
}
|
|
}
|
|
|
|
func On(ee ...string) HandlerRegOp {
|
|
return func(t *handler) {
|
|
for _, e := range ee {
|
|
t.eventTypes[e] = true
|
|
}
|
|
}
|
|
}
|
|
|
|
func Constraint(c ConstraintMatcher) HandlerRegOp {
|
|
return func(t *handler) {
|
|
t.constraints = append(t.constraints, c)
|
|
}
|
|
}
|
|
|
|
func Weight(weight int) HandlerRegOp {
|
|
return func(t *handler) {
|
|
t.weight = weight
|
|
}
|
|
}
|
|
|
|
// handler sorting:
|
|
|
|
func (set HandlerSet) Len() int { return len(set) }
|
|
func (set HandlerSet) Swap(i, j int) { set[i], set[j] = set[j], set[i] }
|
|
func (set HandlerSet) Less(i, j int) bool { return set[i].weight < set[j].weight }
|