mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
move events out of the rooms.
This commit is contained in:
+6
-1
@@ -61,6 +61,11 @@ func (manager *ApiManagerCtx) Mount(r chi.Router) {
|
|||||||
r.Post("/recreate", manager.roomRecreate)
|
r.Post("/recreate", manager.roomRecreate)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Get("/events", manager.events)
|
|
||||||
r.Get("/docker-compose.yaml", manager.dockerCompose)
|
r.Get("/docker-compose.yaml", manager.dockerCompose)
|
||||||
|
|
||||||
|
//
|
||||||
|
// events
|
||||||
|
//
|
||||||
|
|
||||||
|
r.Get("/events", manager.events)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (manager *ApiManagerCtx) events(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
|
||||||
|
sse := r.URL.Query().Has("sse")
|
||||||
|
if sse {
|
||||||
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
flusher, ok := w.(http.Flusher)
|
||||||
|
if !ok {
|
||||||
|
http.Error(w, "Connection does not support streaming", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var ping <-chan time.Time
|
||||||
|
if !sse {
|
||||||
|
// dummy channel, never ping
|
||||||
|
ping = make(<-chan time.Time)
|
||||||
|
} else {
|
||||||
|
// ping every 1 minute
|
||||||
|
ticker := time.NewTicker(time.Minute)
|
||||||
|
defer ticker.Stop()
|
||||||
|
ping = ticker.C
|
||||||
|
}
|
||||||
|
|
||||||
|
// listen for room events
|
||||||
|
events, errs := manager.rooms.Events(r.Context())
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ping:
|
||||||
|
fmt.Fprintf(w, ": ping\n\n")
|
||||||
|
flusher.Flush()
|
||||||
|
case _, ok := <-errs:
|
||||||
|
if !ok {
|
||||||
|
manager.logger.Debug().Msg("sse channel closed")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case e := <-events:
|
||||||
|
jsonData, err := json.Marshal(e)
|
||||||
|
if err != nil {
|
||||||
|
manager.logger.Err(err).Msg("failed to marshal event")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if sse {
|
||||||
|
fmt.Fprintf(w, "event: rooms\n")
|
||||||
|
fmt.Fprintf(w, "data: %s\n\n", jsonData)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "rooms\t%s\n", jsonData)
|
||||||
|
}
|
||||||
|
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -251,49 +250,6 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(ctx context.Context,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *ApiManagerCtx) events(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
|
||||||
w.Header().Set("Connection", "keep-alive")
|
|
||||||
|
|
||||||
sse := r.URL.Query().Has("sse")
|
|
||||||
if sse {
|
|
||||||
w.Header().Set("Content-Type", "text/event-stream")
|
|
||||||
} else {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
|
|
||||||
flusher, ok := w.(http.Flusher)
|
|
||||||
if !ok {
|
|
||||||
http.Error(w, "Connection does not support streaming", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
events, errs := manager.rooms.Events(r.Context())
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case _, ok := <-errs:
|
|
||||||
if !ok {
|
|
||||||
manager.logger.Debug().Msg("sse channel closed")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
case e := <-events:
|
|
||||||
jsonData, err := json.Marshal(e)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if sse {
|
|
||||||
fmt.Fprintf(w, "data: %s\n\n", jsonData)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(w, "%s\n", jsonData)
|
|
||||||
}
|
|
||||||
|
|
||||||
flusher.Flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (manager *ApiManagerCtx) dockerCompose(w http.ResponseWriter, r *http.Request) {
|
func (manager *ApiManagerCtx) dockerCompose(w http.ResponseWriter, r *http.Request) {
|
||||||
response, err := manager.rooms.ExportAsDockerCompose(r.Context())
|
response, err := manager.rooms.ExportAsDockerCompose(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user