-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
191 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package checks | ||
|
||
import "github.com/xray-web/web-check-api/checks/store/legacyrank" | ||
|
||
type DomainRank struct { | ||
Domain string `json:"domain"` | ||
Rank int `json:"rank"` | ||
} | ||
|
||
type LegacyRank struct { | ||
data legacyrank.Getter | ||
} | ||
|
||
func NewLegacyRank(lrg legacyrank.Getter) *LegacyRank { | ||
return &LegacyRank{data: lrg} | ||
} | ||
|
||
func (lr *LegacyRank) LegacyRank(domain string) (*DomainRank, error) { | ||
rank, err := lr.data.GetLegacyRank(domain) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DomainRank{ | ||
Domain: domain, | ||
Rank: rank, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package checks | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xray-web/web-check-api/checks/store/legacyrank" | ||
) | ||
|
||
func TestLegacyRank(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("get rank", func(t *testing.T) { | ||
t.Parallel() | ||
lr := NewLegacyRank(legacyrank.GetterFunc(func(domain string) (int, error) { | ||
return 1, nil | ||
})) | ||
dr, err := lr.LegacyRank("example.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, dr.Rank) | ||
assert.Equal(t, "example.com", dr.Domain) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package legacyrank | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"context" | ||
"encoding/csv" | ||
"errors" | ||
"io" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ErrNotFound = errors.New("domain not found") | ||
|
||
type Getter interface { | ||
GetLegacyRank(domain string) (int, error) | ||
} | ||
|
||
type GetterFunc func(domain string) (int, error) | ||
|
||
func (f GetterFunc) GetLegacyRank(domain string) (int, error) { | ||
return f(domain) | ||
} | ||
|
||
type InMemoryStore struct{} | ||
|
||
var once sync.Once | ||
var data map[string]int //map of domain to rank | ||
|
||
func NewInMemoryStore() *InMemoryStore { | ||
return &InMemoryStore{} | ||
} | ||
|
||
func (s *InMemoryStore) GetLegacyRank(url string) (int, error) { | ||
once.Do(func() { | ||
var err error | ||
data, err = load() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}) | ||
|
||
rank, ok := data[url] | ||
if !ok { | ||
return -1, ErrNotFound | ||
} | ||
return rank, nil | ||
} | ||
|
||
func load() (map[string]int, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
defer cancel() | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://s3-us-west-1.amazonaws.com/umbrella-static/top-1m.csv.zip", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := &http.Client{ | ||
Timeout: time.Second * 10, | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
zf, err := zip.NewReader(bytes.NewReader(b), int64(len(b))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f, err := zf.Open("top-1m.csv") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
r := csv.NewReader(f) | ||
data := make(map[string]int) | ||
for { | ||
record, err := r.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
rank, err := strconv.Atoi(record[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data[record[1]] = rank | ||
} | ||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package legacyrank_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xray-web/web-check-api/checks/store/legacyrank" | ||
) | ||
|
||
func TestInMemoryStore(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("get google rank", func(t *testing.T) { | ||
t.Parallel() | ||
ims := legacyrank.NewInMemoryStore() | ||
dr, err := ims.GetLegacyRank("google.com") | ||
assert.NoError(t, err, dr) | ||
}) | ||
|
||
t.Run("get microsoft rank", func(t *testing.T) { | ||
t.Parallel() | ||
ims := legacyrank.NewInMemoryStore() | ||
dr, err := ims.GetLegacyRank("microsoft.com") | ||
assert.NoError(t, err, dr) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.