Files
radiantwave/archive/internal/license/license.go
T
2026-08-07 14:51:41 -05:00

74 lines
1.4 KiB
Go

package license
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"time"
)
var Level int
type Validator struct {
client *http.Client
apiURL string
apiPath string
}
type Response struct {
Key string `json:"key"`
Email string `json:"email"`
Active bool `json:"active"`
Type string `json:"type"`
Level string `json:"level"`
LastCheck time.Time `json:"lastCheck"`
NewCheck time.Time `json:"newCheck"`
}
func NewValidator(apiURL, apiPath string) *Validator {
return &Validator{
client: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
},
},
apiURL: apiURL,
apiPath: apiPath,
}
}
func (v *Validator) Validate(key, email string) (*Response, error) {
u, err := url.Parse(v.apiURL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, v.apiPath)
q := u.Query()
q.Set("licenseKey", key)
q.Set("emailAddress", email)
u.RawQuery = q.Encode()
resp, err := v.client.Get(u.String())
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status: %w", err)
}
var result Response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed ot decode response: %w", err)
}
return &result, nil
}