Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculator gRPC Service

CI/CD Pipeline Package Build Pull Request Validation Code Quality

A modern, production-ready calculator implementation using gRPC for service communication in .NET 9.0. This project demonstrates enterprise-grade distributed system architecture with client-server communication, containerization, cloud deployment, and comprehensive DevOps practices.

🆕 What's New (November 2025)

  • 📤 Docker Hub Integration: Images now published to pskumar81/calculator-grpc-server and pskumar81/calculator-grpc-client
  • 🎮 Container Management: New run-containers.ps1 script for complete container lifecycle management
  • ☁️ Azure VM Testing: Successfully tested and verified Docker container deployment on Azure VMs
  • ⚡ Quick Start: Run directly from Docker Hub without building - just docker run
  • 🔄 Consistent Naming: Standardized Docker image and container naming conventions
  • 🌐 Cross-Platform: Enhanced PowerShell and Bash script support

🚀 Key Features

  • gRPC Communication: High-performance HTTP/2-based service communication
  • Cloud-Ready: Azure VM deployment with Infrastructure as Code (ARM templates & Terraform)
  • Production Security: Non-root Docker containers, health checks, proper error handling
  • Consistent Naming: Standardized Docker image naming (calculator.grpc.server, calculator.grpc.client)
  • Docker Hub Integration: Public images available at pskumar81/calculator-grpc-server and pskumar81/calculator-grpc-client
  • Container Management: Comprehensive lifecycle management with run-containers.ps1 script
  • Cross-Platform: PowerShell and Bash scripts for Windows, Linux, and macOS
  • Comprehensive Monitoring: Application Insights integration and structured logging
  • Automated Build System: Cake build automation with cross-platform support
  • Package Distribution: Available as NuGet and npm packages

📦 Package Ecosystem

NuGet Packages

  • Calculator.Server - gRPC server library and service implementation
  • Calculator.Client - gRPC client library for consuming calculator services

npm Packages

  • @calculator/web - Angular web client library for calculator services

🏗️ Project Architecture

Calculator.Server - gRPC Service

  • Core Operations: Add, Subtract, Multiply, Divide with validation
  • Production Security: Non-root user, proper permissions
  • Health Monitoring: Built-in health checks and Application Insights
  • Cloud Integration: Azure VM ready with externalized configuration
  • Protocol Support: HTTP/2 gRPC and gRPC-Web for browsers
  • Docker: calculator.grpc.server:latest with metadata labels

Calculator.Client - Console Client

  • Interactive Interface: Menu-driven console application
  • Configuration Flexibility: Environment variables and appsettings.json
  • Error Handling: Graceful server connection error management
  • Dependency Injection: Proper service registration and DI patterns
  • Docker: calculator.grpc.client:latest with security hardening

Calculator.Web - Angular Frontend

  • Modern UI: Responsive design with real-time validation
  • gRPC-Web: Browser-compatible gRPC communication
  • TypeScript: Type-safe client implementation
  • Container Ready: Nginx-based production container

Calculator.Tests - Comprehensive Testing

  • Unit Tests: xUnit-based testing with edge cases
  • Mock Testing: Proper server call context mocking
  • Coverage: All operations and error scenarios

Enhanced Build System

Cake Build Automation

# Cross-platform build scripts
./build.ps1 --target=CI        # Windows PowerShell
./build.sh --target=CI         # Linux/macOS Bash

# Available targets
dotnet cake --target=Build     # Compile solution
dotnet cake --target=Test      # Run unit tests  
dotnet cake --target=Pack      # Create NuGet/npm packages
dotnet cake --target=CI        # Full CI pipeline
dotnet cake --target=Publish   # Publish to registries

Docker Build Scripts (NEW ✨)

# Consistent image building
./build-docker-images.ps1      # Windows PowerShell
./build-docker-images.sh       # Linux/macOS Bash

# Creates standardized images:
# calculator.grpc.server:latest
# calculator.grpc.client:latest

Docker Hub Publishing (NEW ✨)

# Login to Docker Hub
docker login

