Pack Calculator is a Go-based web application that optimizes packing solutions for given order sizes. It's designed with clean architecture principles, ensuring scalability and maintainability.
We approached this challenge by:
- Implementing a greedy algorithm to calculate the optimal pack combination.
- Designing the system using clean architecture principles, separating concerns into distinct layers:
- Domain layer: Core business logic and entities
- Use Case layer: Application-specific business rules
- Interface layer: Adapters for external agency (e.g., HTTP handlers)
- Infrastructure layer: Frameworks and tools (e.g., database, external services)
- Creating a RESTful API for easy integration with other systems.
- Developing a simple web interface for user interaction.
- Containerizing the application for easy deployment and scaling.
- Allowing customization of pack sizes through environment variables and API endpoints for flexibility.
- Calculate optimal pack combinations for any order size 🧮
- RESTful API for system integration 🔌
- User-friendly web interface 🖥️
- Configurable pack sizes via environment variables and API 🔧
- Docker support for easy deployment 🐳
- Comprehensive Makefile for streamlined development 🛠️
- Google Cloud Run deployment for cloud scalability ☁️
- CI/CD (configured on the GCP side when merging into main) pipeline for automated testing and deployment 🚀
- Graceful shutdown for clean resource management 🛑
- Go 1.22 or higher
- Docker and Docker Compose
- Make
-
Clone the repository:
git clone https://github.com/krl4k/pack-calculator.git cd pack-calculator
-
Install dependencies:
go mod download
You can customize the available pack sizes by setting the PACK_SIZES
environment variable. The sizes should be comma-separated integers.
Example:
export PACK_SIZES=250,500,1000,2000,5000
If not set, the application will use default pack sizes.
To set environment variables when running with Docker or Docker Compose, you can:
-
Modify the
docker-compose.yml
file:services: app: environment: - PACK_SIZES=250,500,1000,2000,5000
-
Or use a
.env
file in your project root:PACK_SIZES=250,500,1000,2000,5000
-
Or set it directly when running Docker:
docker run -e PACK_SIZES=250,500,1000,2000,5000 your-image-name
make test
: Run unit tests 🧪make build
: Compile the binary 🔨make run
: Build and run the application using Docker Compose 🚀make stop
: Stop the running Docker containers 🛑make clean
: Clean up build artifacts and Docker resources 🧹make deploy-gcp
: Deploy the application to Google Cloud Run ☁️
-
Build and run the application:
make run
-
Open a web browser and navigate to
http://localhost:8080
To stop the application:
make stop
The application exposes the following RESTful API endpoints:
- Endpoint:
/api/calculate
- Method: GET
- Query Parameter:
orderSize
(integer)
Example:
GET /api/calculate?orderSize=501
Response:
[
{"Size": 500, "Count": 1},
{"Size": 250, "Count": 1}
]
- Endpoint:
/api/pack-sizes
- Method: GET
Example:
GET /api/pack-sizes
Response:
[250, 500, 1000, 2000, 5000]
- Endpoint:
/api/pack-sizes
- Method: PUT
- Body: JSON array of integers
Example:
PUT /api/pack-sizes
Content-Type: application/json
[250, 500, 1000, 2000, 5000]
Response:
Pack sizes updated successfully
To deploy the application to Google Cloud Run:
make deploy-gcp
This command builds the Docker image, pushes it to Google Container Registry, and deploys it to Cloud Run.
Remember to set the PACK_SIZES
environment variable in your Cloud Run configuration if you want to use custom pack sizes.