This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
2017-04-04 19:28:46 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO DRAFT for a rest request to a other microservice
|
|
|
|
const ProductURL = "https://google.com/?q=%d"
|
|
|
|
|
|
|
|
type boolMicroServiceCache struct {
|
|
|
|
LastCheck time.Time
|
|
|
|
Value bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var productExistCache map[int64]boolMicroServiceCache
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
productExistCache = make(map[int64]boolMicroServiceCache)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Product) Exists() (bool, error) {
|
|
|
|
if cache, ok := productExistCache[p.ID]; ok {
|
2017-04-05 20:23:29 +02:00
|
|
|
return cache.Value, nil
|
2017-04-04 19:28:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf(ProductURL, p.ID)
|
|
|
|
log.Log.WithField("url", url).Info("exists product?")
|
|
|
|
res, err := http.Get(url)
|
|
|
|
|
|
|
|
productExistCache[p.ID] = boolMicroServiceCache{
|
|
|
|
LastCheck: time.Now(),
|
|
|
|
Value: (res.StatusCode == http.StatusOK),
|
|
|
|
}
|
|
|
|
return productExistCache[p.ID].Value, err
|
|
|
|
}
|