# Tag images for Docker Hub
docker tag calculator.grpc.server:latest pskumar81/calculator-grpc-server:latest
docker tag calculator.grpc.client:latest pskumar81/calculator-grpc-client:latest

# Push to Docker Hub
docker push pskumar81/calculator-grpc-server:latest
docker push pskumar81/calculator-grpc-client:latest

# Public repositories now available:
# https://hub.docker.com/r/pskumar81/calculator-grpc-server
# https://hub.docker.com/r/pskumar81/calculator-grpc-client

Using Published Images

# Pull from Docker Hub
docker pull pskumar81/calculator-grpc-server:latest
docker pull pskumar81/calculator-grpc-client:latest

# Run server from Docker Hub
docker run -p 5002:5002 pskumar81/calculator-grpc-server:latest

# Run client from Docker Hub
docker run -it --rm -e SERVER_URL="http://host.docker.internal:5002" pskumar81/calculator-grpc-client:latest

Container Management (NEW ✨)

# Comprehensive container lifecycle management
./run-containers.ps1 start      # Start server and create network
./run-containers.ps1 client     # Run interactive client
./run-containers.ps1 stop       # Stop and remove containers
./run-containers.ps1 restart    # Restart containers
./run-containers.ps1 status     # Show container status
./run-containers.ps1 logs       # Show container logs
./run-containers.ps1 cleanup    # Remove containers and network

# Features:
# - Automatic network creation (calculator-grpc-network)
# - Consistent container naming (calculator-grpc-server/client)
# - Health monitoring and status reporting
# - Cross-platform support (PowerShell/Bash)

☁️ Azure Cloud Deployment (NEW ✨)

Infrastructure as Code

  • ARM Templates: Complete Azure VM provisioning (azure-infrastructure/calculator-vms.json)
  • Terraform Configuration: Alternative IaC option with resource definitions
  • Deployment Scripts: Automated deployment with PowerShell and Bash
  • Production Configuration: Externalized settings for cloud environments

Azure Architecture

┌─────────────────┐    ┌─────────────────┐
│   Client VM     │    │   Server VM     │
│  (10.0.1.5)     │────│  (10.0.1.4)     │
│ calculator.grpc │gRPC│ calculator.grpc │
│    .client      │5002│    .server      │
└─────────────────┘    └─────────────────┘
         │                       │
         └───────────────────────┘
              Azure VNet
            (10.0.0.0/16)

Cloud Deployment Commands

# Deploy to Azure VMs
./deployment-scripts/Deploy-Azure.ps1    # PowerShell
./deployment-scripts/deploy-azure.sh     # Bash

# Provision infrastructure
az deployment group create \
  --resource-group calculator-grpc-rg \
  --template-file azure-infrastructure/calculator-vms.json

✅ Testing Status

  • Azure VM Deployment: ✅ Successfully tested and verified
  • Docker Container Loading: ✅ Images successfully pulled from Docker Hub to Azure VM
  • gRPC Communication: ✅ Server-client communication working on Azure infrastructure
  • Network Configuration: ✅ Container networking and port exposure verified
  • Production Readiness: ✅ Confirmed deployment process for production use

🐳 Enhanced Docker Support

Security Hardened Containers

  • Non-root execution: Runs as calculator user (not root)
  • Minimal attack surface: Multi-stage builds with runtime-only final images
  • Health checks: Built-in container health monitoring
  • Proper permissions: Correct file ownership and access controls
  • Metadata labels: Rich container labeling for identification

Consistent Image Naming

Component Image Name Description
Server calculator.grpc.server:latest gRPC service with health checks
Client calculator.grpc.client:latest Interactive console application

Docker Usage Examples

# Build standardized images
./build-docker-images.ps1

# Run server (production-ready)
docker run -d -p 5002:5002 \
  --name calc-server \
  calculator.grpc.server

# Run client (interactive)
docker run -it --rm \
  -e SERVER_URL="http://host.docker.internal:5002" \
  calculator.grpc.client

# Docker Compose (complete stack)
docker-compose up --build

📊 Monitoring & Observability

Application Insights Integration

  • Real-time telemetry: Performance metrics and error tracking
  • Custom events: Business logic monitoring
  • Dependency tracking: External service call monitoring
  • Log correlation: Distributed tracing across services

