Started working on the event bus
This commit is contained in:
parent
fdd2ecc7fc
commit
e2f159e980
9 changed files with 233 additions and 61 deletions
|
|
@ -1,11 +1,13 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"lmika.dev/lmika/hugo-cms/models"
|
||||
"lmika.dev/lmika/hugo-cms/providers/bus"
|
||||
"lmika.dev/lmika/hugo-cms/services/sites"
|
||||
"net/http"
|
||||
"time"
|
||||
|
|
@ -13,6 +15,7 @@ import (
|
|||
|
||||
type Site struct {
|
||||
Site *sites.Service
|
||||
Bus *bus.Bus
|
||||
}
|
||||
|
||||
func (s *Site) Create(c fiber.Ctx) error {
|
||||
|
|
@ -75,6 +78,41 @@ func (s *Site) Rebuild(c fiber.Ctx) error {
|
|||
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", GetSite(c).ID))
|
||||
}
|
||||
|
||||
func (s *Site) SSE(c fiber.Ctx) error {
|
||||
siteOfInterest := GetSite(c)
|
||||
|
||||
c.Set("Content-Type", "text/event-stream")
|
||||
c.Set("Cache-Control", "no-cache")
|
||||
c.Set("Connection", "keep-alive")
|
||||
c.Set("Transfer-Encoding", "chunked")
|
||||
|
||||
return c.SendStreamWriter(func(w *bufio.Writer) {
|
||||
sub := s.Bus.Subscribe()
|
||||
defer s.Bus.Unsubscribe(sub)
|
||||
|
||||
for e := range sub.C {
|
||||
switch e.Type {
|
||||
case models.EventSiteBuildingStart:
|
||||
eventSite := e.Data.(models.Site)
|
||||
if eventSite.ID == siteOfInterest.ID {
|
||||
fmt.Fprintf(w, "event: site-build-status\n")
|
||||
fmt.Fprintf(w, "data: Building\n")
|
||||
}
|
||||
case models.EventSiteBuildingDone:
|
||||
eventSite := e.Data.(models.Site)
|
||||
if eventSite.ID == siteOfInterest.ID {
|
||||
fmt.Fprintf(w, "event: site-build-status\n")
|
||||
fmt.Fprintf(w, "data: \n")
|
||||
}
|
||||
}
|
||||
|
||||
if err := w.Flush(); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Site) WithSite() fiber.Handler {
|
||||
return func(c fiber.Ctx) (err error) {
|
||||
id := fiber.Params[int](c, "siteId")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue