-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add event emit and listen arch, user end is still left
Signed-off-by: sarthakjdev <[email protected]>
- Loading branch information
1 parent
570a1ab
commit b34d46e
Showing
14 changed files
with
325 additions
and
110 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package manager | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/sarthakjdev/wapi.go/pkg/events" | ||
) | ||
|
||
type EventType string | ||
|
||
const ( | ||
TextMessageEvent EventType = "text_message" | ||
AudioMessageEvent EventType = "audio_message" | ||
VideoMessageEvent EventType = "video_message" | ||
ImageMessageEvent EventType = "image_message" | ||
ContactMessageEvent EventType = "contact_message" | ||
DocumentMessageEvent EventType = "document_message" | ||
LocationMessageEvent EventType = "location_message" | ||
ReactionMessageEvent EventType = "reaction_message" | ||
ListInteractionMessageEvent EventType = "list_interaction_message" | ||
TemplateMessageEvent EventType = "template_message" | ||
QuickReplyMessageEvent EventType = "quick_reply_message" | ||
ReplyButtonInteractionEvent EventType = "reply_button_interaction" | ||
StickerMessageEvent EventType = "sticker_message" | ||
AdInteractionEvent EventType = "ad_interaction_message" | ||
CustomerIdentityChangedEvent EventType = "customer_identity_changed" | ||
CustomerNumberChangedEvent EventType = "customer_number_changed" | ||
MessageDeliveredEvent EventType = "message_delivered" | ||
MessageFailedEvent EventType = "message_failed" | ||
MessageReadEvent EventType = "message_read" | ||
MessageSentEvent EventType = "message_sent" | ||
MessageUndeliveredEvent EventType = "message_undelivered" | ||
OrderReceivedEvent EventType = "order_received" | ||
ProductInquiryEvent EventType = "product_inquiry" | ||
UnknownEvent EventType = "unknown" | ||
ErrorEvent EventType = "error" | ||
WarnEvent EventType = "warn" | ||
ReadyEvent EventType = "ready" | ||
) | ||
|
||
type ChannelEvent struct { | ||
Type EventType | ||
Data events.BaseEvent | ||
} | ||
|
||
type EventManger struct { | ||
subscribers map[string]chan ChannelEvent | ||
sync.RWMutex | ||
} | ||
|
||
func NewEventManager() *EventManger { | ||
return &EventManger{ | ||
subscribers: make(map[string]chan ChannelEvent), | ||
} | ||
} | ||
|
||
// subscriber to this event listener will be notified when the event is published | ||
func (em *EventManger) Subscribe(eventName string) (chan ChannelEvent, error) { | ||
em.Lock() | ||
defer em.Unlock() | ||
if ch, ok := em.subscribers[eventName]; ok { | ||
return ch, nil | ||
} | ||
em.subscribers[eventName] = make(chan ChannelEvent, 100) | ||
return em.subscribers[eventName], nil | ||
|
||
} | ||
|
||
// subscriber to this event listener will be notified when the event is published | ||
func (em *EventManger) Unsubscribe(id string) { | ||
em.Lock() | ||
defer em.Unlock() | ||
delete(em.subscribers, id) | ||
} | ||
|
||
// publish event to this events system and let all the subscriber consume them | ||
func (em *EventManger) Publish(eventType EventType, data events.BaseEvent) error { | ||
fmt.Println("Publishing event: ", eventType) | ||
em.Lock() | ||
defer em.Unlock() | ||
|
||
for _, ch := range em.subscribers { | ||
select { | ||
case ch <- ChannelEvent{ | ||
Type: eventType, | ||
Data: data, | ||
}: | ||
default: | ||
return fmt.Errorf("event queue full for type: %s", eventType) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (em *EventManger) On(name EventType, handler func(events.BaseEvent)) string { | ||
ch, _ := em.Subscribe(string(name)) | ||
go func() { | ||
for { | ||
select { | ||
case event := <-ch: | ||
handler(event.Data) | ||
} | ||
} | ||
}() | ||
return string(name) | ||
} |
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
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,90 @@ | ||
package manager | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/sarthakjdev/wapi.go/pkg/events" | ||
) | ||
|
||
// references for event driven architecture in golang: https://medium.com/@souravchoudhary0306/implementation-of-event-driven-architecture-in-go-golang-28d9a1c01f91 | ||
type WebhookManager struct { | ||
secret string | ||
path string | ||
port int | ||
EventManager EventManger | ||
Requester RequestClient | ||
} | ||
|
||
type WebhookManagerConfig struct { | ||
Secret string | ||
Path string | ||
Port int | ||
EventManager EventManger | ||
Requester RequestClient | ||
} | ||
|
||
func NewWebhook(options *WebhookManagerConfig) *WebhookManager { | ||
return &WebhookManager{ | ||
secret: options.Secret, | ||
path: options.Path, | ||
port: options.Port, | ||
EventManager: options.EventManager, | ||
Requester: options.Requester, | ||
} | ||
} | ||
|
||
// this function is used in case if the client have not provided any custom http server | ||
func (wh *WebhookManager) createEchoHttpServer() *echo.Echo { | ||
e := echo.New() | ||
return e | ||
|
||
} | ||
|
||
func (wh *WebhookManager) getRequestHandler(req *http.Request) { | ||
} | ||
|
||
func (wh *WebhookManager) postRequestHandler(req *http.Request) { | ||
// emits events based on the payload of the request | ||
|
||
wh.EventManager.Publish(TextMessageEvent, events.NewTextMessageEvent( | ||
"wiuhbiueqwdqwd", | ||
"2134141414", | ||
"hello", | ||
)) | ||
|
||
} | ||
|
||
func (wh *WebhookManager) ListenToEvents() { | ||
|
||
fmt.Println("Listening to events") | ||
server := wh.createEchoHttpServer() | ||
|
||
// Start server in a goroutine | ||
go func() { | ||
if err := server.Start(":8080"); err != nil { | ||
return | ||
} | ||
}() | ||
|
||
wh.EventManager.Publish(ReadyEvent, events.NewReadyEvent()) | ||
|
||
// Wait for an interrupt signal (e.g., Ctrl+C) | ||
quit := make(chan os.Signal, 1) | ||
signal.Notify(quit, os.Interrupt) // Capture SIGINT (Ctrl+C) | ||
<-quit // Wait for the signal | ||
|
||
// Gracefully shut down the server (optional) | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
if err := server.Shutdown(ctx); err != nil { | ||
log.Fatal(err) // Handle shutdown errors gracefully | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.