Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 2.04 KB

File metadata and controls

75 lines (59 loc) · 2.04 KB

Example Fn Go FDK : Notifications

This example provides a Function to consume a Notification. The function accepts a typed message event.

Source

NotificationMessage

Dependencies

  • [fn-events] for NotificationsHandler fdk handler.

Demonstrated FDK features

This example showcases how to use the fn-event NotificationsHandler to use a Function as the target for Notifications.

Step by step

Set up the Notification Topic with Function Subscription:

The Function fdk.handler accepts the event handler.

    handler := fn_events.NotificationsHandler(NewNotificationsHandler(),
  reflect.TypeOf(Employee{}),
  )
  fdk.Handle(handler)

Full example:

package main

import (
  "context"
  "github.com/fnproject/fdk-go"
  fn_events "github.com/fnproject/fn-events"
  "log"
  "reflect"
)

type ExampleNotificationsHandler struct {
  notificationService NotificationService
}

func NewNotificationsHandler() *ExampleNotificationsHandler {
  return &ExampleNotificationsHandler{
    notificationService: &RealNotificationService{},
  }
}

func (h *ExampleNotificationsHandler) Serve(ctx context.Context, notification fn_events.NotificationMessage[Employee]) {
  h.notificationService.ReadNotification(notification)
}

func main() {
  log.SetFlags(0) // Removes redundant timestamps from logs
  handler := fn_events.NotificationsHandler(NewNotificationsHandler(),
    reflect.TypeOf(Employee{}),
  )
  fdk.Handle(handler)
}

The NotificationMessage Content contains the message from the Notification.

The Employee struct is used to unmarshalled the message. Alternatively, provide a string type for basic message.

    handler := NotificationsHandler(
    MyNotificationHandler,
    reflect.TypeOf(""),
    )