Skip to content

Commit b16c1ac

Browse files
committed
add zero situation
1 parent 962ea73 commit b16c1ac

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

src/binance-proxy/handler/kline.go

+54-53
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,68 @@
11
package handler
22

33
import (
4-
"encoding/json"
5-
"net/http"
6-
"strconv"
7-
"time"
4+
"encoding/json"
5+
"net/http"
6+
"strconv"
7+
"time"
88

9-
"binance-proxy/service"
9+
"binance-proxy/service"
1010
)
1111

1212
func (s *Handler) klines(w http.ResponseWriter, r *http.Request) {
13-
symbol := r.URL.Query().Get("symbol")
14-
interval := r.URL.Query().Get("interval")
15-
limitInt, _ := strconv.Atoi(r.URL.Query().Get("limit"))
16-
if limitInt == 0 {
17-
limitInt = 500
18-
}
13+
symbol := r.URL.Query().Get("symbol")
14+
interval := r.URL.Query().Get("interval")
15+
limitInt, _ := strconv.Atoi(r.URL.Query().Get("limit"))
16+
if limitInt == 0 {
17+
limitInt = 500
18+
}
1919

20-
startTimeUnix, _ := strconv.Atoi(r.URL.Query().Get("startTime"))
21-
startTime := time.Unix(int64(startTimeUnix/1000), 0)
20+
startTimeUnix, _ := strconv.Atoi(r.URL.Query().Get("startTime"))
21+
startTime := time.Unix(int64(startTimeUnix/1000), 0)
2222

23-
switch {
24-
case limitInt <= 0, limitInt > 1000,
25-
startTime.Unix() > 0 && startTime.Before(time.Now().Add(service.INTERVAL_2_DURATION[interval]*999*-1)),
26-
r.URL.Query().Get("endTime") != "",
27-
symbol == "", interval == "":
28-
s.reverseProxy(w, r)
29-
return
30-
}
23+
switch {
24+
case limitInt <= 0, limitInt > 1000,
25+
startTime.Unix() > 0 && startTime.Before(time.Now().Add(service.INTERVAL_2_DURATION[interval]*999*-1)),
26+
r.URL.Query().Get("endTime") != "",
27+
r.URL.Query().Get("startTime") == "0",
28+
symbol == "", interval == "":
29+
s.reverseProxy(w, r)
30+
return
31+
}
3132

32-
data := s.srv.Klines(symbol, interval)
33-
klines := make([]interface{}, 0)
34-
startTimeUnixMs := startTime.Unix() * 1000
35-
if startTimeUnixMs == 0 && limitInt < len(data) {
36-
data = data[len(data)-limitInt:]
37-
}
38-
for _, v := range data {
39-
if len(klines) >= limitInt {
40-
break
41-
}
33+
data := s.srv.Klines(symbol, interval)
34+
klines := make([]interface{}, 0)
35+
startTimeUnixMs := startTime.Unix() * 1000
36+
if startTimeUnixMs == 0 && limitInt < len(data) {
37+
data = data[len(data)-limitInt:]
38+
}
39+
for _, v := range data {
40+
if len(klines) >= limitInt {
41+
break
42+
}
4243

43-
if startTimeUnixMs > 0 && startTimeUnixMs > v.OpenTime {
44-
continue
45-
}
44+
if startTimeUnixMs > 0 && startTimeUnixMs > v.OpenTime {
45+
continue
46+
}
4647

47-
klines = append(klines, []interface{}{
48-
v.OpenTime,
49-
v.Open,
50-
v.High,
51-
v.Low,
52-
v.Close,
53-
v.Volume,
54-
v.CloseTime,
55-
v.QuoteAssetVolume,
56-
v.TradeNum,
57-
v.TakerBuyBaseAssetVolume,
58-
v.TakerBuyQuoteAssetVolume,
59-
"0",
60-
})
61-
}
48+
klines = append(klines, []interface{}{
49+
v.OpenTime,
50+
v.Open,
51+
v.High,
52+
v.Low,
53+
v.Close,
54+
v.Volume,
55+
v.CloseTime,
56+
v.QuoteAssetVolume,
57+
v.TradeNum,
58+
v.TakerBuyBaseAssetVolume,
59+
v.TakerBuyQuoteAssetVolume,
60+
"0",
61+
})
62+
}
6263

63-
w.Header().Set("Content-Type", "application/json")
64-
w.Header().Set("Data-Source", "websocket")
65-
j, _ := json.Marshal(klines)
66-
w.Write(j)
64+
w.Header().Set("Content-Type", "application/json")
65+
w.Header().Set("Data-Source", "websocket")
66+
j, _ := json.Marshal(klines)
67+
w.Write(j)
6768
}

0 commit comments

Comments
 (0)