factor read catalog json code
This commit is contained in:
parent
ce80e8b119
commit
3540c58964
1 changed files with 32 additions and 28 deletions
|
@ -24,6 +24,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -43,9 +44,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
catalogJSON []byte
|
cat pb.ListProductsResponse
|
||||||
cat pb.ListProductsResponse
|
catalogMutex *sync.Mutex
|
||||||
log *logrus.Logger
|
log *logrus.Logger
|
||||||
|
|
||||||
port = flag.Int("port", 3550, "port to listen at")
|
port = flag.Int("port", 3550, "port to listen at")
|
||||||
|
|
||||||
|
@ -53,14 +54,6 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
c, err := ioutil.ReadFile("products.json")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("failed to open product catalog json file: %v", err)
|
|
||||||
}
|
|
||||||
catalogJSON = c
|
|
||||||
if err := jsonpb.Unmarshal(bytes.NewReader(catalogJSON), &cat); err != nil {
|
|
||||||
log.Warnf("failed to parse the catalog JSON: %v", err)
|
|
||||||
}
|
|
||||||
log = logrus.New()
|
log = logrus.New()
|
||||||
log.Formatter = &logrus.JSONFormatter{
|
log.Formatter = &logrus.JSONFormatter{
|
||||||
FieldMap: logrus.FieldMap{
|
FieldMap: logrus.FieldMap{
|
||||||
|
@ -71,7 +64,11 @@ func init() {
|
||||||
TimestampFormat: time.RFC3339Nano,
|
TimestampFormat: time.RFC3339Nano,
|
||||||
}
|
}
|
||||||
log.Out = os.Stdout
|
log.Out = os.Stdout
|
||||||
log.Info("successfully parsed product catalog json")
|
catalogMutex = &sync.Mutex{}
|
||||||
|
err := readCatalogFile(&cat)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("could not parse product catalog")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -170,23 +167,30 @@ func initProfiling(service, version string) {
|
||||||
|
|
||||||
type productCatalog struct{}
|
type productCatalog struct{}
|
||||||
|
|
||||||
func parseCatalog() []*pb.Product {
|
func readCatalogFile(catalog *pb.ListProductsResponse) error {
|
||||||
if reloadCatalog {
|
catalogMutex.Lock()
|
||||||
var catalog pb.ListProductsResponse
|
defer catalogMutex.Unlock()
|
||||||
c, err := ioutil.ReadFile("products.json")
|
catalogJSON, err := ioutil.ReadFile("products.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to open product catalog json file: %v", err)
|
log.Fatalf("failed to open product catalog json file: %v", err)
|
||||||
return nil
|
return err
|
||||||
}
|
|
||||||
if err := jsonpb.Unmarshal(bytes.NewReader(c), &catalog); err != nil {
|
|
||||||
log.Warnf("failed to parse the catalog JSON: %v", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
log.Infof("Catalog reloaded")
|
|
||||||
return catalog.Products
|
|
||||||
} else {
|
|
||||||
return cat.Products
|
|
||||||
}
|
}
|
||||||
|
if err := jsonpb.Unmarshal(bytes.NewReader(catalogJSON), catalog); err != nil {
|
||||||
|
log.Warnf("failed to parse the catalog JSON: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Info("successfully parsed product catalog json")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseCatalog() []*pb.Product {
|
||||||
|
if reloadCatalog || len(cat.Products) == 0 {
|
||||||
|
err := readCatalogFile(&cat)
|
||||||
|
if err != nil {
|
||||||
|
return []*pb.Product{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cat.Products
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *productCatalog) Check(ctx context.Context, req *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
|
func (p *productCatalog) Check(ctx context.Context, req *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue