This commit is contained in:
Miroslav Šedivý
2023-11-27 01:12:33 +01:00
parent a6169da135
commit ab8c6a53ec
6 changed files with 167 additions and 0 deletions
+1
View File
@@ -61,5 +61,6 @@ func (manager *ApiManagerCtx) Mount(r chi.Router) {
r.Post("/recreate", manager.roomRecreate)
})
r.Get("/events", manager.events)
r.Get("/docker-compose.yaml", manager.dockerCompose)
}
+33
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
@@ -250,6 +251,38 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(ctx context.Context,
}
}
func (manager *ApiManagerCtx) events(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
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
}
fmt.Fprintf(w, "data: %s\n\n", jsonData)
flusher.Flush()
}
}
}
func (manager *ApiManagerCtx) dockerCompose(w http.ResponseWriter, r *http.Request) {
response, err := manager.rooms.ExportAsDockerCompose(r.Context())
if err != nil {