diff --git a/go.mod b/go.mod index cad7113..814988c 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/redis/go-redis/v9 v9.4.0 github.com/resendlabs/resend-go v1.7.0 - github.com/yuin/goldmark v1.6.0 + github.com/yuin/goldmark v1.7.0 github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect diff --git a/go.sum b/go.sum index e6caa9a..57a997f 100644 --- a/go.sum +++ b/go.sum @@ -65,6 +65,8 @@ github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+ github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68= github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA= +github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ= github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I= golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= diff --git a/lib/redis.go b/lib/redis.go index c833b3b..81c9057 100644 --- a/lib/redis.go +++ b/lib/redis.go @@ -14,16 +14,22 @@ var ctx = context.Background() var RedisClient *redis.Client func NewClient() *redis.Client { + if RedisClient != nil { + return RedisClient + } + godotenv.Load(".env") redis_host := os.Getenv("REDIS_HOST") redis_password := os.Getenv("REDIS_PASSWORD") log.Printf("Connecting to Redis at %s", redis_host) - return redis.NewClient(&redis.Options{ + RedisClient = redis.NewClient(&redis.Options{ Addr: redis_host, Password: redis_password, DB: 0, }) + + return RedisClient } func Publish(client *redis.Client, channel string, message string) error { diff --git a/lib/sse.go b/lib/sse.go index eb16ddb..585c9f8 100644 --- a/lib/sse.go +++ b/lib/sse.go @@ -1,5 +1,53 @@ package lib +import "sync" + +type SSEServerType struct { + clients map[string]map[chan string]bool + mu sync.Mutex +} + +var SSEServer *SSEServerType + +func init() { + SSEServer = &SSEServerType{ + clients: make(map[string]map[chan string]bool), + } +} + +func NewSSEServer() *SSEServerType { + return &SSEServerType{ + clients: make(map[string]map[chan string]bool), + } +} + +func (s *SSEServerType) AddClient(channel string, client chan string) { + s.mu.Lock() + defer s.mu.Unlock() + + if _, ok := s.clients[channel]; !ok { + s.clients[channel] = make(map[chan string]bool) + } + s.clients[channel][client] = true +} + +func (s *SSEServerType) RemoveClient(channel string, client chan string) { + s.mu.Lock() + defer s.mu.Unlock() + + delete(s.clients[channel], client) + if len(s.clients[channel]) == 0 { + delete(s.clients, channel) + } +} + +func (s *SSEServerType) ClientCount(channel string) int { + s.mu.Lock() + defer s.mu.Unlock() + + return len(s.clients[channel]) +} + func SendSSE(channel string, message string) error { // Create a channel to receive an error from the goroutine errCh := make(chan error, 1)