mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-03 07:01:35 -06:00
WIP.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user