-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
65 lines (55 loc) · 1.34 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"flag"
"fmt"
"math/big"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"github.com/tmaxmax/go-sse"
)
func main() {
var sub string
flag.StringVar(&sub, "sub", "all", "The topics to subscribe to. Valid values are: all, numbers, metrics")
flag.Parse()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
r, _ := http.NewRequestWithContext(ctx, http.MethodGet, getRequestURL(sub), http.NoBody)
conn := sse.NewConnection(r)
conn.SubscribeToAll(func(event sse.Event) {
switch event.Type {
case "cycles", "ops":
fmt.Printf("Metric %s: %s\n", event.Type, event.Data)
case "close":
fmt.Println("Server closed!")
cancel()
default: // no event name
var sum, num big.Int
for _, n := range strings.Split(event.Data, "\n") {
_, _ = num.SetString(n, 10)
sum.Add(&sum, &num)
}
fmt.Printf("Sum of random numbers: %s\n", &sum)
}
})
if err := conn.Connect(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func getRequestURL(sub string) string {
q := url.Values{}
switch sub {
case "all":
q.Add("topic", "numbers")
q.Add("topic", "metrics")
case "numbers", "metrics":
q.Set("topic", sub)
default:
panic(fmt.Errorf("unexpected subscription topic %q", sub))
}
return "http://localhost:8080/events?" + q.Encode()
}