-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
44 lines (38 loc) · 1.01 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package sse
import (
"context"
"fmt"
"net/http"
)
type Publisher interface {
Publish(ctx context.Context, event *Event) error
}
// Create a sender from a response writer
func Create(w http.ResponseWriter) (Publisher, error) {
flusher, ok := w.(http.Flusher)
if !ok {
return nil, fmt.Errorf("sse: response writer is not a flusher")
}
// Set the appropriate response headers
headers := w.Header()
headers.Add(`Content-Type`, `text/event-stream`)
headers.Add(`Cache-Control`, `no-cache`)
headers.Add(`Connection`, `keep-alive`)
headers.Add(`Access-Control-Allow-Origin`, "*")
w.WriteHeader(http.StatusOK)
// Flush the headers
flusher.Flush()
return &publisher{w, flusher}, nil
}
type publisher struct {
w http.ResponseWriter
f http.Flusher
}
var _ Publisher = (*publisher)(nil)
func (p *publisher) Publish(ctx context.Context, event *Event) error {
if _, err := p.w.Write(event.Format().Bytes()); err != nil {
return fmt.Errorf("sse: unable to publish event %s: %w", event, err)
}
p.f.Flush()
return nil
}