bus.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package borealis
  2. import (
  3. "fmt"
  4. "github.com/nats-io/nats.go"
  5. )
  6. type Bus struct {
  7. NATS *nats.EncodedConn
  8. }
  9. func Connect(url string) (*Bus, error) {
  10. conn, err := nats.Connect(url)
  11. if err != nil {
  12. return nil, fmt.Errorf("failed to connect to the broker: %w", err)
  13. }
  14. encodedConn, err := nats.NewEncodedConn(conn, nats.JSON_ENCODER) // TODO
  15. if err != nil {
  16. return nil, fmt.Errorf("failed to configure the bus encoding: %w", err)
  17. }
  18. return &Bus{NATS: encodedConn}, nil
  19. }
  20. func (bus *Bus) SendEmail(request *SendEmail) error {
  21. envelope := request // TODO
  22. if err := bus.NATS.Publish("email.outbound", envelope); err != nil {
  23. return fmt.Errorf("failed to enqueue outbound email: %w", err)
  24. }
  25. return nil
  26. }
  27. func (bus *Bus) RegisterID(request *RegisterID) error {
  28. envelope := request // TODO
  29. if err := bus.NATS.Publish("id.register", envelope); err != nil {
  30. return fmt.Errorf("failed to enqueue ID registration: %w", err)
  31. }
  32. return nil
  33. }
  34. func (bus *Bus) VerifyEmail(request *VerifyEmail) error {
  35. envelope := request // TODO
  36. if err := bus.NATS.Publish("id.verify", envelope); err != nil {
  37. return fmt.Errorf("failed to enqueue ID verification: %w", err)
  38. }
  39. return nil
  40. }
  41. func (bus *Bus) GrantAccess(request *GrantAccess) error {
  42. envelope := request // TODO
  43. if err := bus.NATS.Publish("access.grant", envelope); err != nil {
  44. return fmt.Errorf("failed to enqueue access grant: %w", err)
  45. }
  46. return nil
  47. }
  48. func (bus *Bus) RevokeAccess(request *RevokeAccess) error {
  49. envelope := request // TODO
  50. if err := bus.NATS.Publish("access.revoke", envelope); err != nil {
  51. return fmt.Errorf("failed to enqueue access revocation: %w", err)
  52. }
  53. return nil
  54. }