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.
140 lines
2.7 KiB
Go
140 lines
2.7 KiB
Go
package sse
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type Event struct {
|
|
Type string
|
|
Data any
|
|
}
|
|
|
|
type subscriber struct {
|
|
ch chan Event
|
|
closed bool
|
|
}
|
|
|
|
type Subscription struct {
|
|
hub *Hub
|
|
key string
|
|
personal bool
|
|
sub *subscriber
|
|
once sync.Once
|
|
}
|
|
|
|
func (s *Subscription) Events() <-chan Event { return s.sub.ch }
|
|
|
|
func (s *Subscription) Close() {
|
|
s.once.Do(func() {
|
|
if s.personal {
|
|
s.hub.unsubscribeUser(s.key, s.sub)
|
|
} else {
|
|
s.hub.unsubscribeAlbum(s.key, s.sub)
|
|
}
|
|
})
|
|
}
|
|
|
|
type Hub struct {
|
|
mu sync.RWMutex
|
|
albums map[string]map[*subscriber]struct{}
|
|
users map[string]map[*subscriber]struct{}
|
|
buffer int
|
|
}
|
|
|
|
func NewHub(buffer int) *Hub {
|
|
if buffer <= 0 {
|
|
buffer = 64
|
|
}
|
|
return &Hub{
|
|
albums: make(map[string]map[*subscriber]struct{}),
|
|
users: make(map[string]map[*subscriber]struct{}),
|
|
buffer: buffer,
|
|
}
|
|
}
|
|
|
|
func (h *Hub) SubscribeAlbum(albumID string) *Subscription {
|
|
sub := &subscriber{ch: make(chan Event, h.buffer)}
|
|
h.mu.Lock()
|
|
if h.albums[albumID] == nil {
|
|
h.albums[albumID] = make(map[*subscriber]struct{})
|
|
}
|
|
h.albums[albumID][sub] = struct{}{}
|
|
h.mu.Unlock()
|
|
return &Subscription{hub: h, key: albumID, sub: sub}
|
|
}
|
|
|
|
func (h *Hub) SubscribeUser(userID string) *Subscription {
|
|
sub := &subscriber{ch: make(chan Event, h.buffer)}
|
|
h.mu.Lock()
|
|
if h.users[userID] == nil {
|
|
h.users[userID] = make(map[*subscriber]struct{})
|
|
}
|
|
h.users[userID][sub] = struct{}{}
|
|
h.mu.Unlock()
|
|
return &Subscription{hub: h, key: userID, personal: true, sub: sub}
|
|
}
|
|
|
|
func (h *Hub) Publish(albumID, eventType string, data any) {
|
|
if albumID == "" {
|
|
return
|
|
}
|
|
h.mu.RLock()
|
|
defer h.mu.RUnlock()
|
|
h.broadcast(h.albums[albumID], Event{Type: eventType, Data: data})
|
|
}
|
|
|
|
func (h *Hub) PublishUser(userID, eventType string, data any) {
|
|
if userID == "" {
|
|
return
|
|
}
|
|
h.mu.RLock()
|
|
defer h.mu.RUnlock()
|
|
h.broadcast(h.users[userID], Event{Type: eventType, Data: data})
|
|
}
|
|
|
|
func (h *Hub) broadcast(subs map[*subscriber]struct{}, ev Event) {
|
|
for sub := range subs {
|
|
if sub.closed {
|
|
continue
|
|
}
|
|
select {
|
|
case sub.ch <- ev:
|
|
default:
|
|
// slow client: drop it (backpressure)
|
|
sub.closed = true
|
|
close(sub.ch)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (h *Hub) unsubscribeAlbum(albumID string, sub *subscriber) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
if subs, ok := h.albums[albumID]; ok {
|
|
delete(subs, sub)
|
|
if len(subs) == 0 {
|
|
delete(h.albums, albumID)
|
|
}
|
|
}
|
|
closeIfOpen(sub)
|
|
}
|
|
|
|
func (h *Hub) unsubscribeUser(userID string, sub *subscriber) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
if subs, ok := h.users[userID]; ok {
|
|
delete(subs, sub)
|
|
if len(subs) == 0 {
|
|
delete(h.users, userID)
|
|
}
|
|
}
|
|
closeIfOpen(sub)
|
|
}
|
|
|
|
func closeIfOpen(sub *subscriber) {
|
|
if !sub.closed {
|
|
sub.closed = true
|
|
close(sub.ch)
|
|
}
|
|
}
|