From d463135d767a2217015f949240ab6a21d4cf404e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=8F=D1=80=D1=88=D0=B8=D0=BD=20=D0=94=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=81=20=D0=98=D0=B3=D0=BE=D1=80=D0=B5=D0=B2=D0=B8?= =?UTF-8?q?=D1=87?= Date: Wed, 18 Jun 2025 21:29:24 +0500 Subject: [PATCH 1/2] upgrade go version and actualize callback event --- .github/workflows/main.yml | 4 +- .github/workflows/publish.yml | 2 +- go.mod | 2 +- internal/quik/models.go | 21 ++++++-- quik/init.lua | 91 +++++++++++++++++++---------------- 5 files changed, 71 insertions(+), 49 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ce71207..a69901c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.22', '1.23'] + go-version: [ '1.23', '1.24'] steps: - name: Check out code @@ -33,7 +33,7 @@ jobs: - name: Run golangci-lint run: golangci-lint run ./... - + - name: Install gosec run: go install github.com/securego/gosec/v2/cmd/gosec@latest diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6179257..409e7ad 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.22', '1.23'] + go-version: [ '1.23', '1.24'] steps: - name: Checkout code diff --git a/go.mod b/go.mod index b0eb63f..e3246f1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/DBoyara/QuikGo -go 1.22 +go 1.23 require ( github.com/pkg/errors v0.9.1 diff --git a/internal/quik/models.go b/internal/quik/models.go index 548bc1e..4093fab 100644 --- a/internal/quik/models.go +++ b/internal/quik/models.go @@ -190,7 +190,22 @@ func putRequest(req *request) { // Event — структура для обработки колбэков от QUIK. type Event struct { - Cmd string `json:"cmd"` - T int64 `json:"t"` - Data interface{} `json:"data"` + Cmd string `json:"cmd"` + T int64 `json:"t"` + OrderBook *OrderBook `json:"order_book,omitempty"` + Data *interface{} `json:"data,omitempty"` +} + +type OrderBook struct { + Bid []Order `json:"bid"` + Offer []Order `json:"offer"` + ClassCode string `json:"class_code"` + SecCode string `json:"sec_code"` + BidCount string `json:"bid_count"` + OfferCount string `json:"offer_count"` +} + +type Order struct { + Price string `json:"price"` + Quantity string `json:"quantity"` } diff --git a/quik/init.lua b/quik/init.lua index 67a3d85..842a319 100644 --- a/quik/init.lua +++ b/quik/init.lua @@ -53,7 +53,7 @@ function connect_to_go() end log("Подключение к Go-серверу установлено!", 1) - + client:settimeout(0) return client end @@ -63,12 +63,12 @@ local event_client = connect_to_go() function to_json(msg) local status, str = pcall(json.encode, msg, { indent = false }) - - if status then - return str - else + + if status then + return str + else log("Ошибка сериализации JSON: " .. tostring(str), 3) - return nil + return nil end end @@ -383,9 +383,9 @@ function OnQuote(class_code, sec_code) local msg = {} local status, ql2 = pcall(getQuoteLevel2, class_code, sec_code) if status then - msg.data = ql2 - msg.data.class_code = class_code - msg.data.sec_code = sec_code + msg = ql2 + msg.class_code = class_code + msg.sec_code = sec_code send_event("OnQuote", msg) else @@ -481,9 +481,9 @@ function subscribe_to_order_book(class_code, sec_code) local msg = {} local status, ql2 = pcall(getQuoteLevel2, class_code, sec_code) if status then - msg.data = ql2 - msg.data.class_code = class_code - msg.data.sec_code = sec_code + msg = ql2 + msg.class_code = class_code + msg.sec_code = sec_code -- Отправляем данные клиенту send_event("OrderBookUpdate", msg) @@ -505,7 +505,14 @@ function send_event(event_name, event_data) return end - local msg = json.encode({ cmd = event_name, data = event_data }) .. "\n" + if event_name == "OnQuote" then + local msg = json.encode({ cmd = event_name, order_book = event_data }) .. "\n" + elseif event_name == "OrderBookUpdate " then + local msg = json.encode({ cmd = event_name, order_book = event_data }) .. "\n" + else + local msg = json.encode({ cmd = event_name, data = event_data }) .. "\n" + end + local success, err = event_client:send(msg) if success then @@ -611,9 +618,9 @@ function reconnect_to_go(max_attempts, delay) for attempt = 1, max_attempts do log("Попытка подключения #" .. attempt, 2) - + event_client = connect_to_go() - + if event_client then log("Успешное подключение к Go-серверу!", 1) return true @@ -630,44 +637,44 @@ end -- Основная функция запуска сервера и обработки запросов. function main() response_server = setup_socket() - + if not response_server then log("Ошибка при настройке сокета", 3) return end - while true do - if not is_connected then + while true do + if not is_connected then response_client = accept_client() - - if not response_client then + + if not response_client then sleep(500) -- Ждём перед повторной проверкой подключения клиента. - else - is_connected = true - log("Клиент успешно подключен",1) - + else + is_connected = true + log("Клиент успешно подключен",1) + -- Проверяем соединение с Go-сервером и переподключаемся при необходимости. - if not event_client or not reconnect_to_go() then - log("Не удалось установить соединение с Go. Завершение работы.",3) - break - end + if not event_client or not reconnect_to_go() then + log("Не удалось установить соединение с Go. Завершение работы.",3) + break + end end - - else + + else local request, err = receive_message(response_client) - if request then - local response = process_request(request) + if request then + local response = process_request(request) send_response(response_client, response) - elseif err and err ~= "timeout" then + elseif err and err ~= "timeout" then -- Ошибка связи или отключение клиента. - log ("Клиент отключился: "..err ,3 ) - response_client=nil - is_connected=false - end - end - - sleep(100) -- Пауза перед следующим циклом обработки событий. - end -end + log ("Клиент отключился: "..err ,3 ) + response_client=nil + is_connected=false + end + end + + sleep(100) -- Пауза перед следующим циклом обработки событий. + end +end From 2533914672a5d124955c35dd3fa9ebb90cf2f7f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=8F=D1=80=D1=88=D0=B8=D0=BD=20=D0=94=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=81=20=D0=98=D0=B3=D0=BE=D1=80=D0=B5=D0=B2=D0=B8?= =?UTF-8?q?=D1=87?= Date: Wed, 18 Jun 2025 21:31:07 +0500 Subject: [PATCH 2/2] fix --- .github/workflows/main.yml | 2 +- .github/workflows/publish.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a69901c..25a83e6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.23', '1.24'] + go-version: [ '1.23'] steps: - name: Check out code diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 409e7ad..7871024 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.23', '1.24'] + go-version: [ '1.23'] steps: - name: Checkout code