fasten-onprem/backend/pkg/web/handler/server_sent_event.go

39 lines
1.1 KiB
Go
Raw Normal View History

package handler
import (
"github.com/fastenhealth/fasten-onprem/backend/pkg"
"github.com/fastenhealth/fasten-onprem/backend/pkg/web/sse"
"github.com/gin-gonic/gin"
"io"
2023-09-08 12:27:38 -06:00
"log"
)
// SSEStream is a handler for the server sent event stream (notifications from background processes)
// see: https://github.com/gin-gonic/examples/blob/master/server-sent-event/main.go
// see: https://stackoverflow.com/questions/66327142/selectively-send-event-to-particular-clients
//
// test using:
// curl -N -H "Authorization: Bearer xxxxx" http://localhost:9090/api/secure/sse/stream
func SSEStream(c *gin.Context) {
//logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
//databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
v, ok := c.Get(pkg.ContextKeyTypeSSEClientChannel)
if !ok {
2023-09-08 12:27:38 -06:00
log.Printf("could not get client channel from context")
return
}
2023-09-08 12:27:38 -06:00
listener, ok := v.(sse.EventBusListener)
if !ok {
return
}
c.Stream(func(w io.Writer) bool {
// Stream message to client from message channel
2023-09-08 12:27:38 -06:00
if msg, ok := <-listener.ResponseChan; ok {
c.SSEvent("message", msg)
return true
}
return false
})
}