From b16c1ac3bc71355680930bbd65ea377f20a50e83 Mon Sep 17 00:00:00 2001 From: adriance Date: Thu, 16 Jun 2022 13:36:30 +0800 Subject: [PATCH] add zero situation --- src/binance-proxy/handler/kline.go | 107 +++++++++++++++-------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/src/binance-proxy/handler/kline.go b/src/binance-proxy/handler/kline.go index 067b95e..006915b 100644 --- a/src/binance-proxy/handler/kline.go +++ b/src/binance-proxy/handler/kline.go @@ -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) }