Skip to content

Commit

Permalink
added new backend for product catalog service
Browse files Browse the repository at this point in the history
  • Loading branch information
Asam80 committed Dec 24, 2024
1 parent 681700e commit b9336c8
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 150 deletions.
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,35 @@ services:
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- OTEL_RESOURCE_ATTRIBUTES
- OTEL_SERVICE_NAME=productcatalogservice
- DB_HOST=postgres-db
- DB_PORT=5432
- DB_NAME=product_catalog
- DB_USER=postgres
- DB_PASSWORD=postgres
volumes:
- ./src/productcatalogservice/products:/app/products
depends_on:
otelcol:
condition: service_started
flagd:
condition: service_started
postgres:
condition: service_started
logging: *logging



postgres:
image: postgres:latest
container_name: postgres-db
environment:
POSTGRES_DB: product_catalog
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"


# Quote service
quoteservice:
image: ${IMAGE_NAME}:${DEMO_VERSION}-quoteservice
Expand Down
115 changes: 89 additions & 26 deletions src/productcatalogservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package main

import (
"context"
"database/sql"
"fmt"
"io/fs"
"net"
"os"
"os/signal"
Expand All @@ -18,6 +18,9 @@ import (
"syscall"
"time"

"github.com/lib/pq"
_ "github.com/lib/pq"

"github.com/sirupsen/logrus"

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
Expand All @@ -43,24 +46,75 @@ import (
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
)

var (
log *logrus.Logger
catalog []*pb.Product
resource *sdkresource.Resource
initResourcesOnce sync.Once
db *sql.DB
)

func init() {
log = logrus.New()
var err error

db, err = initDB()
if err != nil {
log.Fatalf("Error initializing database: %v", err)
}

if db == nil {
log.Fatal("Failed to initialize database: db is nil")
}

catalog, err = readProductFiles()
if err != nil {
log.Fatalf("Reading Product Files: %v", err)
os.Exit(1)
}

}

func initDB() (*sql.DB, error) {

connStr := "user=postgres password=postgres dbname=product_catalog host=postgres-db port=5432 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, fmt.Errorf("could not connect to database: %v", err)
}

if err := db.Ping(); err != nil {
return nil, fmt.Errorf("could not ping database: %v", err)
}

log.Info("Successfully connected to the PostgreSQL database")

err = initDBSchema(db)
if err != nil {
return nil, fmt.Errorf("could not initialize database schema: %v", err)
}

return db, nil
}

func initDBSchema(db *sql.DB) error {

sqlFilePath := "/app/products/initdb.sql"

sqlBytes, err := os.ReadFile(sqlFilePath)
if err != nil {
return fmt.Errorf("could not read init.sql file: %v", err)
}

_, err = db.Exec(string(sqlBytes))
if err != nil {
return fmt.Errorf("could not execute initdb.sql: %v", err)
}

log.Info("Database schema initialized successfully")
return nil
}

func initResource() *sdkresource.Resource {
Expand Down Expand Up @@ -180,41 +234,50 @@ type productCatalog struct {

func readProductFiles() ([]*pb.Product, error) {

// find all .json files in the products directory
entries, err := os.ReadDir("./products")
if err != nil {
return nil, err
if db == nil {
return nil, fmt.Errorf("database connection is nil")
}

jsonFiles := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), ".json") {
info, err := entry.Info()
if err != nil {
return nil, err
}
jsonFiles = append(jsonFiles, info)
}
rows, err := db.Query("SELECT * FROM products")
if err != nil {
return nil, fmt.Errorf("failed to execute query: %v", err)
}
defer rows.Close()

// read the contents of each .json file and unmarshal into a ListProductsResponse
// then append the products to the catalog
var products []*pb.Product
for _, f := range jsonFiles {
jsonData, err := os.ReadFile("./products/" + f.Name())
if err != nil {
return nil, err
for rows.Next() {
var product pb.Product

var priceUsdCurrencyCode string
var priceUsdUnits, priceUsdNanos int
var categories []string

if err := rows.Scan(
&product.Id,
&product.Name,
&product.Description,
&product.Picture,
&priceUsdCurrencyCode,
&priceUsdUnits,
&priceUsdNanos,
pq.Array(&categories),
); err != nil {
return nil, fmt.Errorf("failed to scan row: %v", err)
}

var res pb.ListProductsResponse
if err := protojson.Unmarshal(jsonData, &res); err != nil {
return nil, err
product.PriceUsd = &pb.Money{
CurrencyCode: priceUsdCurrencyCode,
Units: int64(priceUsdUnits),
Nanos: int32(priceUsdNanos),
}
product.Categories = categories

products = append(products, res.Products...)
products = append(products, &product)
}

log.Infof("Loaded %d products", len(products))
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error occurred while iterating over rows: %v", err)
}

return products, nil
}
Expand Down
41 changes: 41 additions & 0 deletions src/productcatalogservice/products/initdb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
DO
$$
BEGIN
IF NOT EXISTS (
SELECT FROM pg_roles
WHERE rolname = 'product_user'
) THEN
CREATE ROLE product_user WITH LOGIN PASSWORD 'admin';
END IF;
END
$$;

GRANT ALL PRIVILEGES ON DATABASE product_catalog TO product_user;

DROP TABLE IF EXISTS products;

CREATE TABLE products (
id VARCHAR(50) PRIMARY KEY,
name VARCHAR(255),
description TEXT,
picture VARCHAR(255),
price_usd_currency_code VARCHAR(10),
price_usd_units INT,
price_usd_nanos INT,
categories TEXT[]
);


INSERT INTO products (id, name, description, picture, price_usd_currency_code, price_usd_units, price_usd_nanos, categories)
VALUES
('OLJCESPC7Z', 'National Park Foundation Explorascope', 'The National Park Foundation’s (NPF) Explorascope 60AZ is a manual alt-azimuth, refractor telescope perfect for celestial viewing on the go. The NPF Explorascope 60 can view the planets, moon, star clusters and brighter deep sky objects like the Orion Nebula and Andromeda Galaxy.', 'NationalParkFoundationExplorascope.jpg', 'USD', 101, 960000000, '{"telescopes"}'),
('66VCHSJNUP', 'Starsense Explorer Refractor Telescope', 'The first telescope that uses your smartphone to analyze the night sky and calculate its position in real time. StarSense Explorer is ideal for beginners thanks to the app’s user-friendly interface and detailed tutorials. It’s like having your own personal tour guide of the night sky', 'StarsenseExplorer.jpg', 'USD', 349, 950000000, '{"telescopes"}'),
('1YMWWN1N4O', 'Eclipsmart Travel Refractor Telescope', 'Dedicated white-light solar scope for the observer on the go. The 50mm refracting solar scope uses Solar Safe, ISO compliant, full-aperture glass filter material to ensure the safest view of solar events. The kit comes complete with everything you need, including the dedicated travel solar scope, a Solar Safe finderscope, tripod, a high quality 20mm (18x) Kellner eyepiece and a nylon backpack to carry everything in. This Travel Solar Scope makes it easy to share the Sun as well as partial and total solar eclipses with the whole family and offers much higher magnifications than you would otherwise get using handheld solar viewers or binoculars.', 'EclipsmartTravelRefractorTelescope.jpg', 'USD', 129, 950000000, '{"telescopes", "travel"}'),
('L9ECAV7KIM', 'Lens Cleaning Kit', 'Wipe away dust, dirt, fingerprints and other particles on your lenses to see clearly with the Lens Cleaning Kit. This cleaning kit works on all glass and optical surfaces, including telescopes, binoculars, spotting scopes, monoculars, microscopes, and even your camera lenses, computer screens, and mobile devices. The kit comes complete with a retractable lens brush to remove dust particles and dirt and two options to clean smudges and fingerprints off of your optics, pre-moistened lens wipes and a bottled lens cleaning fluid with soft cloth.', 'LensCleaningKit.jpg', 'USD', 21, 950000000, '{"accessories"}'),
('2ZYFJ3GM2N', 'Roof Binoculars', 'This versatile, all-around binocular is a great choice for the trail, the stadium, the arena, or just about anywhere you want a close-up view of the action without sacrificing brightness or detail. It’s an especially great companion for nature observation and bird watching, with ED glass that helps you spot the subtlest field markings and a close focus of just 6.5 feet.', 'RoofBinoculars.jpg', 'USD', 209, 950000000, '{"binoculars"}'),
('0PUK6V6EV0', 'Solar System Color Imager', 'You have your new telescope and have observed Saturn and Jupiter. Now you are ready to take the next step and start imaging them. But where do you begin? The NexImage 10 Solar System Imager is the perfect solution.', 'SolarSystemColorImager.jpg', 'USD', 175, 0, '{"accessories", "telescopes"}'),
('LS4PSXUNUM', 'Red Flashlight', 'This 3-in-1 device features a 3-mode red flashlight, a hand warmer, and a portable power bank for recharging your personal electronics on the go. Whether you use it to light the way at an astronomy star party, a night walk, or wildlife research, ThermoTorch 3 Astro Red’s rugged, IPX4-rated design will withstand your everyday activities.', 'RedFlashlight.jpg', 'USD', 57, 80000000, '{"accessories", "flashlights"}'),
('9SIQT8TOJO', 'Optical Tube Assembly', 'Capturing impressive deep-sky astroimages is easier than ever with Rowe-Ackermann Schmidt Astrograph (RASA) V2, the perfect companion to today’s top DSLR or astronomical CCD cameras. This fast, wide-field f/2.2 system allows for shorter exposure times compared to traditional f/10 astroimaging, without sacrificing resolution. Because shorter sub-exposure times are possible, your equatorial mount won’t need to accurately track over extended periods. The short focal length also lessens equatorial tracking demands. In many cases, autoguiding will not be required.', 'OpticalTubeAssembly.jpg', 'USD', 3599, 0, '{"accessories", "telescopes", "assembly"}'),
('6E92ZMYYFZ', 'Solar Filter', 'Enhance your viewing experience with EclipSmart Solar Filter for 8” telescopes. With two Velcro straps and four self-adhesive Velcro pads for added safety, you can be assured that the solar filter cannot be accidentally knocked off and will provide Solar Safe, ISO compliant viewing.', 'SolarFilter.jpg', 'USD', 69, 950000000, '{"accessories", "telescopes"}'),
('HQTGWGPNH4', 'The Comet Book', 'A 16th-century treatise on comets, created anonymously in Flanders (now northern France) and now held at the Universitätsbibliothek Kassel. Commonly known as The Comet Book (or Kometenbuch in German), its full title translates as “Comets and their General and Particular Meanings, According to Ptolomeé, Albumasar, Haly, Aliquind and other Astrologers”. The image is from https://publicdomainreview.org/collection/the-comet-book, made available by the Universitätsbibliothek Kassel under a CC-BY SA 4.0 license (https://creativecommons.org/licenses/by-sa/4.0/)', 'TheCometBook.jpg', 'USD', 0, 990000000, '{"books"}');

124 changes: 0 additions & 124 deletions src/productcatalogservice/products/products.json

This file was deleted.

0 comments on commit b9336c8

Please sign in to comment.