Skip to content

Commit

Permalink
add zero situation
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianceding committed Jun 16, 2022
1 parent 962ea73 commit b16c1ac
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions src/binance-proxy/handler/kline.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
package handler

import (
"encoding/json"
"net/http"
"strconv"
"time"
"encoding/json"
"net/http"
"strconv"
"time"

"binance-proxy/service"
"binance-proxy/service"
)

func (s *Handler) klines(w http.ResponseWriter, r *http.Request) {
symbol := r.URL.Query().Get("symbol")
interval := r.URL.Query().Get("interval")
limitInt, _ := strconv.Atoi(r.URL.Query().Get("limit"))
if limitInt == 0 {
limitInt = 500
}
symbol := r.URL.Query().Get("symbol")
interval := r.URL.Query().Get("interval")
limitInt, _ := strconv.Atoi(r.URL.Query().Get("limit"))
if limitInt == 0 {
limitInt = 500
}

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

switch {
case limitInt <= 0, limitInt > 1000,
startTime.Unix() > 0 && startTime.Before(time.Now().Add(service.INTERVAL_2_DURATION[interval]*999*-1)),
r.URL.Query().Get("endTime") != "",
symbol == "", interval == "":
s.reverseProxy(w, r)
return
}
switch {
case limitInt <= 0, limitInt > 1000,
startTime.Unix() > 0 && startTime.Before(time.Now().Add(service.INTERVAL_2_DURATION[interval]*999*-1)),
r.URL.Query().Get("endTime") != "",
r.URL.Query().Get("startTime") == "0",
symbol == "", interval == "":
s.reverseProxy(w, r)
return
}

data := s.srv.Klines(symbol, interval)
klines := make([]interface{}, 0)
startTimeUnixMs := startTime.Unix() * 1000
if startTimeUnixMs == 0 && limitInt < len(data) {
data = data[len(data)-limitInt:]
}
for _, v := range data {
if len(klines) >= limitInt {
break
}
data := s.srv.Klines(symbol, interval)
klines := make([]interface{}, 0)
startTimeUnixMs := startTime.Unix() * 1000
if startTimeUnixMs == 0 && limitInt < len(data) {
data = data[len(data)-limitInt:]
}
for _, v := range data {
if len(klines) >= limitInt {
break
}

if startTimeUnixMs > 0 && startTimeUnixMs > v.OpenTime {
continue
}
if startTimeUnixMs > 0 && startTimeUnixMs > v.OpenTime {
continue
}

klines = append(klines, []interface{}{
v.OpenTime,
v.Open,
v.High,
v.Low,
v.Close,
v.Volume,
v.CloseTime,
v.QuoteAssetVolume,
v.TradeNum,
v.TakerBuyBaseAssetVolume,
v.TakerBuyQuoteAssetVolume,
"0",
})
}
klines = append(klines, []interface{}{
v.OpenTime,
v.Open,
v.High,
v.Low,
v.Close,
v.Volume,
v.CloseTime,
v.QuoteAssetVolume,
v.TradeNum,
v.TakerBuyBaseAssetVolume,
v.TakerBuyQuoteAssetVolume,
"0",
})
}

w.Header().Set("Content-Type", "application/json")
w.Header().Set("Data-Source", "websocket")
j, _ := json.Marshal(klines)
w.Write(j)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Data-Source", "websocket")
j, _ := json.Marshal(klines)
w.Write(j)
}

0 comments on commit b16c1ac

Please sign in to comment.