1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

Upgrade to kv2 and add websocket connection to stulbe

This commit is contained in:
Ash Keel 2021-05-08 16:02:47 +02:00
parent 207be89ccd
commit 7efd1a18bf
No known key found for this signature in database
GPG key ID: CF2CC050478BD7E5
5 changed files with 60 additions and 9 deletions

2
go.mod
View file

@ -5,11 +5,13 @@ go 1.16
require (
github.com/dgraph-io/badger/v3 v3.2011.1
github.com/gempir/go-twitch-irc/v2 v2.5.0
github.com/gorilla/websocket v1.4.2
github.com/json-iterator/go v1.1.11
github.com/mattn/go-colorable v0.1.8
github.com/nicklaw5/helix v1.14.0
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/sirupsen/logrus v1.8.1
github.com/strimertul/kilovolt v0.1.2
github.com/strimertul/kilovolt/v2 v2.0.1
github.com/strimertul/stulbe v0.1.1
)

8
go.sum
View file

@ -21,7 +21,6 @@ github.com/dgraph-io/badger/v3 v3.2011.1 h1:Hmyof0WMEF/QtutX5SQHzIMnJQxb/IrSzhjc
github.com/dgraph-io/badger/v3 v3.2011.1/go.mod h1:0rLLrQpKVQAL0or/lBLMQznhr6dWWX7h5AKnmnqx268=
github.com/dgraph-io/ristretto v0.0.4-0.20210122082011-bb5d392ed82d h1:eQYOG6A4td1tht0NdJB9Ls6DsXRGb2Ft6X9REU/MbbE=
github.com/dgraph-io/ristretto v0.0.4-0.20210122082011-bb5d392ed82d/go.mod h1:tv2ec8nA7vRpSYX7/MbP52ihrUMXIHit54CQMq8npXQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
@ -45,7 +44,6 @@ github.com/google/flatbuffers v1.12.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-dap v0.2.0/go.mod h1:5q8aYQFnHOAZEMP+6vmq25HKYAEwE+LF5yh7JKrrhSQ=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
@ -72,7 +70,6 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/nicklaw5/helix v1.13.1 h1:J+DiwXMnYlY7paECl/wCEOUnOWGSs26XO/niDcskYHI=
github.com/nicklaw5/helix v1.13.1/go.mod h1:XeeXY7oY5W+MVMu6wF4qGm8uvjZ1/Nss0FqprVkXKrg=
github.com/nicklaw5/helix v1.14.0 h1:yJI+dUDxFzmlSelNygWs/lhirvuzCqgIXIZy05JdHVk=
github.com/nicklaw5/helix v1.14.0/go.mod h1:XeeXY7oY5W+MVMu6wF4qGm8uvjZ1/Nss0FqprVkXKrg=
@ -106,8 +103,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/strimertul/kilovolt v0.1.2 h1:11vERei0gNosc8mLgoqoVqrJmdA63BBShuIH2RKbLVo=
github.com/strimertul/kilovolt v0.1.2/go.mod h1:izWOzpeJX4z9MWDK/9B4Hbq4FRtKINh7OUU5E0He5DI=
github.com/strimertul/stulbe v0.1.0 h1:4GPWDdm0VU7JE3/NZWf/S1ks+Pu1yGfkqEcInOOSxE0=
github.com/strimertul/stulbe v0.1.0/go.mod h1:zZuI6uUKaOSfsxSslRS+y8ZYj5CY2REz07FjrfRxyr0=
github.com/strimertul/kilovolt/v2 v2.0.1 h1:hR/jWzOEc0NR5mBZ6i+R9bmnXO9hA0nbf5OrFheAFFU=
github.com/strimertul/kilovolt/v2 v2.0.1/go.mod h1:J/axcmsQe+sXlkEv2nEyDXSicxX3R3Nv+Bksa7vz544=
github.com/strimertul/stulbe v0.1.1 h1:+upiTvM8HZYUj2qNFwGN15CHkFh5M39ZJNwqeXZgun8=
github.com/strimertul/stulbe v0.1.1/go.mod h1:zZuI6uUKaOSfsxSslRS+y8ZYj5CY2REz07FjrfRxyr0=
github.com/twitchyliquid64/golang-asm v0.15.0/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
@ -123,7 +120,6 @@ golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210505212654-3497b51f5e64 h1:QuAh/1Gwc0d+u9walMU1NqzhRemNegsv5esp2ALQIY4=
golang.org/x/crypto v0.0.0-20210505212654-3497b51f5e64/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

View file

@ -9,7 +9,7 @@ import (
"runtime"
"time"
kv "github.com/strimertul/kilovolt"
kv "github.com/strimertul/kilovolt/v2"
"github.com/strimertul/strimertul/modules"
"github.com/strimertul/strimertul/modules/loyalty"
@ -145,6 +145,7 @@ func main() {
log.WithError(err).Error("Stulbe initialization failed! Module was temporarely disabled")
moduleConfig.EnableStulbe = false
}
defer stulbeClient.Close()
}
var loyaltyManager *loyalty.Manager

View file

@ -9,7 +9,7 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/sirupsen/logrus"
kv "github.com/strimertul/kilovolt"
kv "github.com/strimertul/kilovolt/v2"
"github.com/strimertul/strimertul/utils"
)

View file

@ -6,13 +6,15 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/dgraph-io/badger/v3"
"github.com/gorilla/websocket"
jsoniter "github.com/json-iterator/go"
"github.com/nicklaw5/helix"
"github.com/sirupsen/logrus"
kv "github.com/strimertul/kilovolt"
kv "github.com/strimertul/kilovolt/v2"
"github.com/strimertul/stulbe/api"
"github.com/strimertul/strimertul/utils"
@ -24,6 +26,7 @@ type Client struct {
client *http.Client
token string
logger logrus.FieldLogger
ws *websocket.Conn
}
func NewClient(db *badger.DB, hub *kv.Hub, logger logrus.FieldLogger) (*Client, error) {
@ -38,13 +41,25 @@ func NewClient(db *badger.DB, hub *kv.Hub, logger logrus.FieldLogger) (*Client,
token: "",
logger: logger,
client: &http.Client{},
ws: nil,
}
err = client.Authenticate(config.Username, config.AuthKey)
if err != nil {
return nil, err
}
err = client.ConnectToWebsocket()
return client, err
}
func (s *Client) Close() {
if s.ws != nil {
s.ws.Close()
}
}
func (s *Client) Authenticate(user string, authKey string) error {
body := new(bytes.Buffer)
err := jsoniter.ConfigFastest.NewEncoder(body).Encode(api.AuthRequest{User: user, AuthKey: authKey})
@ -74,6 +89,43 @@ func (s *Client) authenticated() bool {
return s.token != ""
}
func (s *Client) ConnectToWebsocket() error {
if !s.authenticated() {
return ErrNotAuthenticated
}
uri, err := url.Parse(fmt.Sprintf("%s/ws", s.Endpoint))
if err != nil {
return err
}
if uri.Scheme == "https" {
uri.Scheme = "wss"
} else {
uri.Scheme = "ws"
}
s.ws, _, err = websocket.DefaultDialer.Dial(uri.String(), http.Header{
"Authorization": []string{"Bearer " + s.token},
})
if err != nil {
return err
}
go func() {
s.logger.Info("connected to ws, reading")
for {
_, message, err := s.ws.ReadMessage()
if err != nil {
s.logger.WithError(err).Error("websocket read error")
return
}
s.logger.WithField("message", string(message)).Info("recv ws")
}
}()
return nil
}
func (s *Client) newAuthRequest(method string, url string, body io.Reader) (*http.Request, error) {
if !s.authenticated() {
return nil, ErrNotAuthenticated