Skip to content

Commit

Permalink
refactor(productcatalogservice): use go:embed directive for products.…
Browse files Browse the repository at this point in the history
…json
  • Loading branch information
tranngoclam authored and Lam Tran committed Feb 2, 2024
1 parent 26b1710 commit 48a3089
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/productcatalogservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ FROM alpine AS release

WORKDIR /usr/src/app/

COPY ./src/productcatalogservice/products.json ./
COPY --from=builder /go/bin/productcatalogservice/ ./

EXPOSE ${PRODUCT_SERVICE_PORT}
Expand Down
12 changes: 5 additions & 7 deletions src/productcatalogservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package main

import (
"context"
_ "embed"
"fmt"
"go.opentelemetry.io/otel/sdk/instrumentation"
"io/ioutil"
"net"
"os"
"strings"
Expand Down Expand Up @@ -50,6 +50,9 @@ var (
initResourcesOnce sync.Once
)

//go:embed products.json
var products []byte

func init() {
log = logrus.New()
catalog = readCatalogFile()
Expand Down Expand Up @@ -157,13 +160,8 @@ type productCatalog struct {
}

func readCatalogFile() []*pb.Product {
catalogJSON, err := ioutil.ReadFile("products.json")
if err != nil {
log.Fatalf("Reading Catalog File: %v", err)
}

var res pb.ListProductsResponse
if err := protojson.Unmarshal(catalogJSON, &res); err != nil {
if err := protojson.Unmarshal(products, &res); err != nil {
log.Fatalf("Parsing Catalog JSON: %v", err)
}

Expand Down

0 comments on commit 48a3089

Please sign in to comment.