12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217 |
- // main.go
- package main
- import (
- "compress/gzip"
- "encoding/json"
- "errors"
- "flag"
- "fmt"
- "io"
- "log"
- "net/http"
- _ "net/http/pprof"
- "os"
- "runtime"
- "strconv"
- "strings"
- "time"
- "tickserver/client"
- "tickserver/markinfo"
- "tickserver/server/market"
- )
- const (
- K_STYLE = iota
- T_STYLE
- )
- type Options struct {
- Start int `json:"start"`
- End int `json:"end"`
- Total int `json:"total_size"`
- }
- type KCandle struct {
- X []int `json:"x"`
- Y [][5]float32 `json:"y"`
- }
- type KVolume struct {
- X []int `json:"x"`
- Y []float32 `json:"y"`
- }
- type KData struct {
- C string `json:"c"`
- P string `json:"p"`
- Action string `json:"action"`
- End int `json:"end"`
- //KOptions Options `json:"options"`
- Candles KCandle `json:"main"`
- Volumes KVolume `json:"volumes|||__ignore__"`
- }
- type TData struct {
- End int `json:"end"`
- Candles []client.Candle `json:"data"`
- }
- var saddr1 = flag.String("s1", "127.0.0.1:19528", "tick server address 1") //115.231.103.7
- var saddr2 = flag.String("s2", "127.0.0.1:19528", "tick server address 2") //127.0.0.1
- var saddr3 = flag.String("s3", "127.0.0.1:19529", "tick server address 3") //115.236.75.194
- var saddr4 = flag.String("s4", "127.0.0.1:9090", "tick server address 4") //19528 9090
- var clientDown *client.ClientSimple
- //var clientDownTmp *client.ClientSimple
- type Conf struct {
- Saddr1 string //
- Saddr2 string //
- Saddr3 string //
- Saddr4 string
- }
- func readConf() (*Conf, error) {
- f, err := os.Open("webproxys.json")
- if err != nil {
- return nil, err
- }
- defer f.Close()
- dec := json.NewDecoder(f)
- conf := &Conf{}
- err = dec.Decode(conf)
- if err != nil {
- return nil, err
- }
- return conf, nil
- }
- func connectServer() (err error) {
- clientDown, err = client.NewClientSimple(*saddr1, *saddr2, *saddr3, *saddr4, "./tmp")
- if err != nil {
- //log.Println("new client", err)
- return err
- }
- //clientDownTmp, err = client.NewClientSimple("114.215.207.24:19528", "114.215.207.24:19528", "114.215.207.24:19529", "114.215.207.24:9090", "./tmp")
- //if err != nil {
- //log.Println("new client", err)
- //return err
- //}
- return nil
- }
- func changeCandle(candle, candle2, candle3 client.Candle) client.Candle {
- newCandle := candle
- if newCandle.High >= newCandle.Low*1.05 || newCandle.High >= candle2.Low*1.05 || newCandle.High >= candle3.Low*1.05 {
- //log.Println(newCandle)
- var low float64
- if newCandle.High >= newCandle.Low*1.05 {
- low = newCandle.Low
- }
- if newCandle.High >= candle2.Low*1.05 {
- low = candle2.Low
- }
- if newCandle.High >= candle3.Low*1.05 {
- low = candle3.Low
- }
- if newCandle.Open > newCandle.Close {
- newCandle.Open = low * 1.003
- newCandle.Close = low * 1.002
- } else {
- newCandle.Open = low * 1.002
- newCandle.Close = low * 1.003
- }
- newCandle.High = low * 1.005
- if newCandle.Low != low {
- newCandle.Low = low * 1.001
- }
- }
- return newCandle
- }
- func getData(symbol, period, action, count, out, cb, te, ts string, style int) (string, error) {
- iCount, err := strconv.Atoi(count)
- if action != "new" && err != nil {
- return "", errors.New("webserver doesn't support count")
- }
- if action == "new" {
- iCount = 1000
- }
- periodId, ok := market.PeriodIdMap[period]
- if !ok {
- return "", errors.New("webserver doesn't support period")
- }
- periodIdOld := periodId
- if periodId == market.M15 || periodId == market.M30 {
- periodId = market.M5
- if periodIdOld == market.M15 {
- iCount *= 3
- }
- if periodIdOld == market.M30 {
- iCount *= 6
- }
- }
- if periodId == market.H2 || periodId == market.H4 {
- periodId = market.H1
- if periodIdOld == market.H2 {
- iCount *= 2
- }
- if periodIdOld == market.H4 {
- iCount *= 4
- }
- }
- if periodId == market.W1 || periodId == market.MN1 {
- periodId = market.D1
- if periodIdOld == market.W1 {
- iCount *= 7
- }
- if periodIdOld == market.MN1 {
- iCount *= 31
- }
- }
- iCount++
- //startTime := time.Now().UnixNano()
- //log.Println("step1", symbol, period, action, count, out, cb, te, ts)
- symbolU := strings.ToUpper(symbol)
- if len(symbolU) == 3 {
- symbolU += "CNY"
- }
- _, err = markinfo.SymbolId(symbolU)
- if err != nil {
- return "", err
- }
- var prefix string
- prefix = market.BtyPrefix
- myInsId := prefix + symbolU
- var iTE, iTS int64
- var bufferedCandles []client.Candle
- bufferedCandles, _ = clientDown.GetLastCandles(myInsId, periodId, 0x7fffffff)
- /*bNormal := false
- for i, _ := range bufferedCandles {
- if myInsId == "bty_ETHUSDT" {
- if periodId == market.M1 {
- if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524035400000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524035400000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.M5 {
- if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524035100000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524035100000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.H1 {
- if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524031200000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524031200000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.D1 {
- if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
- if bufferedCandles[i].Open > bufferedCandles[i].Close {
- bufferedCandles[i].Low = bufferedCandles[i-1].Close
- } else {
- bufferedCandles[i].Low = bufferedCandles[i-1].Open
- }
- }
- }
- }
- if myInsId == "bty_BTCUSDT" {
- if periodId == market.M1 {
- if bufferedCandles[i].Timestamp >= 1524035160000 && bufferedCandles[i].Timestamp <= 1524035520000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524035520000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.M5 {
- if bufferedCandles[i].Timestamp >= 1524035100000 && bufferedCandles[i].Timestamp <= 1524035400000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524035400000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.H1 {
- if bufferedCandles[i].Timestamp >= 1524034800000 && bufferedCandles[i].Timestamp <= 1524034800000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.D1 {
- if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
- if bufferedCandles[i].Open > bufferedCandles[i].Close {
- bufferedCandles[i].Low = bufferedCandles[i-1].Close
- } else {
- bufferedCandles[i].Low = bufferedCandles[i-1].Open
- }
- }
- }
- }
- if myInsId == "bty_BCCUSDT" {
- if periodId == market.M1 {
- if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038580000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038580000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.M5 {
- if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524038100000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038100000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.H1 {
- if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.D1 {
- if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
- if bufferedCandles[i].Open > bufferedCandles[i].Close {
- bufferedCandles[i].Low = bufferedCandles[i-1].Close
- } else {
- bufferedCandles[i].Low = bufferedCandles[i-1].Open
- }
- }
- }
- }
- if myInsId == "bty_ETCUSDT" {
- if periodId == market.M1 {
- if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038520000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038520000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.M5 {
- if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524037800000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524037800000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.H1 {
- if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.D1 {
- if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
- if bufferedCandles[i].Open > bufferedCandles[i].Close {
- bufferedCandles[i].Low = bufferedCandles[i-1].Close
- } else {
- bufferedCandles[i].Low = bufferedCandles[i-1].Open
- }
- }
- }
- }
- if myInsId == "bty_ZECUSDT" {
- if periodId == market.M1 {
- if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038820000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038820000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.M5 {
- if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524038400000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038400000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.H1 {
- if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.D1 {
- if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
- if bufferedCandles[i].Open > bufferedCandles[i].Close {
- bufferedCandles[i].Low = bufferedCandles[i-1].Close
- } else {
- bufferedCandles[i].Low = bufferedCandles[i-1].Open
- }
- }
- }
- }
- if myInsId == "bty_LTCUSDT" {
- if periodId == market.M1 {
- if bufferedCandles[i].Timestamp >= 1524033780000 && bufferedCandles[i].Timestamp <= 1524038760000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038760000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.M5 {
- if bufferedCandles[i].Timestamp >= 1524033600000 && bufferedCandles[i].Timestamp <= 1524038400000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038400000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.H1 {
- if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.D1 {
- if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
- if bufferedCandles[i].Open > bufferedCandles[i].Close {
- bufferedCandles[i].Low = bufferedCandles[i-1].Close
- } else {
- bufferedCandles[i].Low = bufferedCandles[i-1].Open
- }
- }
- }
- }
- if myInsId == "bty_DCRUSDT" {
- if periodId == market.M1 {
- if bufferedCandles[i].Timestamp >= 1524033840000 && bufferedCandles[i].Timestamp <= 1524038520000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038520000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.M5 {
- if bufferedCandles[i].Timestamp >= 1524033900000 && bufferedCandles[i].Timestamp <= 1524038100000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524038100000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.H1 {
- if bufferedCandles[i].Timestamp >= 1524031200000 && bufferedCandles[i].Timestamp <= 1524034800000 {
- bufferedCandles[i].High = bufferedCandles[i-1].High
- bufferedCandles[i].Open = bufferedCandles[i-1].Open
- bufferedCandles[i].Close = bufferedCandles[i-1].Close
- bufferedCandles[i].Low = bufferedCandles[i-1].Low
- }
- if !bNormal && bufferedCandles[i].Timestamp > 1524034800000 {
- bNormal = true
- bufferedCandles[i].Open = bufferedCandles[i-1].Close
- if bufferedCandles[i].High < bufferedCandles[i].Open {
- bufferedCandles[i].High = bufferedCandles[i].Open
- }
- if bufferedCandles[i].Low > bufferedCandles[i].Open {
- bufferedCandles[i].Low = bufferedCandles[i].Open
- }
- }
- }
- if periodId == market.D1 {
- if bufferedCandles[i].Timestamp >= 1524009600000 && bufferedCandles[i].Timestamp <= 1524009600000 {
- if bufferedCandles[i].Open > bufferedCandles[i].Close {
- bufferedCandles[i].Low = bufferedCandles[i-1].Close
- } else {
- bufferedCandles[i].Low = bufferedCandles[i-1].Open
- }
- }
- }
- }
- }*/
- bufferedCandles = truncateBuffer(bufferedCandles, periodId, myInsId)
- var downN int
- var downTS int64
- switch action {
- case "init":
- if len(bufferedCandles) >= iCount {
- bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
- downN = 0
- downTS = 0
- } else {
- downN = -(iCount - len(bufferedCandles))
- downTS = -1
- }
- case "new":
- iTE, _ = strconv.ParseInt(te, 10, 64)
- iTE *= 1000
- beginTimeBuffered := int64(0x7fffffff)
- if len(bufferedCandles) > 0 {
- beginTimeBuffered = bufferedCandles[0].Timestamp
- }
- for cindex := 0; cindex < len(bufferedCandles); cindex++ {
- if bufferedCandles[cindex].Timestamp >= iTE {
- bufferedCandles = bufferedCandles[cindex:]
- break
- }
- }
- if beginTimeBuffered <= iTE {
- downN = 0
- downTS = 0
- if len(bufferedCandles) >= iCount {
- bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
- }
- } else {
- if len(bufferedCandles) >= iCount {
- bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
- downN = 0
- downTS = 0
- } else {
- if len(bufferedCandles) == 0 {
- downN = iCount
- downTS = iTE
- } else {
- downN = -(iCount - len(bufferedCandles))
- downTS = -1
- }
- }
- }
- case "down":
- iTS, _ = strconv.ParseInt(ts, 10, 64)
- iTS *= 1000
- for cindex := 0; cindex < len(bufferedCandles); cindex++ {
- if bufferedCandles[cindex].Timestamp > iTS {
- bufferedCandles = bufferedCandles[:cindex]
- break
- }
- }
- if len(bufferedCandles) >= iCount {
- bufferedCandles = bufferedCandles[len(bufferedCandles)-iCount:]
- downN = 0
- downTS = 0
- } else {
- if len(bufferedCandles) == 0 {
- downN = -iCount
- downTS = iTS
- } else {
- downN = -(iCount - len(bufferedCandles))
- downTS = -1
- }
- }
- }
- //log.Println("step2", len(bufferedCandles), downN, downTS)
- var filecandles []client.Candle
- _, filecandles, _ = clientDown.GetHistory(myInsId, periodId, downN, downTS)
- //log.Println("step3", len(filecandles))
- candles := make([]client.Candle, len(bufferedCandles)+len(filecandles))
- for findex := 0; findex <= 1; findex++ {
- if findex == 1 {
- for iBuf := 0; iBuf < len(bufferedCandles); iBuf++ {
- //bufferedCandles[iBuf] = changeCandle(bufferedCandles[iBuf], strings.ToUpper(period), strings.ToUpper(symbol))
- candles[len(filecandles)+iBuf] = bufferedCandles[iBuf]
- }
- } else {
- if downN < 0 {
- for iFile := len(filecandles) - 1; iFile >= 0; iFile-- {
- //filecandles[iFile] = changeCandle(filecandles[iFile], strings.ToUpper(period), strings.ToUpper(symbol))
- candles[len(filecandles)-iFile-1] = filecandles[iFile]
- }
- } else {
- for iFile := 0; iFile < len(filecandles); iFile++ {
- //filecandles[iFile] = changeCandle(filecandles[iFile], strings.ToUpper(period), strings.ToUpper(symbol))
- candles[iFile] = filecandles[iFile]
- }
- }
- }
- }
- var tData TData
- var kData KData
- if K_STYLE == style {
- kData.C = symbol
- kData.P = period
- kData.Action = action
- }
- if len(candles) >= iCount {
- if T_STYLE == style {
- tData.End = 0
- }
- if K_STYLE == style {
- kData.End = 0
- }
- } else {
- if T_STYLE == style {
- tData.End = 1
- }
- if K_STYLE == style {
- kData.End = 1
- }
- }
- if periodIdOld == market.M15 || periodIdOld == market.M30 ||
- periodIdOld == market.H2 || periodIdOld == market.H4 ||
- periodIdOld == market.W1 || periodIdOld == market.MN1 {
- candles = makeCandle(candles, periodIdOld, myInsId)
- }
- var candleLast client.Candle
- for cindex := 0; cindex < len(candles); cindex++ {
- if candles[cindex].Timestamp < candleLast.Timestamp {
- continue
- }
- bduplicated := false
- if candles[cindex].Timestamp == candleLast.Timestamp {
- bduplicated = true
- }
- if action == "down" && candles[cindex].Timestamp > iTS {
- break
- }
- if action == "new" && candles[cindex].Timestamp < iTE {
- continue
- }
- if candles[cindex].Open == 0 ||
- candles[cindex].High == 0 ||
- candles[cindex].Low == 0 ||
- candles[cindex].Close == 0 {
- candles[cindex] = candleLast
- }
- if bduplicated {
- if candleLast.RealVolums < candles[cindex].RealVolums {
- if T_STYLE == style {
- tData.Candles[len(tData.Candles)-1] = candles[cindex]
- }
- if K_STYLE == style {
- kData.Candles.X[len(kData.Candles.X)-1] = int(candles[cindex].Timestamp / 1000)
- kData.Candles.Y[len(kData.Candles.Y)-1] = [5]float32{float32(candles[cindex].Open), float32(candles[cindex].High), float32(candles[cindex].Low), float32(candles[cindex].Close), float32(candles[cindex].RealVolums)}
- kData.Volumes.X[len(kData.Volumes.X)-1] = int(candles[cindex].Timestamp / 1000)
- kData.Volumes.Y[len(kData.Volumes.Y)-1] = float32(candles[cindex].RealVolums)
- }
- candleLast = candles[cindex]
- }
- } else {
- if T_STYLE == style {
- tData.Candles = append(tData.Candles, candles[cindex])
- }
- if K_STYLE == style {
- kData.Candles.X = append(kData.Candles.X, int(candles[cindex].Timestamp/1000))
- kData.Candles.Y = append(kData.Candles.Y, [5]float32{float32(candles[cindex].Open), float32(candles[cindex].High), float32(candles[cindex].Low), float32(candles[cindex].Close), float32(candles[cindex].RealVolums)})
- kData.Volumes.X = append(kData.Volumes.X, int(candles[cindex].Timestamp/1000))
- kData.Volumes.Y = append(kData.Volumes.Y, float32(candles[cindex].RealVolums))
- }
- candleLast = candles[cindex]
- }
- }
- var dataLen int
- if T_STYLE == style {
- dataLen = len(tData.Candles)
- }
- if K_STYLE == style {
- dataLen = len(kData.Candles.X)
- }
- if dataLen >= iCount {
- if T_STYLE == style {
- tData.Candles = tData.Candles[dataLen-(iCount-1):]
- }
- if K_STYLE == style {
- kData.Candles.X = kData.Candles.X[dataLen-(iCount-1):]
- kData.Candles.Y = kData.Candles.Y[dataLen-(iCount-1):]
- kData.Volumes.X = kData.Volumes.X[dataLen-(iCount-1):]
- kData.Volumes.Y = kData.Volumes.Y[dataLen-(iCount-1):]
- }
- }
- var b []byte
- if K_STYLE == style {
- b, err = json.Marshal(&kData)
- }
- if T_STYLE == style {
- b, err = json.Marshal(&tData)
- }
- if err != nil {
- return "", errors.New("marshal error")
- }
- var output string
- if out == "jsonp" {
- output = fmt.Sprintf("if (%s) %s(%s)\n", cb, cb, string(b))
- } else {
- output = string(b)
- }
- return output, nil
- }
- func kdataHandler(w http.ResponseWriter, r *http.Request) {
- symbol := r.FormValue("c")
- period := r.FormValue("p")
- action := r.FormValue("action")
- count := r.FormValue("count")
- out := r.FormValue("out")
- cb := r.FormValue("callback")
- var ts, te string
- if action == "new" {
- te = r.FormValue("te")
- }
- if action == "down" {
- ts = r.FormValue("ts")
- }
- output, err := getData(symbol, period, action, count, out, cb, te, ts, K_STYLE)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- if out != "jsonp" {
- w.Header().Set("Access-Control-Allow-Origin", "*")
- w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
- w.Header().Set("content-type", "application/json")
- }
- io.WriteString(w, output)
- }
- func tdataHandler(w http.ResponseWriter, r *http.Request) {
- symbol := r.FormValue("c")
- period := r.FormValue("p")
- action := r.FormValue("action")
- count := r.FormValue("count")
- out := r.FormValue("out")
- cb := r.FormValue("callback")
- var ts, te string
- if action == "new" {
- te = r.FormValue("te")
- }
- if action == "down" {
- ts = r.FormValue("ts")
- }
- output, err := getData(symbol, period, action, count, out, cb, te, ts, T_STYLE)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- if out != "jsonp" {
- w.Header().Set("Access-Control-Allow-Origin", "*")
- w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
- w.Header().Set("content-type", "application/json")
- }
- io.WriteString(w, output)
- }
- func makeCandle(candles []client.Candle, periodIdOld int, insId string) []client.Candle {
- var candlestmp []market.Candle
- for _, v := range candles {
- candlestmp = append(candlestmp, market.Candle{
- Timestamp: v.Timestamp,
- Open: v.Open,
- High: v.High,
- Low: v.Low,
- Close: v.Close,
- RealVolums: v.RealVolums,
- TickVolums: v.TickVolums,
- })
- }
- r := market.NewCandleBuf(candlestmp)
- candlestmpnew, _ := market.ConvPeriod(r, insId, periodIdOld)
- candles = candles[0:0]
- for _, v := range candlestmpnew {
- candles = append(candles, client.Candle{
- Timestamp: v.Timestamp,
- Open: v.Open,
- High: v.High,
- Low: v.Low,
- Close: v.Close,
- RealVolums: v.RealVolums,
- TickVolums: v.TickVolums,
- })
- }
- return candles
- }
- func truncateBuffer(bufferedCandles []client.Candle, periodId int, insId string) []client.Candle {
- var filecandlelatesttime int64
- tmpDownN := -2
- var tmpfilecandles []client.Candle
- _, tmpfilecandles, _ = clientDown.GetHistory(insId, periodId, tmpDownN, -1)
- if len(tmpfilecandles) > 1 {
- filecandlelatesttime = tmpfilecandles[1].Timestamp
- /*if insId != "bty_BTYUSDT" && len(bufferedCandles) > 0 && bufferedCandles[0].Timestamp > tmpfilecandles[0].Timestamp {
- bufferedCandlesTmp := bufferedCandles[:]
- bufferedCandles = make([]client.Candle, len(bufferedCandlesTmp))
- for timestampAdded := tmpfilecandles[0].Timestamp + int64(periodId*1000); timestampAdded < bufferedCandlesTmp[0].Timestamp; timestampAdded += int64(periodId * 1000) {
- candleAdded := bufferedCandlesTmp[0]
- candleAdded.Timestamp = timestampAdded
- candleAdded.TickVolums = 0
- candleAdded.RealVolums = 0
- bufferedCandles = append(bufferedCandles, candleAdded)
- }
- bufferedCandles = append(bufferedCandles, bufferedCandlesTmp...)
- }*/
- var iPos int
- for iPos = 0; iPos < len(bufferedCandles); iPos++ {
- if bufferedCandles[iPos].Timestamp > filecandlelatesttime {
- break
- }
- }
- bufferedCandles = bufferedCandles[iPos:]
- }
- return bufferedCandles
- }
- func patchingByVolume(candles []client.Candle, periodId int, bAsc bool, bBuffered bool) []client.Candle {
- bNeeded := false
- for i := 0; i < len(candles); i++ {
- if candles[i].TickVolums == 0 {
- bNeeded = true
- break
- }
- }
- if bNeeded {
- var st, et int64
- var posHuobi int
- //var posOkCoin int
- if bAsc {
- st = candles[0].Timestamp
- et = candles[len(candles)-1].Timestamp
- } else {
- st = candles[len(candles)-1].Timestamp
- et = candles[0].Timestamp
- }
- var candlesHuobi []client.Candle
- if bBuffered {
- candlesHuobi, _ = clientDown.GetLastCandles("huobi_BTCCNY", periodId, 0x7fffffff)
- //candlesOkCoin, _ = clientDown.GetLastCandles("btc_BTCCNY", periodId, 0x7fffffff)
- } else {
- _, candlesHuobi, _ = clientDown.GetHistoryEx("huobi_BTCCNY", periodId, st, et)
- //_, candlesOkCoin, _ = clientDown.GetHistoryEx("btc_BTCCNY", periodId, st, et)
- }
- if bAsc {
- posHuobi = 0
- //posOkCoin = 0
- } else {
- posHuobi = len(candlesHuobi) - 1
- //posOkCoin = len(filecandlesOkCoin) - 1
- }
- candlesTmp := candles[:]
- candles = candles[0:0]
- for i := 0; i < len(candlesTmp); i++ {
- if candlesTmp[i].TickVolums == 0 {
- count := 0
- var candleHuobi client.Candle
- //var candleOkCoin client.Candle
- if bAsc {
- for j := posHuobi; j < len(candlesHuobi); j++ {
- if candlesHuobi[j].Timestamp == candlesTmp[i].Timestamp {
- posHuobi = j
- count++
- candleHuobi = candlesHuobi[j]
- break
- }
- if candlesHuobi[j].Timestamp > candlesTmp[i].Timestamp {
- posHuobi = j
- break
- }
- }
- /*for k := posOkCoin; k < len(filecandlesOkCoin); k++ {
- if filecandlesOkCoin[k].Timestamp == filecandlesTmp[i].Timestamp {
- posOkCoin = k
- count++
- candleOkCoin = filecandlesOkCoin[k]
- break
- }
- if filecandlesOkCoin[k].Timestamp > filecandlesTmp[i].Timestamp {
- posOkCoin = k
- break
- }
- }*/
- } else {
- for j := posHuobi; j >= 0; j-- {
- if candlesHuobi[j].Timestamp == candlesTmp[i].Timestamp {
- posHuobi = j
- count++
- candleHuobi = candlesHuobi[j]
- break
- }
- if candlesHuobi[j].Timestamp < candlesTmp[i].Timestamp {
- posHuobi = j
- break
- }
- }
- /*for k := posOkCoin; k >= 0; k-- {
- if filecandlesOkCoin[k].Timestamp == filecandlesTmp[i].Timestamp {
- posOkCoin = k
- count++
- candleOkCoin = filecandlesOkCoin[k]
- break
- }
- if filecandlesOkCoin[k].Timestamp < filecandlesTmp[i].Timestamp {
- posOkCoin = k
- break
- }
- }*/
- }
- if count > 0 {
- var candleComplex client.Candle
- count = 0
- if candleHuobi.Open > 0 && candleHuobi.High > 0 && candleHuobi.Low > 0 && candleHuobi.Close > 0 {
- count++
- candleComplex.Open += candleHuobi.Open
- candleComplex.High += candleHuobi.High
- candleComplex.Low += candleHuobi.Low
- candleComplex.Close += candleHuobi.Close
- }
- /*if candleOkCoin.Open > 0 && candleOkCoin.High > 0 && candleOkCoin.Low > 0 && candleOkCoin.Close > 0 {
- count++
- candleComplex.Open += candleOkCoin.Open
- candleComplex.High += candleOkCoin.High
- candleComplex.Low += candleOkCoin.Low
- candleComplex.Close += candleOkCoin.Close
- }*/
- //filecandles = append(filecandles, client.Candle{Timestamp: filecandlesTmp[i].Timestamp, Open: candleComplex.Open / float64(count),
- //High: candleComplex.High / float64(count), Low: candleComplex.Low / float64(count),
- //Close: candleComplex.Close / float64(count), RealVolums: (candleHuobi.TickVolums + candleOkCoin.TickVolums) * 150, TickVolums: (candleHuobi.TickVolums + candleOkCoin.TickVolums) / 2})
- candleHuobi.RealVolums /= 1e6
- candles = append(candles, candleHuobi)
- //log.Println("1", candleHuobi)
- } else {
- candles = append(candles, candlesTmp[i])
- //log.Println("2", candlesTmp[i])
- }
- } else {
- candles = append(candles, candlesTmp[i])
- //log.Println("3", candlesTmp[i])
- }
- }
- }
- return candles
- }
- func patchingByTime(candles []client.Candle, periodId int, bAsc bool) []client.Candle {
- bNeedAdd := false
- for i := 0; i < len(candles); i++ {
- if bAsc {
- if i > 0 && ((candles[i].Timestamp - candles[i-1].Timestamp) > int64(periodId*1000)) {
- bNeedAdd = true
- break
- }
- } else {
- if i > 0 && ((candles[i-1].Timestamp - candles[i].Timestamp) > int64(periodId*1000)) {
- bNeedAdd = true
- break
- }
- }
- }
- if bNeedAdd {
- candlesTmp := make([]client.Candle, len(candles))
- for k, v := range candles {
- candlesTmp[k] = v
- }
- candles = candles[0:0]
- for i := 0; i < len(candlesTmp); i++ {
- if bAsc {
- if i > 0 && ((candlesTmp[i].Timestamp - candlesTmp[i-1].Timestamp) > int64(periodId*1000)) {
- for timestampAdded := candlesTmp[i-1].Timestamp + int64(periodId*1000); timestampAdded < candlesTmp[i].Timestamp; timestampAdded += int64(periodId * 1000) {
- candleAdded := candlesTmp[i-1]
- candleAdded.Timestamp = timestampAdded
- candleAdded.TickVolums = 0
- candles = append(candles, candleAdded)
- }
- }
- } else {
- if i > 0 && ((candlesTmp[i-1].Timestamp - candlesTmp[i].Timestamp) > int64(periodId*1000)) {
- for timestampAdded := candlesTmp[i-1].Timestamp - int64(periodId*1000); timestampAdded > candlesTmp[i].Timestamp; timestampAdded -= int64(periodId * 1000) {
- candleAdded := candlesTmp[i-1]
- candleAdded.Timestamp = timestampAdded
- candleAdded.TickVolums = 0
- candles = append(candles, candleAdded)
- }
- }
- }
- candles = append(candles, candlesTmp[i])
- }
- }
- return candles
- }
- type gzipResponseWriter struct {
- io.Writer
- http.ResponseWriter
- }
- func (w gzipResponseWriter) Write(b []byte) (int, error) {
- return w.Writer.Write(b)
- }
- func (w gzipResponseWriter) Flush() {
- w.Writer.(*gzip.Writer).Flush()
- w.ResponseWriter.(http.Flusher).Flush()
- }
- func makeGzipHandler(fn http.HandlerFunc) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
- fn(w, r)
- return
- }
- w.Header().Set("Content-Encoding", "gzip")
- w.Header().Set("Content-Type", "text/javascript")
- gz := gzip.NewWriter(w)
- defer gz.Close()
- fn(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
- }
- }
- func main() {
- runtime.GOMAXPROCS(runtime.NumCPU())
- conf, err := readConf()
- if err != nil {
- flag.Parse()
- } else {
- *saddr1 = conf.Saddr1
- *saddr2 = conf.Saddr2
- *saddr3 = conf.Saddr3
- *saddr4 = conf.Saddr4
- }
- err = connectServer()
- if err != nil {
- log.Fatal("connect server", err)
- }
- s := &http.Server{
- //Addr: ":3062",
- Addr: ":4062",
- //Addr: ":9062",
- ReadTimeout: 10 * time.Second,
- WriteTimeout: 10 * time.Second,
- MaxHeaderBytes: 1 << 20,
- }
- http.HandleFunc("/kdata", makeGzipHandler(kdataHandler))
- http.HandleFunc("/tdata", makeGzipHandler(tdataHandler))
- log.Fatal(s.ListenAndServeTLS("214341259320977.pem", "214341259320977.key"))
- //log.Fatal(s.ListenAndServe())
- //log.Fatal(s.ListenAndServeTLS("licai20170620.pem", "licai20170620.key"))
- }
|