Health Checks

  • Endpoint monitoring: /health endpoint for container orchestration
  • Dependency validation: Database and external service health
  • Graceful degradation: Service availability reporting

Structured Logging

// Enhanced logging with correlation IDs
_logger.LogInformation("Operation {Operation} executed for numbers {Number1} and {Number2} with result {Result}", 
    "Add", request.Number1, request.Number2, result);

🛠️ Technical Stack & Standards

Core Technologies

  • .NET 9.0: Latest LTS runtime with performance improvements
  • gRPC/HTTP2: High-performance binary protocol
  • Angular 17: Modern frontend with TypeScript
  • Docker: Containerization with security best practices
  • Azure: Cloud-native deployment ready

Development Practices

  • Infrastructure as Code: ARM templates and Terraform
  • GitOps: Version-controlled infrastructure and deployments
  • Security: Non-root containers, health checks, proper error handling
  • Monitoring: Application Insights and structured logging
  • Testing: Comprehensive unit tests with edge case coverage

🚀 Quick Start Guide

Option 1: Docker Hub (Fastest)

# Run directly from published images
docker run -d -p 5002:5002 --name calc-server pskumar81/calculator-grpc-server:latest
docker run -it --rm -e SERVER_URL="http://host.docker.internal:5002" pskumar81/calculator-grpc-client:latest

# No build required - images are pre-built and ready to use!

Option 2: Docker (Build Locally)

# Clone and build
git clone https://github.com/pskumar81/Calculator.Grpc.git
cd Calculator.Grpc
./build-docker-images.ps1

# Run complete stack
docker run -d -p 5002:5002 --name calc-server calculator.grpc.server
docker run -it --rm -e SERVER_URL="http://host.docker.internal:5002" calculator.grpc.client

Option 3: Local Development

# Build solution
dotnet build

# Terminal 1: Start server
cd Calculator.Server && dotnet run

# Terminal 2: Run client  
cd Calculator.Client && dotnet run

Option 4: Azure Cloud Deployment

# Method 1: Infrastructure deployment (ARM templates)
az login
./deployment-scripts/Deploy-Azure.ps1

# Method 2: Manual Docker deployment to existing Azure VM
# SSH to Azure VM
ssh -i your-key.pem azureuser@your-vm-ip

# Install Docker and pull images from Docker Hub
sudo apt update && sudo apt install -y docker.io
docker pull pskumar81/calculator-grpc-server:latest
docker pull pskumar81/calculator-grpc-client:latest

# Run containers on Azure VM
docker network create calculator-grpc-network
docker run -d --name calculator-grpc-server --network calculator-grpc-network -p 5002:5002 pskumar81/calculator-grpc-server:latest
docker run -it --rm --name calculator-grpc-client --network calculator-grpc-network -e SERVER_URL="http://calculator-grpc-server:5002" pskumar81/calculator-grpc-client:latest

# ✅ Tested and verified working on Azure VMs

📦 Package Usage Examples

NuGet Server Package

dotnet add package Calculator.Server
using Calculator.Server.Extensions;
using Microsoft.AspNetCore.Server.Kestrel.Core;

var builder = WebApplication.CreateBuilder(args);

// Add calculator services with gRPC
builder.Services.AddCalculatorServices();

// Configure for HTTP/2 (required for gRPC)
builder.Services.Configure<KestrelServerOptions>(options =>
{
    options.ListenAnyIP(5002, o => o.Protocols = HttpProtocols.Http2);
});

var app = builder.Build();

// Configure gRPC pipeline
app.MapGrpcService<CalculatorServiceImpl>();
app.MapGrpcReflectionService();

app.Run();

NuGet Client Package

dotnet add package Calculator.Client
using Calculator.Client.Extensions;
using Calculator.Client.Services.Interfaces;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddCalculatorClient("https://localhost:5002");
    })
    .Build();

var calculatorClient = host.Services.GetRequiredService<ICalculatorClientService>();

