first commit
This commit is contained in:
36
internal/webhook/service.go
Normal file
36
internal/webhook/service.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo *Repo
|
||||
domain string
|
||||
enabled bool
|
||||
}
|
||||
|
||||
func NewService(repo *Repo, domain string, enabled bool) *Service {
|
||||
return &Service{repo: repo, domain: domain, enabled: enabled}
|
||||
}
|
||||
|
||||
func (s *Service) Enabled() bool { return s.enabled }
|
||||
|
||||
func (s *Service) Enqueue(ctx context.Context, event EventType, data map[string]any) error {
|
||||
if !s.enabled {
|
||||
return nil
|
||||
}
|
||||
p := Payload{
|
||||
Event: event,
|
||||
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
||||
Domain: s.domain,
|
||||
Data: data,
|
||||
}
|
||||
b, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.Insert(ctx, event, string(b))
|
||||
}
|
||||
Reference in New Issue
Block a user