Files
ds-galo/backend/internal/sse/serve.go
iu 2b2b342492 feat: implement shared-album multi-user photo album
Backend (Go/Gin/pgx): env config, pgx pool with retries, Garage S3 client
(path-style, region garage), embedded golang-migrate migrations, JWT auth with
refresh rotation, albums CRUD, members/roles/invites with no-escalation matrix,
photo upload + async imaging/goexif WebP variants, cursor pagination, presigned
URLs, likes/comments, SSE hub (album + personal), notifications, share links.

Frontend (SvelteKit 2 + Svelte 5 + Tailwind v4): API wrapper with 401 refresh,
auth store, all screens, Uploader/PhotoGrid/Lightbox/Comments, SSE client with
reconnect + REST catch-up, three themes + system via data-theme.

Deploy without Docker: Makefile builds the frontend into backend/static and
embeds it (go:embed) into a single statically linked binary; systemd unit and
nginx SSE example included.

Use gen2brain/webp (-tags nodynamic) instead of cgo kolesa-team/go-webp to keep
CGO_ENABLED=0 static builds; design.md/proposal.md updated accordingly.
2026-10-10 12:50:33 +03:00

54 lines
1.1 KiB
Go

package sse
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func Serve(c *gin.Context, sub *Subscription, heartbeat time.Duration) {
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("X-Accel-Buffering", "no")
c.Writer.WriteHeader(http.StatusOK)
flusher, ok := c.Writer.(http.Flusher)
if !ok {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
fmt.Fprint(c.Writer, ": connected\n\n")
flusher.Flush()
if heartbeat <= 0 {
heartbeat = 25 * time.Second
}
ticker := time.NewTicker(heartbeat)
defer ticker.Stop()
ctx := c.Request.Context()
for {
select {
case <-ctx.Done():
return
case ev, ok := <-sub.Events():
if !ok {
return
}
payload, err := json.Marshal(ev.Data)
if err != nil {
continue
}
fmt.Fprintf(c.Writer, "event: %s\ndata: %s\n\n", ev.Type, payload)
flusher.Flush()
case <-ticker.C:
fmt.Fprint(c.Writer, ": heartbeat\n\n")
flusher.Flush()
}
}
}