From efc6e8a79cfab22d28289bbeb98bdf2777302189 Mon Sep 17 00:00:00 2001 From: dieg0code Date: Wed, 28 Aug 2024 00:53:17 -0400 Subject: [PATCH] dockerized products microservice --- products-microservice/Makefile | 18 ++++++++ products-microservice/docker-compose.yml | 24 ++++++++++ products-microservice/main.go | 2 + products-microservice/src/db/config.go | 17 ++++++- .../src/repository/product_repository_impl.go | 2 +- .../src/services/product_service_impl.go | 44 ++++++++++++------- 6 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 products-microservice/Makefile create mode 100644 products-microservice/docker-compose.yml diff --git a/products-microservice/Makefile b/products-microservice/Makefile new file mode 100644 index 0000000..567fd89 --- /dev/null +++ b/products-microservice/Makefile @@ -0,0 +1,18 @@ +create_network: + docker network create prod-network + +start_test_db: + docker run -d --name pg-prod --network prod-network -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -p 5432:5432 postgres:12.2 + +build: + docker build -t prod-microservice . + +run: + docker run -d --name prod-microservice --network prod-network -p 8080:8080 prod-microservice + +stop: + docker stop pg-prod + docker rm pg-prod + docker stop prod-microservice + docker rm prod-microservice + docker network rm prod-network \ No newline at end of file diff --git a/products-microservice/docker-compose.yml b/products-microservice/docker-compose.yml new file mode 100644 index 0000000..8f580a6 --- /dev/null +++ b/products-microservice/docker-compose.yml @@ -0,0 +1,24 @@ +version: '3.8' +services: + app: + build: . + ports: + - "8080:8080" + depends_on: + - db + environment: + - DB_HOST=db + - DB_PORT=5432 + - DB_USER=postgres + - DB_PASSWORD=password + - DB_NAME=products + db: + image: postgres:latest + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + - POSTGRES_DB=products + volumes: + - postgres_data:/var/lib/postgresql/data/ +volumes: + postgres_data: \ No newline at end of file diff --git a/products-microservice/main.go b/products-microservice/main.go index 7541fce..be9570c 100644 --- a/products-microservice/main.go +++ b/products-microservice/main.go @@ -43,4 +43,6 @@ func main() { if err != nil { log.Fatalf("Server failed to start: %v", err) } + + logrus.Info("Server started successfully") } diff --git a/products-microservice/src/db/config.go b/products-microservice/src/db/config.go index ed027fe..7c2e0e8 100644 --- a/products-microservice/src/db/config.go +++ b/products-microservice/src/db/config.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strconv" + "time" "github.com/sirupsen/logrus" "gorm.io/driver/postgres" @@ -18,10 +19,22 @@ func DatabaseConnection() *gorm.DB { dbName := os.Getenv("DB_NAME") dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbName) - db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + + var db *gorm.DB + var err error + + maxAttempts := 5 + for attempts := 1; attempts <= maxAttempts; attempts++ { + db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if err == nil { + break + } + logrus.WithError(err).Errorf("Failed to connect to database (attempt %d/%d)", attempts, maxAttempts) + time.Sleep(5 * time.Second) // Wait for 5 seconds before retrying + } if err != nil { - panic("Failed to connect to database!") + panic("Failed to connect to database after multiple attempts!") } return db diff --git a/products-microservice/src/repository/product_repository_impl.go b/products-microservice/src/repository/product_repository_impl.go index d3b92bd..9b975c3 100644 --- a/products-microservice/src/repository/product_repository_impl.go +++ b/products-microservice/src/repository/product_repository_impl.go @@ -120,7 +120,7 @@ func (p *ProductRepositoryImpl) UpdateProduct(prodctID uint, product *models.Pro } if !exists { - logrus.Error("Product not found") + logrus.WithField("product_id", product.ID).Errorf("Product with id %d not found", product.ID) return nil, errors.New("product not found") } diff --git a/products-microservice/src/services/product_service_impl.go b/products-microservice/src/services/product_service_impl.go index 5fd2af6..12ed304 100644 --- a/products-microservice/src/services/product_service_impl.go +++ b/products-microservice/src/services/product_service_impl.go @@ -30,6 +30,7 @@ func (p *ProductServiceImpl) CreateProduct(product *request.CreateProductRequest return nil, err } + logrus.WithField("product_id", createdProduct.ID).Info("Product created successfully") return &createdProduct.ID, nil } @@ -46,6 +47,8 @@ func (p *ProductServiceImpl) DeleteProduct(productID uint) error { return err } + logrus.WithField("product_id", productID).Info("Product deleted successfully") + return nil } @@ -63,14 +66,17 @@ func (p *ProductServiceImpl) GetAllProducts(page int, pageSize int) ([]response. var productResponses []response.ProductResponse for _, product := range products { productResponses = append(productResponses, response.ProductResponse{ - ProductID: product.ID, - Name: product.Name, - Category: product.Category, - Price: product.Price, - Stock: product.Stock, + ProductID: product.ID, + Name: product.Name, + Category: product.Category, + Price: product.Price, + Stock: product.Stock, + LastUpdate: product.UpdatedAt.Format("02-01-2006"), }) } + logrus.WithField("total_products", len(productResponses)).Info("Products retrieved successfully") + return productResponses, nil } @@ -86,14 +92,17 @@ func (p *ProductServiceImpl) GetByCategory(category string) ([]response.ProductR var productResponses []response.ProductResponse for _, product := range products { productResponses = append(productResponses, response.ProductResponse{ - ProductID: product.ID, - Name: product.Name, - Category: product.Category, - Price: product.Price, - Stock: product.Stock, + ProductID: product.ID, + Name: product.Name, + Category: product.Category, + Price: product.Price, + Stock: product.Stock, + LastUpdate: product.UpdatedAt.Format("02-01-2006"), }) } + logrus.WithField("total_products", len(productResponses)).Info("Products retrieved successfully") + return productResponses, nil } @@ -107,13 +116,16 @@ func (p *ProductServiceImpl) GetProductById(ProductID uint) (*response.ProductRe } productResponse := &response.ProductResponse{ - ProductID: product.ID, - Name: product.Name, - Category: product.Category, - Price: product.Price, - Stock: product.Stock, + ProductID: product.ID, + Name: product.Name, + Category: product.Category, + Price: product.Price, + Stock: product.Stock, + LastUpdate: product.UpdatedAt.Format("02-01-2006"), } + logrus.WithField("product_id", product.ID).Info("Product retrieved successfully") + return productResponse, nil } @@ -141,6 +153,8 @@ func (p *ProductServiceImpl) UpdateProduct(productID uint, product *request.Upda Stock: updatedProduct.Stock, } + logrus.WithField("product_id", updatedProduct.ID).Info("Product updated successfully") + return productResponse, nil }