// Perform calculations
var sum = await calculatorClient.AddAsync(10, 5);
var difference = await calculatorClient.SubtractAsync(10, 5);
var product = await calculatorClient.MultiplyAsync(10, 5);
var quotient = await calculatorClient.DivideAsync(10, 5);

Console.WriteLine($"Results: {sum}, {difference}, {product}, {quotient}");

npm Web Package

npm install @calculator/web
import { CalculatorModule } from '@calculator/web';

@NgModule({
  imports: [
    CalculatorModule.forRoot({
      serverUrl: 'https://localhost:5002'
    })
  ]
})
export class AppModule { }

🧪 Testing & Quality Assurance

Running Tests

# Unit tests
dotnet test

# With coverage
dotnet test --collect:"XPlat Code Coverage"

# Cake build system tests
dotnet cake --target=Test

Test Coverage

  • All gRPC Operations: Add, Subtract, Multiply, Divide
  • Edge Cases: Division by zero, overflow scenarios
  • Error Handling: Invalid inputs and server errors
  • Mock Testing: Proper ServerCallContext mocking
  • Integration Tests: Client-server communication

🔐 Security Features

Container Security

  • Non-root execution: All containers run as calculator user
  • Minimal base images: Using official Microsoft runtime images
  • Health monitoring: Built-in health checks for orchestration
  • Resource limits: Proper CPU and memory constraints

Network Security

  • HTTP/2 encryption: gRPC communication over encrypted channels
  • Container isolation: Docker network isolation between services
  • Certificate tools: mTLS certificate generation utilities
  • Environment-based config: Externalized sensitive configuration

Application Security

  • Input validation: All operations validate numeric inputs
  • Error handling: Structured error responses without sensitive data
  • Logging: Comprehensive audit trail without PII exposure
  • Dependency scanning: Automated vulnerability assessment

📋 Configuration Management

Server Configuration (appsettings.json)

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://0.0.0.0:5002"
      }
    }
  },
  "Azure": {
    "ApplicationInsights": {
      "InstrumentationKey": "${APPINSIGHTS_INSTRUMENTATIONKEY}"
    }
  }
}

Client Configuration

{
  "CalculatorService": {
    "ServerUrl": "http://localhost:5002"
  }
}

Environment Variables

  • SERVER_URL: Override client server connection
  • ASPNETCORE_ENVIRONMENT: Runtime environment (Development/Production)
  • APPINSIGHTS_INSTRUMENTATIONKEY: Azure monitoring key
  • AZURE_*: Cloud deployment metadata

🔄 CI/CD & DevOps

GitHub Actions Workflows

  • CI/CD Pipeline: Automated build, test, and deployment
  • Pull Request Validation: Quick PR checks and validations
  • Code Quality Analysis: SonarCloud and CodeQL security scanning
  • Package Publishing: Automated NuGet and npm package releases
  • Container Registry: GitHub Container Registry integration

Deployment Strategies

  • Docker Compose: Local development and testing
  • Azure VMs: Production cloud deployment with ARM templates
  • Kubernetes Ready: Helm charts and deployment manifests available
  • Automated Rollback: Infrastructure and application rollback capabilities

🚀 Performance & Scalability

gRPC Advantages

  • HTTP/2 Binary Protocol: ~7x faster than REST/JSON
  • Bidirectional Streaming: Efficient client-server communication
  • Protocol Buffers: Compact serialization with schema evolution
  • Connection Multiplexing: Single connection for multiple requests

Container Optimizations

  • Multi-stage Builds: 70% smaller production images (220MB vs 700MB)
  • Layer Caching: Dependency restoration cached independently
  • Health Checks: Zero-downtime deployments with proper readiness

Monitoring Metrics

  • Application Insights: Request duration, throughput, error rates
  • Container Metrics: CPU, memory, network utilization
  • Business Metrics: Operations per second, calculation accuracy

🏗️ Architecture Patterns

SOLID Principles Implementation

The project demonstrates enterprise architecture patterns:

  • Single Responsibility: Each service has one clear purpose
  • Open/Closed: Extensible without modifying existing code
  • Dependency Inversion: Depends on abstractions, not concretions
  • Interface Segregation: Client-specific interfaces

