diff --git a/.golangci.yaml b/.golangci.yaml index a7082e2..fd51cea 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -51,6 +51,8 @@ linters-settings: disabled: true - name: early-return disabled: true + - name: disable-stuttering-check + disabled: true gocritic: disabled-checks: - ifElseChain diff --git a/go.mod b/go.mod index 47b8fe9..5d6bbd2 100644 --- a/go.mod +++ b/go.mod @@ -80,6 +80,7 @@ require ( github.com/h2non/filetype v1.1.3 github.com/improbable-eng/grpc-web v0.15.0 github.com/mssola/useragent v1.0.0 + github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 go.deanishe.net/favicon v0.1.0 go.uber.org/zap v1.21.0 diff --git a/go.sum b/go.sum index b62ea48..cf13e79 100644 --- a/go.sum +++ b/go.sum @@ -422,6 +422,8 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= diff --git a/plugin/license/cache.go b/plugin/license/cache.go new file mode 100644 index 0000000..8591713 --- /dev/null +++ b/plugin/license/cache.go @@ -0,0 +1,24 @@ +package license + +import ( + "fmt" + "time" + + "github.com/patrickmn/go-cache" +) + +var ( + licenseCache = cache.New(24*time.Hour, 24*time.Hour) +) + +func SetLicenseCache(licenseKey, instanceName string, license LicenseKey) { + licenseCache.Set(fmt.Sprintf("%s-%s", licenseKey, instanceName), license, 24*time.Hour) +} + +func GetLicenseCache(licenseKey, instanceName string) *LicenseKey { + cache, ok := licenseCache.Get(fmt.Sprintf("%s-%s", licenseKey, instanceName)) + if !ok { + return nil + } + return cache.(*LicenseKey) +} diff --git a/plugin/license/license.go b/plugin/license/license.go index 4c55aa0..a56fef1 100644 --- a/plugin/license/license.go +++ b/plugin/license/license.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "time" "github.com/pkg/errors" ) @@ -21,7 +22,7 @@ const ( subscriptionProProductID = 98995 ) -type licenseKey struct { +type LicenseKey struct { ID int32 `json:"id"` Status string `json:"status"` Key string `json:"key"` @@ -29,7 +30,7 @@ type licenseKey struct { ExpiresAt *string `json:"updated_at"` } -type licenseKeyMeta struct { +type LicenseKeyMeta struct { StoreID int32 `json:"store_id"` OrderID int32 `json:"order_id"` OrderItemID int32 `json:"order_item_id"` @@ -45,15 +46,15 @@ type licenseKeyMeta struct { type ValidateLicenseKeyResponse struct { Valid bool `json:"valid"` Error *string `json:"error"` - LicenseKey *licenseKey `json:"license_key"` - Meta *licenseKeyMeta `json:"meta"` + LicenseKey *LicenseKey `json:"license_key"` + Meta *LicenseKeyMeta `json:"meta"` } type ActiveLicenseKeyResponse struct { Activated bool `json:"activated"` Error *string `json:"error"` - LicenseKey *licenseKey `json:"license_key"` - Meta *licenseKeyMeta `json:"meta"` + LicenseKey *LicenseKey `json:"license_key"` + Meta *LicenseKeyMeta `json:"meta"` } func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicenseKeyResponse, error) { @@ -97,6 +98,7 @@ func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicens return nil, errors.New("invalid store or product id") } } + licenseCache.Set("key", "value", 24*time.Hour) return &response, nil }