schnutibox/pkg/sselog/sselog.go

73 lines
1.4 KiB
Go
Raw Normal View History

2021-03-30 19:59:26 +02:00
// package sselog is work in progress to implement a writer that sends its logs
// to a http server side event.
2021-05-03 14:51:53 +02:00
// nolint:gochecknoglobals,godox
2021-03-30 19:59:26 +02:00
package sselog
2021-05-03 14:51:53 +02:00
import (
"fmt"
"net/http"
"go.xsfx.dev/logginghandler"
)
var Log *SSELog
2021-03-30 19:59:26 +02:00
type SSELog struct {
2021-05-03 14:51:53 +02:00
Receivers map[chan []byte]struct{}
}
func NewSSELog() *SSELog {
return &SSELog{
Receivers: make(map[chan []byte]struct{}),
}
2021-03-30 19:59:26 +02:00
}
func (l SSELog) Write(p []byte) (n int, err error) {
// Send log message to all receiver channels.
2021-05-03 14:51:53 +02:00
for r := range l.Receivers {
r <- p
}
return len(p), nil
}
2021-03-30 19:59:26 +02:00
2021-05-03 14:51:53 +02:00
func LogHandler(w http.ResponseWriter, r *http.Request) {
logger := logginghandler.Logger(r)
logger.Info().Msg("registering a new sse logger")
flusher, ok := w.(http.Flusher)
if !ok {
logger.Error().Msg("streaming unsupported")
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
2021-03-30 19:59:26 +02:00
}
2021-05-03 14:51:53 +02:00
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
// TODO: has to be something else!
w.Header().Set("Access-Control-Allow-Origin", "*")
cChan := make(chan []byte)
Log.Receivers[cChan] = struct{}{}
for {
select {
case e := <-cChan:
// Send event to client.
fmt.Fprintf(w, "data: %s\n\n", e)
// Send it right now and not buffering it.
flusher.Flush()
case <-r.Context().Done():
close(cChan)
delete(Log.Receivers, cChan)
return
}
}
2021-03-30 19:59:26 +02:00
}