Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
d010926c07 | |||
7e3f6cf674 | |||
de5626814b | |||
923fdea2a1 | |||
58f77f22a4 | |||
a0d42bbbc3 |
34
cache/cache.go
vendored
Normal file
34
cache/cache.go
vendored
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Provider interface {
|
||||||
|
Get(key string) ([]byte, error)
|
||||||
|
Set(key string, b []byte, d time.Duration) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func ForURI(uri string) Provider {
|
||||||
|
u, err := url.Parse(uri)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.Scheme == "" && u.Path != "" {
|
||||||
|
u.Scheme = u.Path
|
||||||
|
}
|
||||||
|
|
||||||
|
switch u.Scheme {
|
||||||
|
case "memcached":
|
||||||
|
return NewMemcached(u)
|
||||||
|
case "redis":
|
||||||
|
return NewRedisCache(u)
|
||||||
|
case "gcache":
|
||||||
|
return NewGcache(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &NullCache{}
|
||||||
|
}
|
47
cache/gcache.go
vendored
Normal file
47
cache/gcache.go
vendored
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bluele/gcache"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Gcache struct {
|
||||||
|
cache gcache.Cache
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGcache(u *url.URL) *Gcache {
|
||||||
|
size := 128
|
||||||
|
|
||||||
|
q := u.Query()
|
||||||
|
|
||||||
|
if sizeStr := q.Get("size"); sizeStr != "" {
|
||||||
|
var err error
|
||||||
|
size, err = strconv.Atoi(sizeStr)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
size = 128
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gc := gcache.New(size).
|
||||||
|
LRU().
|
||||||
|
Build()
|
||||||
|
|
||||||
|
return &Gcache{cache: gc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Gcache) Get(key string) ([]byte, error) {
|
||||||
|
res, err := c.cache.Get(key)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.([]byte), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Gcache) Set(key string, b []byte, d time.Duration) error {
|
||||||
|
return c.cache.SetWithExpire(key, b, d)
|
||||||
|
}
|
32
cache/memcached.go
vendored
Normal file
32
cache/memcached.go
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bradfitz/gomemcache/memcache"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Memcached struct {
|
||||||
|
client *memcache.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMemcached(u *url.URL) *Memcached {
|
||||||
|
mc := memcache.New(strings.Split(u.Host, ",")...)
|
||||||
|
|
||||||
|
return &Memcached{client: mc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Memcached) Get(key string) ([]byte, error) {
|
||||||
|
item, err := m.client.Get(key)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return item.Value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Memcached) Set(key string, b []byte, d time.Duration) error {
|
||||||
|
return m.client.Set(&memcache.Item{Key: key, Value: b, Expiration: int32(d.Seconds())})
|
||||||
|
}
|
14
cache/null.go
vendored
Normal file
14
cache/null.go
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type NullCache struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NullCache) Get(key string) ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NullCache) Set(key string, b []byte, d time.Duration) error {
|
||||||
|
return nil
|
||||||
|
}
|
29
cache/redis.go
vendored
Normal file
29
cache/redis.go
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-redis/redis"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RedisCache struct {
|
||||||
|
client *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRedisCache(uri *url.URL) *RedisCache {
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: uri.Host,
|
||||||
|
Password: "", // no password set
|
||||||
|
DB: 0, // use default DB
|
||||||
|
})
|
||||||
|
|
||||||
|
return &RedisCache{client: client}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RedisCache) Get(key string) ([]byte, error) {
|
||||||
|
return c.client.Get(key).Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RedisCache) Set(key string, b []byte, d time.Duration) error {
|
||||||
|
return c.client.Set(key, b, d).Err()
|
||||||
|
}
|
166
endpoints.go
Normal file
166
endpoints.go
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func stats(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
data, err := statsResponse(w, ps, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
w.Write(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func profile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
cacheKey := generateCacheKey(ps) + "-profile"
|
||||||
|
|
||||||
|
res, err := cacheProvider.Get(cacheKey)
|
||||||
|
|
||||||
|
if res != nil && err == nil {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache result for profile specifically
|
||||||
|
data, err := statsResponse(w, ps, profilePatch)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheTime > 0 {
|
||||||
|
cacheProvider.Set(cacheKey, data, cacheTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
w.Write(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func heroes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
names := strings.Split(ps.ByName("heroes"), ",")
|
||||||
|
|
||||||
|
if len(names) == 0 {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
writeError(w, errors.New("name list must contain at least one hero"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(names)
|
||||||
|
|
||||||
|
cacheKey := generateCacheKey(ps) + "-heroes-" + hex.EncodeToString(md5.New().Sum([]byte(strings.Join(names, ","))))
|
||||||
|
|
||||||
|
res, err := cacheProvider.Get(cacheKey)
|
||||||
|
|
||||||
|
if res != nil && err == nil {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nameMap := make(map[string]bool)
|
||||||
|
|
||||||
|
for _, name := range names {
|
||||||
|
nameMap[name] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
ops := make([]patchOperation, 0)
|
||||||
|
|
||||||
|
for _, heroName := range heroNames {
|
||||||
|
if _, exists := nameMap[heroName]; !exists {
|
||||||
|
ops = append(ops, patchOperation{
|
||||||
|
Op: OpRemove,
|
||||||
|
Path: "/quickPlayStats/topHeroes/" + heroName,
|
||||||
|
}, patchOperation{
|
||||||
|
Op: OpRemove,
|
||||||
|
Path: "/quickPlayStats/careerStats/" + heroName,
|
||||||
|
}, patchOperation{
|
||||||
|
Op: OpRemove,
|
||||||
|
Path: "/competitiveStats/topHeroes/" + heroName,
|
||||||
|
}, patchOperation{
|
||||||
|
Op: OpRemove,
|
||||||
|
Path: "/competitiveStats/careerStats/" + heroName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
patch, err := patchFromOperations(ops)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a patch to remove all but specified heroes
|
||||||
|
data, err := statsResponse(w, ps, patch)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheTime > 0 {
|
||||||
|
cacheProvider.Set(cacheKey, data, cacheTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
w.Write(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
type versionObject struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(&versionObject{Version: Version}); err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type statusObject struct {
|
||||||
|
ResponseCode int `json:"responseCode"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func statusHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
status := &statusObject{}
|
||||||
|
|
||||||
|
res, err := http.DefaultClient.Head("https://playoverwatch.com")
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
status.ResponseCode = res.StatusCode
|
||||||
|
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
w.WriteHeader(res.StatusCode)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
status.Error = err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method != http.MethodHead {
|
||||||
|
if err := json.NewEncoder(w).Encode(status); err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
go.mod
8
go.mod
@ -4,12 +4,18 @@ go 1.12
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/PuerkitoBio/goquery v1.5.1-0.20190109230704-3dcf72e6c17f
|
github.com/PuerkitoBio/goquery v1.5.1-0.20190109230704-3dcf72e6c17f
|
||||||
|
github.com/bluele/gcache v0.0.0-20190518031135-bc40bd653833
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668
|
||||||
github.com/go-redis/redis v6.14.2+incompatible
|
github.com/go-redis/redis v6.14.2+incompatible
|
||||||
github.com/julienschmidt/httprouter v1.2.0
|
github.com/julienschmidt/httprouter v1.2.0
|
||||||
|
github.com/labstack/echo/v4 v4.1.10 // indirect
|
||||||
github.com/onsi/ginkgo v1.8.0 // indirect
|
github.com/onsi/ginkgo v1.8.0 // indirect
|
||||||
github.com/onsi/gomega v1.5.0 // indirect
|
github.com/onsi/gomega v1.5.0 // indirect
|
||||||
github.com/rs/cors v1.6.0
|
github.com/rs/cors v1.6.0
|
||||||
github.com/starboy/httpclient v0.0.0-20180910064439-d6d6e42b6080 // indirect
|
github.com/starboy/httpclient v0.0.0-20180910064439-d6d6e42b6080 // indirect
|
||||||
github.com/tystuyfzand/ovrstat v0.0.0-20181127024708-dc4fc9f39eb7
|
github.com/tystuyfzand/ovrstat v0.0.0-20181127024708-dc4fc9f39eb7
|
||||||
s32x.com/ovrstat v0.0.0-20190506040748-69ba35e9ff58
|
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect
|
||||||
|
golang.org/x/tools v0.0.0-20190816200558-6889da9d5479 // indirect
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||||
|
s32x.com/ovrstat v0.0.0-20190816015343-36b2cbd419d3
|
||||||
)
|
)
|
||||||
|
44
go.sum
44
go.sum
@ -2,7 +2,12 @@ github.com/PuerkitoBio/goquery v1.5.1-0.20190109230704-3dcf72e6c17f h1:cWOyRTtBc
|
|||||||
github.com/PuerkitoBio/goquery v1.5.1-0.20190109230704-3dcf72e6c17f/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
|
github.com/PuerkitoBio/goquery v1.5.1-0.20190109230704-3dcf72e6c17f/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
|
||||||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||||
|
github.com/bluele/gcache v0.0.0-20190518031135-bc40bd653833 h1:yCfXxYaelOyqnia8F/Yng47qhmfC9nKTRIbYRrRueq4=
|
||||||
|
github.com/bluele/gcache v0.0.0-20190518031135-bc40bd653833/go.mod h1:8c4/i2VlovMO2gBnHGQPN5EJw+H0lx1u/5p+cgsXtCk=
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668 h1:U/lr3Dgy4WK+hNk4tyD+nuGjpVLPEHuJSFXMw11/HPA=
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||||
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
@ -14,48 +19,87 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
|||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k=
|
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k=
|
||||||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
|
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
|
||||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||||
github.com/labstack/echo/v4 v4.0.1-0.20190313005416-e3717be4beda/go.mod h1:tZv7nai5buKSg5h/8E6zz4LsD/Dqh9/91Mvs7Z5Zyno=
|
github.com/labstack/echo/v4 v4.0.1-0.20190313005416-e3717be4beda/go.mod h1:tZv7nai5buKSg5h/8E6zz4LsD/Dqh9/91Mvs7Z5Zyno=
|
||||||
|
github.com/labstack/echo/v4 v4.1.7-0.20190725203903-8cabd1e123d6/go.mod h1:kU/7PwzgNxZH4das4XNsSpBSOD09XIF5YEPzjpkGnGE=
|
||||||
|
github.com/labstack/echo/v4 v4.1.10/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
|
||||||
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
|
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
|
||||||
|
github.com/labstack/gommon v0.2.9/go.mod h1:E8ZTmW9vw5az5/ZyHWCp0Lw4OH2ecsaBP1C/NKavGG4=
|
||||||
|
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
|
||||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||||
|
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||||
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||||
|
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||||
|
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
|
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
|
||||||
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
|
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
|
||||||
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
|
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
|
||||||
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
|
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
|
||||||
github.com/starboy/httpclient v0.0.0-20180910064439-d6d6e42b6080 h1:2MLHNkv6Jl3E58fFOlOeshNo6eqanZV+yGPbJukRQIQ=
|
github.com/starboy/httpclient v0.0.0-20180910064439-d6d6e42b6080 h1:2MLHNkv6Jl3E58fFOlOeshNo6eqanZV+yGPbJukRQIQ=
|
||||||
github.com/starboy/httpclient v0.0.0-20180910064439-d6d6e42b6080/go.mod h1:ZaOOtAQcXELCP9jugRrAv1qg3ctLlGnz++lH0KpIZ24=
|
github.com/starboy/httpclient v0.0.0-20180910064439-d6d6e42b6080/go.mod h1:ZaOOtAQcXELCP9jugRrAv1qg3ctLlGnz++lH0KpIZ24=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
github.com/tystuyfzand/ovrstat v0.0.0-20181127024708-dc4fc9f39eb7 h1:ZHUhYZJiBOfJ9NPDT/YEKHOCc0tTtQiLGpTcW0M2mBM=
|
github.com/tystuyfzand/ovrstat v0.0.0-20181127024708-dc4fc9f39eb7 h1:ZHUhYZJiBOfJ9NPDT/YEKHOCc0tTtQiLGpTcW0M2mBM=
|
||||||
github.com/tystuyfzand/ovrstat v0.0.0-20181127024708-dc4fc9f39eb7/go.mod h1:GgVOFCqOnwT77FsVfvySTbw6u3rMW9rqUxvY7nGgpjg=
|
github.com/tystuyfzand/ovrstat v0.0.0-20181127024708-dc4fc9f39eb7/go.mod h1:GgVOFCqOnwT77FsVfvySTbw6u3rMW9rqUxvY7nGgpjg=
|
||||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw=
|
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw=
|
||||||
|
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||||
golang.org/x/crypto v0.0.0-20190130090550-b01c7a725664/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
golang.org/x/crypto v0.0.0-20190130090550-b01c7a725664/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
|
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
|
||||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190607181551-461777fb6f67/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA=
|
||||||
|
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
|
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
|
||||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc h1:WiYx1rIFmx8c0mXAFtv5D/mHyKe1+jmuP7PViuwqwuQ=
|
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc h1:WiYx1rIFmx8c0mXAFtv5D/mHyKe1+jmuP7PViuwqwuQ=
|
||||||
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190609082536-301114b31cce/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190608022120-eacb66d2a7c3/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||||
|
golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
||||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
s32x.com/ovrstat v0.0.0-20190506040748-69ba35e9ff58 h1:1ioAQKrIwwtxMm4uZySpqj9imNuyCDbL7dk9ccxntmQ=
|
s32x.com/ovrstat v0.0.0-20190506040748-69ba35e9ff58 h1:1ioAQKrIwwtxMm4uZySpqj9imNuyCDbL7dk9ccxntmQ=
|
||||||
s32x.com/ovrstat v0.0.0-20190506040748-69ba35e9ff58/go.mod h1:ukrDi0b+Kdtd+dI4M0HILhkMGtMZ4v3l2C82meAMzyw=
|
s32x.com/ovrstat v0.0.0-20190506040748-69ba35e9ff58/go.mod h1:ukrDi0b+Kdtd+dI4M0HILhkMGtMZ4v3l2C82meAMzyw=
|
||||||
|
s32x.com/ovrstat v0.0.0-20190816015343-36b2cbd419d3 h1:04gLpRbm72K2OTsTnflheHmuuV7A0VOT33HNNo0UAnM=
|
||||||
|
s32x.com/ovrstat v0.0.0-20190816015343-36b2cbd419d3/go.mod h1:UzsLSEoY8B4FByz4e+5GGKebJio+H+axo3RxLr7d7Mw=
|
||||||
|
261
main.go
261
main.go
@ -4,30 +4,25 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
|
"git.meow.tf/ow-api/ow-api/cache"
|
||||||
"git.meow.tf/ow-api/ow-api/json-patch"
|
"git.meow.tf/ow-api/ow-api/json-patch"
|
||||||
"github.com/go-redis/redis"
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"s32x.com/ovrstat/ovrstat"
|
"s32x.com/ovrstat/ovrstat"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "2.0.12"
|
Version = "2.2.1"
|
||||||
|
|
||||||
OpAdd = "add"
|
OpAdd = "add"
|
||||||
OpRemove = "remove"
|
OpRemove = "remove"
|
||||||
)
|
)
|
||||||
|
|
||||||
type patchOperation struct {
|
|
||||||
Op string `json:"op"`
|
|
||||||
Path string `json:"path"`
|
|
||||||
Value interface{} `json:"value,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gamesStats struct {
|
type gamesStats struct {
|
||||||
Played int64 `json:"played"`
|
Played int64 `json:"played"`
|
||||||
Won int64 `json:"won"`
|
Won int64 `json:"won"`
|
||||||
@ -42,9 +37,13 @@ type awardsStats struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
flagBind = flag.String("bind-address", ":8080", "Address to bind to for http requests")
|
flagBind = flag.String("bind-address", ":8080", "Address to bind to for http requests")
|
||||||
|
flagCache = flag.String("cache", "redis://localhost:6379", "Cache uri or 'none' to disable")
|
||||||
|
flagCacheTime = flag.Int("cacheTime", 300, "Cache time in seconds")
|
||||||
|
|
||||||
client *redis.Client
|
cacheProvider cache.Provider
|
||||||
|
|
||||||
|
cacheTime time.Duration
|
||||||
|
|
||||||
profilePatch *jsonpatch.Patch
|
profilePatch *jsonpatch.Patch
|
||||||
|
|
||||||
@ -54,11 +53,9 @@ var (
|
|||||||
func main() {
|
func main() {
|
||||||
loadHeroNames()
|
loadHeroNames()
|
||||||
|
|
||||||
client = redis.NewClient(&redis.Options{
|
cacheProvider = cache.ForURI(*flagCache)
|
||||||
Addr: "localhost:6379",
|
|
||||||
Password: "", // no password set
|
cacheTime = time.Duration(*flagCacheTime) * time.Second
|
||||||
DB: 0, // use default DB
|
|
||||||
})
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@ -93,6 +90,7 @@ func main() {
|
|||||||
// Version
|
// Version
|
||||||
router.GET("/v1/version", versionHandler)
|
router.GET("/v1/version", versionHandler)
|
||||||
|
|
||||||
|
router.HEAD("/v1/status", statusHandler)
|
||||||
router.GET("/v1/status", statusHandler)
|
router.GET("/v1/status", statusHandler)
|
||||||
|
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
@ -140,6 +138,10 @@ func injectPlatform(platform string, handler httprouter.Handle) httprouter.Handl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
tagRegexp = regexp.MustCompile("-(\\d+)$")
|
||||||
|
)
|
||||||
|
|
||||||
func statsResponse(w http.ResponseWriter, ps httprouter.Params, patch *jsonpatch.Patch) ([]byte, error) {
|
func statsResponse(w http.ResponseWriter, ps httprouter.Params, patch *jsonpatch.Patch) ([]byte, error) {
|
||||||
var stats *ovrstat.PlayerStats
|
var stats *ovrstat.PlayerStats
|
||||||
var err error
|
var err error
|
||||||
@ -151,6 +153,10 @@ func statsResponse(w http.ResponseWriter, ps httprouter.Params, patch *jsonpatch
|
|||||||
cacheKey := generateCacheKey(ps)
|
cacheKey := generateCacheKey(ps)
|
||||||
|
|
||||||
if region := ps.ByName("region"); region != "" {
|
if region := ps.ByName("region"); region != "" {
|
||||||
|
if !tagRegexp.MatchString(tag) {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return nil, errors.New("bad tag")
|
||||||
|
}
|
||||||
stats, err = ovrstat.PCStats(tag)
|
stats, err = ovrstat.PCStats(tag)
|
||||||
} else if platform := ps.ByName("platform"); platform != "" {
|
} else if platform := ps.ByName("platform"); platform != "" {
|
||||||
stats, err = ovrstat.ConsoleStats(platform, tag)
|
stats, err = ovrstat.ConsoleStats(platform, tag)
|
||||||
@ -164,7 +170,7 @@ func statsResponse(w http.ResponseWriter, ps httprouter.Params, patch *jsonpatch
|
|||||||
|
|
||||||
// Caching of full response for modification
|
// Caching of full response for modification
|
||||||
|
|
||||||
res, err := client.Get(cacheKey).Bytes()
|
res, err := cacheProvider.Get(cacheKey)
|
||||||
|
|
||||||
if res != nil && err == nil {
|
if res != nil && err == nil {
|
||||||
if patch != nil {
|
if patch != nil {
|
||||||
@ -226,6 +232,35 @@ func statsResponse(w http.ResponseWriter, ps httprouter.Params, patch *jsonpatch
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rating := 0
|
||||||
|
var ratingIcon string
|
||||||
|
|
||||||
|
if len(stats.Ratings) > 0 {
|
||||||
|
totalRating := 0
|
||||||
|
iconUrl := ""
|
||||||
|
|
||||||
|
for _, rating := range stats.Ratings {
|
||||||
|
totalRating += rating.Level
|
||||||
|
iconUrl = rating.RankIcon
|
||||||
|
}
|
||||||
|
|
||||||
|
rating = int(totalRating / len(stats.Ratings))
|
||||||
|
|
||||||
|
urlBase := iconUrl[0 : strings.Index(iconUrl, "rank-icons/")+11]
|
||||||
|
|
||||||
|
ratingIcon = urlBase + iconFor(rating)
|
||||||
|
}
|
||||||
|
|
||||||
|
extra = append(extra, patchOperation{
|
||||||
|
Op: OpAdd,
|
||||||
|
Path: "/rating",
|
||||||
|
Value: rating,
|
||||||
|
}, patchOperation{
|
||||||
|
Op: OpAdd,
|
||||||
|
Path: "/ratingIcon",
|
||||||
|
Value: ratingIcon,
|
||||||
|
})
|
||||||
|
|
||||||
b, err := json.Marshal(stats)
|
b, err := json.Marshal(stats)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -247,7 +282,9 @@ func statsResponse(w http.ResponseWriter, ps httprouter.Params, patch *jsonpatch
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cache response
|
// Cache response
|
||||||
client.Set(cacheKey, b, 10*time.Minute)
|
if cacheTime > 0 {
|
||||||
|
cacheProvider.Set(cacheKey, b, cacheTime)
|
||||||
|
}
|
||||||
|
|
||||||
if patch != nil {
|
if patch != nil {
|
||||||
// Apply filter patch
|
// Apply filter patch
|
||||||
@ -257,182 +294,22 @@ func statsResponse(w http.ResponseWriter, ps httprouter.Params, patch *jsonpatch
|
|||||||
return b, err
|
return b, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func stats(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
func iconFor(rating int) string {
|
||||||
data, err := statsResponse(w, ps, nil)
|
if rating >= 4000 {
|
||||||
|
return "rank-GrandmasterTier.png"
|
||||||
if err != nil {
|
} else if rating >= 3500 {
|
||||||
writeError(w, err)
|
return "rank-MasterTier.png"
|
||||||
return
|
} else if rating >= 3000 {
|
||||||
|
return "rank-DiamondTier.png"
|
||||||
|
} else if rating >= 2500 {
|
||||||
|
return "rank-PlatinumTier"
|
||||||
|
} else if rating >= 2000 {
|
||||||
|
return "rank-GoldTier.png"
|
||||||
|
} else if rating >= 1500 {
|
||||||
|
return "rank-SilverTier.png"
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
return "rank-BronzeTier.png"
|
||||||
|
|
||||||
w.Write(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func profile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
||||||
cacheKey := generateCacheKey(ps) + "-profile"
|
|
||||||
|
|
||||||
// Check cache for -profile to prevent jsonpatch calls
|
|
||||||
res, err := client.Get(cacheKey).Bytes()
|
|
||||||
|
|
||||||
if res != nil && err == nil {
|
|
||||||
w.Write(res)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cache result for profile specifically
|
|
||||||
data, err := statsResponse(w, ps, profilePatch)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
client.Set(cacheKey, data, 10*time.Minute)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
w.Write(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func heroes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
||||||
names := strings.Split(ps.ByName("heroes"), ",")
|
|
||||||
|
|
||||||
if len(names) == 0 {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
writeError(w, errors.New("name list must contain at least one hero"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
nameMap := make(map[string]bool)
|
|
||||||
|
|
||||||
for _, name := range names {
|
|
||||||
nameMap[name] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
ops := make([]patchOperation, 0)
|
|
||||||
|
|
||||||
for _, heroName := range heroNames {
|
|
||||||
if _, exists := nameMap[heroName]; !exists {
|
|
||||||
ops = append(ops, patchOperation{
|
|
||||||
Op: OpRemove,
|
|
||||||
Path: "/quickPlayStats/topHeroes/" + heroName,
|
|
||||||
}, patchOperation{
|
|
||||||
Op: OpRemove,
|
|
||||||
Path: "/quickPlayStats/careerStats/" + heroName,
|
|
||||||
}, patchOperation{
|
|
||||||
Op: OpRemove,
|
|
||||||
Path: "/competitiveStats/topHeroes/" + heroName,
|
|
||||||
}, patchOperation{
|
|
||||||
Op: OpRemove,
|
|
||||||
Path: "/competitiveStats/careerStats/" + heroName,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
patch, err := patchFromOperations(ops)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a patch to remove all but specified heroes
|
|
||||||
data, err := statsResponse(w, ps, patch)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
w.Write(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func valueOrDefault(m map[string]interface{}, key string, d int64) int64 {
|
|
||||||
if v, ok := m[key]; ok {
|
|
||||||
switch v.(type) {
|
|
||||||
case int64:
|
|
||||||
return v.(int64)
|
|
||||||
case int:
|
|
||||||
return int64(v.(int))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
||||||
func patchFromOperations(ops []patchOperation) (*jsonpatch.Patch, error) {
|
|
||||||
patchBytes, err := json.Marshal(ops)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
patch, err := jsonpatch.DecodePatch(patchBytes)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &patch, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type versionObject struct {
|
|
||||||
Version string `json:"version"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func versionHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(&versionObject{Version: Version}); err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type statusObject struct {
|
|
||||||
ResponseCode int `json:"responseCode"`
|
|
||||||
Error string `json:"error,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func statusHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
status := &statusObject{}
|
|
||||||
|
|
||||||
res, err := http.DefaultClient.Head("https://playoverwatch.com")
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
status.ResponseCode = res.StatusCode
|
|
||||||
|
|
||||||
if res.StatusCode != http.StatusOK {
|
|
||||||
w.WriteHeader(res.StatusCode)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
status.Error = err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(status); err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type errorObject struct {
|
|
||||||
Error string `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeError(w http.ResponseWriter, err error) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if err == ovrstat.ErrPlayerNotFound {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(&errorObject{Error: err.Error()}); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateCacheKey(ps httprouter.Params) string {
|
func generateCacheKey(ps httprouter.Params) string {
|
||||||
@ -441,7 +318,7 @@ func generateCacheKey(ps httprouter.Params) string {
|
|||||||
tag := ps.ByName("tag")
|
tag := ps.ByName("tag")
|
||||||
|
|
||||||
if region := ps.ByName("region"); region != "" {
|
if region := ps.ByName("region"); region != "" {
|
||||||
cacheKey = "pc-" + region + "-" + tag
|
cacheKey = "pc-" + tag
|
||||||
} else if platform := ps.ByName("platform"); platform != "" {
|
} else if platform := ps.ByName("platform"); platform != "" {
|
||||||
cacheKey = platform + "-" + tag
|
cacheKey = platform + "-" + tag
|
||||||
}
|
}
|
||||||
|
58
util.go
Normal file
58
util.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
jsonpatch "git.meow.tf/ow-api/ow-api/json-patch"
|
||||||
|
"net/http"
|
||||||
|
"s32x.com/ovrstat/ovrstat"
|
||||||
|
)
|
||||||
|
|
||||||
|
func valueOrDefault(m map[string]interface{}, key string, d int64) int64 {
|
||||||
|
if v, ok := m[key]; ok {
|
||||||
|
switch v.(type) {
|
||||||
|
case int64:
|
||||||
|
return v.(int64)
|
||||||
|
case int:
|
||||||
|
return int64(v.(int))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
type patchOperation struct {
|
||||||
|
Op string `json:"op"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Value interface{} `json:"value,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func patchFromOperations(ops []patchOperation) (*jsonpatch.Patch, error) {
|
||||||
|
patchBytes, err := json.Marshal(ops)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
patch, err := jsonpatch.DecodePatch(patchBytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &patch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type errorObject struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, err error) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if err == ovrstat.ErrPlayerNotFound {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(&errorObject{Error: err.Error()}); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user