This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
stream.go
102 lines (85 loc) · 1.99 KB
/
stream.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/fiatjaf/lightningd-gjson-rpc/plugin"
"github.com/tidwall/gjson"
"gopkg.in/antage/eventsource.v1"
)
type event struct {
typ string
data string
}
func checkStreamPermission(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if permissions, ok := r.Context().Value("permissions").(map[string]bool); ok {
if len(permissions) > 0 {
if _, allowed := permissions["stream"]; !allowed {
w.WriteHeader(401)
return
}
}
}
next.ServeHTTP(w, r)
})
}
func startStreams(p *plugin.Plugin) eventsource.EventSource {
id := 1
es := eventsource.New(
&eventsource.Settings{
Timeout: 5 * time.Second,
CloseOnTimeout: true,
IdleTimeout: 300 * time.Minute,
},
func(req *http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Cache-Control: no-cache"),
[]byte("Content-Type: text/event-stream"),
[]byte("Connection: keep-alive"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
ee = make(chan event)
go pollRate(p, ee)
go func() {
time.Sleep(1 * time.Second)
es.SendRetryMessage(3 * time.Second)
}()
go func() {
for {
time.Sleep(25 * time.Second)
es.SendEventMessage("", "keepalive", "")
}
}()
go func() {
for {
select {
case e := <-ee:
es.SendEventMessage(e.data, e.typ, strconv.Itoa(id))
}
id++
}
}()
return es
}
func pollRate(p *plugin.Plugin, ee chan<- event) {
time.Sleep(time.Minute * 1)
defer pollRate(p, ee)
resp, err := http.Get("https://www.bitstamp.net/api/v2/ticker/btcusd")
if err != nil || resp.StatusCode >= 300 {
p.Log(resp.StatusCode, " error fetching BTC price: ", err)
return
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
p.Log("Error decoding BTC price: ", err)
return
}
lastRate := gjson.GetBytes(b, "last").String()
ee <- event{typ: "btcusd", data: `"` + lastRate + `"`}
time.Sleep(time.Minute * 4)
}