Fully implemented backend sse
This commit is contained in:
parent
ef4a9f2ee0
commit
7316b0fabd
|
@ -1,15 +1,17 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/models"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/gommon/log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
str func() models.Managable
|
||||
str func() models.Managable
|
||||
broker *Broker
|
||||
}
|
||||
|
||||
type UpdatedMessage struct {
|
||||
|
@ -52,6 +54,12 @@ func (h *Handler) Create(c echo.Context) error {
|
|||
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
||||
}
|
||||
|
||||
// Notify the broker
|
||||
h.broker.messages <- Message{
|
||||
Kind: KindCreate,
|
||||
Data: str,
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "success")
|
||||
}
|
||||
|
||||
|
@ -66,6 +74,12 @@ func (h *Handler) Delete(c echo.Context) error {
|
|||
return echo.NewHTTPError(http.StatusInternalServerError, "Error.")
|
||||
}
|
||||
|
||||
// Notify the broker
|
||||
h.broker.messages <- Message{
|
||||
Kind: KindDelete,
|
||||
Data: str,
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "success")
|
||||
}
|
||||
|
||||
|
@ -81,6 +95,13 @@ func (h *Handler) Update(c echo.Context) error {
|
|||
log.Error(err)
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
// Notify the broker
|
||||
h.broker.messages <- Message{
|
||||
Kind: KindUpdate,
|
||||
Data: str,
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, UpdatedMessage{
|
||||
Message: "success",
|
||||
Data: str,
|
||||
|
|
|
@ -65,13 +65,13 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
log.Fatal("Invalid Mode!")
|
||||
}
|
||||
|
||||
e.GET("/list", handler.ReadAll)
|
||||
|
||||
// Fancy message broker with SSE
|
||||
b := NewBroker()
|
||||
b.Start()
|
||||
handler.broker = b
|
||||
b.handler = &handler
|
||||
b.Start()
|
||||
e.GET("/events", b.Serve)
|
||||
e.GET("/list", handler.ReadAll)
|
||||
|
||||
// Routes with auth
|
||||
a := e.Group("")
|
||||
|
|
|
@ -39,7 +39,10 @@ type Broker struct {
|
|||
type MessageKind string
|
||||
|
||||
const (
|
||||
KindInit MessageKind = `init`
|
||||
KindInit MessageKind = `init`
|
||||
KindUpdate MessageKind = `update`
|
||||
KindCreate MessageKind = `create`
|
||||
KindDelete MessageKind = `delete`
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
|
@ -62,6 +65,10 @@ func NewBroker() *Broker {
|
|||
//
|
||||
func (b *Broker) Start() {
|
||||
|
||||
if b.handler == nil {
|
||||
panic("handler must not be nil!")
|
||||
}
|
||||
|
||||
// Start a goroutine
|
||||
//
|
||||
go func() {
|
||||
|
|
Loading…
Reference in New Issue