-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload_evidence_sse.go
52 lines (40 loc) · 1.49 KB
/
upload_evidence_sse.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
45
46
47
48
49
50
51
52
package donor
import (
"fmt"
"io"
"net/http"
"time"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
)
func UploadEvidenceSSE(documentStore DocumentStore, ttl time.Duration, flushFrequency time.Duration, now func() time.Time) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Content-Type", "text/event-stream")
documents, err := documentStore.GetAll(r.Context())
if err != nil {
printMessage("data: {\"closeConnection\": \"1\"}\n\n", w)
return nil
}
alreadyScannedCount := len(documents.Scanned())
batchToBeScannedCount := len(documents.NotScanned())
for start := now(); time.Since(start) < ttl; {
documents, err := documentStore.GetAll(r.Context())
if err != nil {
printMessage("data: {\"closeConnection\": \"1\"}\n\n", w)
return nil
}
scannedCount := len(documents.Scanned()) - alreadyScannedCount
printMessage(fmt.Sprintf("data: {\"finishedScanning\": %v, \"scannedCount\": %d}\n\n", scannedCount == batchToBeScannedCount, scannedCount), w)
time.Sleep(flushFrequency)
}
printMessage("data: {\"closeConnection\": \"1\"}\n\n", w)
return nil
}
}
func printMessage(message string, w io.Writer) {
fmt.Fprint(w, "event: message\n")
fmt.Fprint(w, message)
w.(http.Flusher).Flush()
}