add public methods for gettings allowed domains

This commit is contained in:
Akolzin Dmitry 2021-12-21 14:29:44 +03:00
parent 7e21e73711
commit fafd016fff
3 changed files with 89 additions and 53 deletions

60
core/domains.go Normal file
View File

@ -0,0 +1,60 @@
package core
import (
"encoding/json"
"io/ioutil"
"net/http"
)
const crmDomainsURL string = "https://infra-data.retailcrm.tech/crm-domains.json"
const boxDomainsURL string = "https://infra-data.retailcrm.tech/box-domains.json"
type Domain struct {
Domain string `json:"domain"`
}
type CrmDomains struct {
CreateDate string `json:"createDate"`
Domains []Domain `json:"domains"`
}
func GetSaasDomains() []Domain {
return getDomainsByStore(crmDomainsURL)
}
func GetBoxDomains() []Domain {
return getDomainsByStore(boxDomainsURL)
}
func getDomainsByStore(store string) []Domain {
req, reqErr := http.NewRequest(http.MethodGet, store, nil)
if reqErr != nil {
return nil
}
req.Header.Add("Accept", "application/json")
resp, respErr := http.DefaultClient.Do(req)
if respErr != nil {
return nil
}
respBody, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return nil
}
var crmDomains CrmDomains
err := json.Unmarshal(respBody, &crmDomains)
if err != nil {
return nil
}
_ = resp.Body.Close()
return crmDomains.Domains
}

26
core/domains_test.go Normal file
View File

@ -0,0 +1,26 @@
package core
import (
"github.com/stretchr/testify/assert"
"testing"
)
func Test_GetSaasDomains(t *testing.T) {
domains := GetSaasDomains()
if domains == nil {
t.Fail()
}
assert.NotEmpty(t, domains)
}
func Test_GetBoxDomains(t *testing.T) {
domains := GetBoxDomains()
if domains == nil {
t.Fail()
}
assert.NotEmpty(t, domains)
}

View File

@ -1,9 +1,6 @@
package core
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
@ -11,9 +8,6 @@ import (
"github.com/go-playground/validator/v10"
)
const crmDomainsURL string = "https://infra-data.retailcrm.tech/crm-domains.json"
const boxDomainsURL string = "https://infra-data.retailcrm.tech/box-domains.json"
// init here will register `validateCrmURL` function for gin validator.
func init() {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
@ -39,20 +33,18 @@ func isDomainValid(crmURL string) bool {
mainDomain := getMainDomain(parseURL.Hostname())
if checkDomains(crmDomainsURL, mainDomain) {
if checkDomains(GetSaasDomains(), mainDomain) {
return true
}
if checkDomains(boxDomainsURL, parseURL.Hostname()) {
if checkDomains(GetBoxDomains(), parseURL.Hostname()) {
return true
}
return false
}
func checkDomains(domainsStoreURL string, domain string) bool {
crmDomains := getDomainsByStore(domainsStoreURL)
func checkDomains(crmDomains []Domain, domain string) bool {
if nil == crmDomains {
return false
}
@ -86,45 +78,3 @@ func checkURLString(parseURL *url.URL) bool {
return true
}
func getDomainsByStore(store string) []Domain {
req, reqErr := http.NewRequest(http.MethodGet, store, nil)
if reqErr != nil {
return nil
}
req.Header.Add("Accept", "application/json")
resp, respErr := http.DefaultClient.Do(req)
if respErr != nil {
return nil
}
respBody, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return nil
}
var crmDomains CrmDomains
err := json.Unmarshal(respBody, &crmDomains)
if err != nil {
return nil
}
_ = resp.Body.Close()
return crmDomains.Domains
}
type Domain struct {
Domain string `json:"domain"`
}
type CrmDomains struct {
CreateDate string `json:"createDate"`
Domains []Domain `json:"domains"`
}