openreplay/ee/backend/pkg/license/check.go
Alexander b92177a151
Assist-only feature (#2214)
* feat(http): small prep for task implementation

* feat(http): added assist-only check to ee

* feat(backend): using a new license check url

* feat(backend): packages upgrade
2024-05-28 09:41:16 +02:00

57 lines
1.2 KiB
Go

package license
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"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://api.openreplay.com/license/validate", "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.")
}
}