-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
41 lines (36 loc) · 1 KB
/
handler.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func chartHandler(p *priceModel) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
type pdata struct {
Price float64 `json:"price"`
RSI float64 `json:"rsi"`
ShortMomentum float64 `json:"shortMomentum"`
LongMomentum float64 `json:"longMomentum"`
BuySignal int `json:"buySignal"`
SellSignal int `json:"sellSignal"`
Action string `json:"action"`
}
for d := range p.data {
pd := pdata{
Price: d.price,
RSI: d.rsi,
ShortMomentum: d.shortMoment,
LongMomentum: d.longMoment,
BuySignal: d.buySignal,
SellSignal: d.sellSignal,
Action: d.action,
}
jsonData, _ := json.Marshal(pd)
fmt.Fprintf(w, "data: %s\n\n", jsonData)
w.(http.Flusher).Flush()
}
}
}