Microservice Patterns

  • Service Discovery: Environment-based configuration
  • Circuit Breaker: Graceful degradation on service failures
  • Health Checks: Container orchestration integration
  • Distributed Tracing: Request correlation across services

📁 Project Structure

Calculator.Grpc/
├── 🏗️ build.cake                     # Cake build automation
├── 🐳 docker-compose.yml             # Container orchestration  
├── 🔧 build-docker-images.ps1        # Consistent image building
├── 📤 push-to-dockerhub.ps1          # Docker Hub publishing
├── 🎮 run-containers.ps1             # Container management
│
├── Calculator.Server/                 # gRPC Server
│   ├── 📝 Dockerfile                 # Production-hardened container
│   ├── ⚙️ appsettings.json           # Server configuration
│   ├── 🏥 Program.cs                 # Host and health checks
│   └── 🔧 Services/                  # Business logic
│
├── Calculator.Client/                 # Interactive Client
│   ├── 📝 Dockerfile                 # Security-hardened container
│   ├── ⚙️ appsettings.json           # Client configuration  
│   ├── 🖥️ Program.cs                 # Interactive console
│   └── 🔧 Services/                  # gRPC communication
│
├── Calculator.Web/                    # Angular Frontend
│   ├── 📝 Dockerfile                 # Nginx production build
│   ├── 🅰️ angular.json               # Angular configuration
│   └── 🎨 src/                       # TypeScript implementation
│
├── ☁️ azure-infrastructure/           # Azure Deployment
│   ├── 📋 calculator-vms.json        # ARM template
│   ├── 🏗️ main.tf                    # Terraform configuration
│   └── 📜 *.sh                       # VM initialization scripts
│
├── 🚀 deployment-scripts/             # Deployment Automation
│   ├── 💻 Deploy-Azure.ps1           # PowerShell deployment
│   └── 🐧 deploy-azure.sh            # Bash deployment
│
└── Calculator.Tests/                  # Comprehensive Testing
    ├── 🧪 CalculatorServiceTests.cs   # Unit tests
    └── 🎭 Mock implementations

Roadmap & Future Enhancements

Planned Features

  • mTLS Security: Complete certificate-based authentication
  • Kubernetes Deployment: Helm charts and cluster deployment
  • Advanced Operations: Scientific calculator functions
  • Rate Limiting: DDoS protection and usage quotas
  • Caching: Redis integration for performance optimization
  • Database Integration: Calculation history persistence

Architecture Improvements

  • CQRS Pattern: Command/Query Responsibility Segregation
  • Event Sourcing: Audit trail and replay capabilities
  • Service Mesh: Istio integration for advanced networking
  • Distributed Caching: Multi-region cache coherence

💡 Learning Outcomes

This project demonstrates:

Cloud-Native Development

  • ✅ Container orchestration with Docker and Docker Compose
  • ✅ Infrastructure as Code with ARM templates and Terraform
  • ✅ Cloud deployment patterns for Azure VM hosting
  • ✅ Monitoring and observability with Application Insights

Modern .NET Practices

  • ✅ gRPC service development with .NET 9.0
  • ✅ Dependency injection and service patterns
  • ✅ Configuration management and environment separation
  • ✅ Structured logging and health monitoring

DevOps Excellence

  • ✅ Automated build systems with Cake
  • ✅ Docker security best practices
  • ✅ CI/CD pipeline implementation
  • ✅ Infrastructure automation and deployment

🤝 Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Implement changes with tests
  4. Ensure all builds pass (./build.ps1)
  5. Commit with conventional commits (feat: add amazing feature)
  6. Push to branch (git push origin feature/amazing-feature)
  7. Create a Pull Request

Development Setup

# Prerequisites
git clone https://github.com/pskumar81/Calculator.Grpc.git
dotnet tool restore
docker --version

# Build and test
./build.ps1 --target=CI
./build-docker-images.ps1

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Microsoft .NET Team for gRPC integration and tooling
  • Docker Community for container best practices
  • Azure Team for cloud infrastructure guidance
  • Open Source Community for inspiration and contributions

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages