feat (backend ee): check license

This commit is contained in:
ShiKhu 2021-05-10 19:44:28 +02:00
parent 207f6e7751
commit bddb08bda7
4 changed files with 70 additions and 2 deletions

View file

@ -4,6 +4,8 @@ import (
"log"
"database/sql"
_ "github.com/ClickHouse/clickhouse-go"
"openreplay/backend/pkg/license"
)
type Connector struct {
@ -27,6 +29,8 @@ type Connector struct {
}
func NewConnector(url string) *Connector {
license.CheckLicense()
db, err := sql.Open("clickhouse", url)
if err != nil {
log.Fatalln(err)

View file

@ -0,0 +1,61 @@
package license
import (
"log"
"net/http"
"encoding/json"
"io/ioutil"
"bytes"
"openreplay/backend/pkg/env"
)
type request struct {
MID string `json:"mid"`
License string `json:"license"`
}
type response struct {
Data struct {
IsValid bool `json:"valid"`
ExpirationTimestamp int64 `json:"expiration"`
} `json:"data"`
}
func CheckLicense() {
license := env.String("LICENSE_KEY")
requestBody, err := json.Marshal(request{ License: license })
if err != nil {
log.Fatal("Can not form a license check request.")
}
resp, err := http.Post("https://parrot.asayer.io/os/license", "application/json", bytes.NewReader(requestBody))
if err != nil {
log.Fatalf("Error while checking license. %v", err)
}
if resp.StatusCode != 200 {
log.Fatal("Error on license check request.")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error while reading license check response. %v", err)
}
respJson := new(response)
if err = json.Unmarshal(body, respJson); err != nil {
log.Fatalf("Error while parsing license check response. %v", err)
}
if !respJson.Data.IsValid {
log.Fatal("License is not valid.")
}
}

View file

@ -3,13 +3,16 @@ package queue
import (
"openreplay/backend/pkg/kafka"
"openreplay/backend/pkg/queue/types"
"openreplay/backend/pkg/license"
)
func NewConsumer(group string, topics []string, handler types.MessageHandler) types.Consumer {
license.CheckLicense()
return kafka.NewConsumer(group, topics, handler)
}
func NewProducer() types.Producer {
license.CheckLicense()
return kafka.NewProducer()
}

View file

@ -12,7 +12,7 @@ import (
)
var ch *clickhouse.Connector
var finalizeTicker *time.Ticker
var finalizeTicker <-chan time.Time
func initStats() {
ch = clickhouse.NewConnector(env.String("CLICKHOUSE_STRING"))
@ -20,7 +20,7 @@ func initStats() {
log.Fatalf("Clickhouse prepare error: %v\n", err)
}
finalizeTicker = time.NewTicker(20 * time.Minute)
finalizeTicker = time.Tick(20 * time.Minute)
}