Skip to content

Commit

Permalink
dockerized products microservice
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieg0Code committed Aug 28, 2024
1 parent f8c3568 commit efc6e8a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
18 changes: 18 additions & 0 deletions products-microservice/Makefile
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions products-microservice/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 2 additions & 0 deletions products-microservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ func main() {
if err != nil {
log.Fatalf("Server failed to start: %v", err)
}

logrus.Info("Server started successfully")
}
17 changes: 15 additions & 2 deletions products-microservice/src/db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"time"

"github.com/sirupsen/logrus"
"gorm.io/driver/postgres"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
44 changes: 29 additions & 15 deletions products-microservice/src/services/product_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -46,6 +47,8 @@ func (p *ProductServiceImpl) DeleteProduct(productID uint) error {
return err
}

logrus.WithField("product_id", productID).Info("Product deleted successfully")

return nil
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit efc6e8a

Please sign in to comment.