Budget-Management-Backend-API

Budget Management API - A Comprehensive, Microservices-Based API for Managing Budgets, Expenses, Users, and More! πŸ’°

Welcome to the Budget Management API, a robust, microservices backend platform for managing budgets, expenses, users, orders, and notifications. Built with Node.js, Express, and TypeScript, it supports advanced features like GraphQL, gRPC, WebSockets, and REST APIs.

The API also integrates with PostgreSQL, MongoDB, MySQL, Redis, RabbitMQ, Kafka, and Elasticsearch, and is containerized with Docker and orchestrated using Kubernetes that fully supports zero-downtime deployment (blue/green & canary deployment). It also includes a developer-friendly CLI tool and interactive Swagger/OpenAPI documentation for exploring and testing endpoints.

Budget Management API Logo

Below is a very comprehensive guide to setting up, running, and utilizing this API. πŸ’ΈπŸš€

Table of Contents

  1. Overview
  2. Live Deployment
  3. Technologies Used
  4. Project Structure
  5. Setup Instructions
  6. Available Endpoints
  7. Schemas
  8. Features and Integrations
  9. Environment Variables
  10. CLI Usage
  11. Demo Frontend UI
  12. Swagger Documentation
  13. GraphQL Integration
  14. NGINX Configuration
  15. gRPC Integration
  16. Dockerization
  17. Kubernetes Deployment
  18. Spring Boot Backends with Gradle & Maven
  19. Dotnet Backends with C#
  20. Continuous Integration and Deployment with Jenkins
  21. Testing
  22. Contributing
  23. Author

Overview

The Budget Management API is designed to handle complex budget management requirements, including:

The purpose of this API is to demonstrate the capabilities of modern backend technologies and provide a foundation for building scalable, real-time applications. It can be used as a reference for developers looking to implement similar features in their projects. Simply clone the repository, set up the environment, and start building the frontend or additional functionality on top of the existing API!

JavaScript TypeScript Java C# Node.js Express.js Spring Boot ASP.NET Core RabbitMQ Kafka MongoDB PostgreSQL MySQL Redis Elasticsearch REST GraphQL gRPC WebSockets Swagger OpenAPI Docker Kubernetes Jenkins NGINX PM2 Render Vercel Netlify Prometheus Grafana Maven Gradle npm Git GitHub GitHub Actions Node CLI ESLint Prettier Jest Mocha Chai Microservices

Live Deployment

The Budget Management API is deployed live at https://budget-management-backend-api.onrender.com.

Additionally, a mock frontend UI is also available, hosted at https://budget-manage-app.vercel.app.

You can access the API and test the endpoints directly from the browser. Feel free to use the API for your own projects or applications. Simply add some attribution to the original repository and the creator. Also, be sure that you use your own credentials and tokens, otherwise your data may clash with mine and other users’ data!

[!IMPORTANT] Be mindful of the rate limits and usage policies when testing the live API. Additionally, because the API is hosted on the free plan of Render, it may take a while (1-2 minutes) to wake up if it has been inactive for some time. Kindly be patient during this process!

[!NOTE] Backup Frontend: https://budget-management-system.netlify.app.

Deployment and Technology Status

API Status MongoDB Redis PostgreSQL RabbitMQ Kafka Elasticsearch gRPC GraphQL Swagger Docs Docker Kubernetes WebSockets Nginx OpenAPI Jenkins CI/CD Prometheus Grafana Maven Package Gradle Package Spring Boot Dotnet Vercel Render Netlify

Technologies Used

Technology Purpose
Node.js Core application framework.
Express.js Web application framework for building APIs.
MongoDB Primary NoSQL database for managing budgets and expenses.
PostgreSQL Relational database for transaction logs.
MySQL Optional relational database support.
Redis In-memory database for caching.
RabbitMQ Message broker for task queuing.
Kafka Distributed event streaming platform.
Elasticsearch Advanced search engine for querying data.
gRPC High-performance remote procedure call framework.
GraphQL Query language for fetching and manipulating data.
WebSocket Real-time communication for notifications.
Swagger/OpenAPI API documentation and testing.
Docker Containerization for easy deployment.
Kubernetes Orchestrating containerized applications at scale.
Nginx Reverse proxy and load balancer.
Prometheus Monitoring and alerting toolkit.
Grafana Observability and visualization platform.
Jenkins CI/CD pipeline for automated testing and deployment.

Microservices Architecture

The Budget Management API is designed with a microservices architecture, allowing for modular development and deployment. Each service can be developed, deployed, and scaled independently, providing flexibility and resilience.

  1. Authentication/User Service: Handles user registration, login, and JWT token management (+ profile management as well).
  2. Budget Service: Manages budgets, including creation, updates, and retrieval.
  3. Expense Service: Manages expenses associated with budgets, including CRUD operations.
  4. Customer Service: Manages customer data, including creation and updates.
  5. Search Service: Provides advanced search capabilities using Elasticsearch.
  6. Order Service: Manages orders, including creation and updates.
  7. Transaction Service: Logs transactions for budgets and expenses.
  8. Notification Service: Sends real-time notifications to users via WebSockets.
  9. Task Management Service: Handles asynchronous tasks using RabbitMQ and Kafka.

This architecture allows for easy integration of additional services, such as order management, task management, and notification services, without affecting the core functionality of the API.

Communication Between Services

Each service communicates with others using RabbitMQ for asynchronous messaging, gRPC for high-performance remote procedure calls, and REST APIs for standard HTTP communication. This ensures that services can operate independently while still being able to interact with each other as needed.

Also, the API is designed to be modular, allowing for easy addition of new services or features without disrupting existing functionality. This modularity is achieved through the use of separate directories for each service, with shared utilities and configurations in a common structure.

[!TIP] Each service does not live in its own directory, but rather is organized within the main project structure. This allows for easier management and deployment of the entire application as a single unit, while still maintaining the modularity of the microservices architecture.

Microservices Diagram

Microservices Architecture Diagram

[!NOTE] This diagram illustrates the microservices architecture of the Budget Management API, showing how different services interact with each other and external clients. Each service can be independently developed and deployed, allowing for scalability and maintainability.

Full Architecture Diagram

Architecture Diagram

[!NOTE] This architecture diagram illustrates the modular nature of the Budget Management API, showcasing how different services interact with each other and external clients. It also highlights the use of various technologies such as Docker, Kubernetes, and Nginx for deployment and scaling.

Project Structure

mindmap
  root((Budget-Management-Backend-API))
    Root Files
      ".env"[.env β€” Environment variables configuration]
      ".env.example"[.env.example β€” Sample environment config]
      ".gitignore"[.gitignore β€” Git ignore rules]
      ".prettierrc"[.prettierrc β€” Prettier formatting config]
      LICENSE[LICENSE β€” Project license]
      README[README.md β€” Project documentation]
      cli.js[cli.js β€” CLI tool for backend]
      docker-compose.yml[docker-compose.yml β€” Docker Compose configuration]
      Dockerfile[Dockerfile β€” Backend container image]
      grpcServer.js[gRPC server implementation]
      index.js[Main application entry point]
      nodemon.json[nodemon.json β€” Dev server reload config]
      openapi.yaml[openapi.yaml β€” API specification]
      package.json[package.json β€” NPM configuration]
      prometheus.yml[prometheus.yml β€” Monitoring configuration]
      start.sh[start.sh β€” Startup script]
    Tests
      "__tests__/app.test.js"[__tests__/app.test.js β€” Main test file]
    Docs
      "docs/swaggerConfig.js"[swaggerConfig.js β€” Swagger setup]
    GraphQL
      "graphql/schema.js"[schema.js β€” GraphQL schema definition]
    Proto
      "proto/budget.proto"[budget.proto β€” gRPC proto file]
    NGINX
      "nginx/docker-compose.yml"[docker-compose.yml β€” NGINX stack]
      "nginx/Dockerfile"[Dockerfile β€” NGINX image]
      "nginx/nginx.conf"[nginx.conf β€” Reverse proxy config]
      "nginx/start_nginx.sh"[start_nginx.sh β€” NGINX start script]
      "nginx/README.md"[README.md β€” NGINX documentation]
    Redis-Mongo Flow
      "redis-mongo-flow/app.js"[app.js β€” Express app for Redis-Mongo flow]
      "redis-mongo-flow/config.js"[config.js β€” Flow configuration]
      "redis-mongo-flow/package.json"[package.json β€” Module dependencies]
      "redis-mongo-flow/README.md"[README.md β€” Flow documentation]
      "redis-mongo-flow/seed.js"[seed.js β€” Data seeder]
      "redis-mongo-flow/test.js"[test.js β€” Flow test script]
    Round-Robin Load Balancer
      "round-robin/config.js"[config.js β€” Load balancer configuration]
      "round-robin/index.js"[index.js β€” Round-robin logic]
      "round-robin/README.md"[README.md β€” Load balancer docs]
    Services
      "services/dataSeeder.js"[dataSeeder.js β€” MongoDB seeder]
      "services/elasticService.js"[elasticService.js β€” Elasticsearch utilities]
      "services/jwtService.js"[jwtService.js β€” JWT helpers]
      "services/postgresService.js"[postgresService.js β€” PostgreSQL utilities]
      "services/rabbitMQService.js"[rabbitMQService.js β€” RabbitMQ client]
      "services/redisService.js"[redisService.js β€” Redis client]
      "services/websocketService.js"[websocketService.js β€” WebSocket server]
    Controllers
      "controllers/authController.js"[authController.js β€” Authentication endpoints]
      "controllers/budgetController.js"[budgetController.js β€” Budget endpoints]
      "controllers/customerController.js"[customerController.js β€” Customer endpoints]
      "controllers/expenseController.js"[expenseController.js β€” Expense endpoints]
      "controllers/notificationController.js"[notificationController.js β€” Notification endpoints]
      "controllers/orderController.js"[orderController.js β€” Order endpoints]
      "controllers/searchController.js"[searchController.js β€” Elasticsearch endpoints]
      "controllers/taskController.js"[taskController.js β€” Task endpoints]
      "controllers/transactionController.js"[transactionController.js β€” Transaction log endpoints]
      "controllers/userController.js"[userController.js β€” User profile endpoints]
    Middleware
      "middleware/authMiddleware.js"[authMiddleware.js β€” JWT authentication middleware]
    Models
      "models/budget.js"[budget.js β€” Budget schema]
      "models/customer.js"[customer.js β€” Customer schema]
      "models/expense.js"[expense.js β€” Expense schema]
      "models/order.js"[order.js β€” Order schema]
      "models/task.js"[task.js β€” Task schema]
      "models/user.js"[user.js β€” User schema]
    Routes
      "routes/authRoutes.js"[authRoutes.js β€” Auth routes]
      "routes/budgetRoutes.js"[budgetRoutes.js β€” Budget routes]
      "routes/customerRoutes.js"[customerRoutes.js β€” Customer routes]
      "routes/expenseRoutes.js"[expenseRoutes.js β€” Expense routes]
      "routes/graphqlRoutes.js"[graphqlRoutes.js β€” GraphQL routes]
      "routes/index.js"[index.js β€” Router entry point]
      "routes/notificationRoutes.js"[notificationRoutes.js β€” Notification routes]
      "routes/orderRoutes.js"[orderRoutes.js β€” Order routes]
      "routes/searchRoutes.js"[searchRoutes.js β€” Elasticsearch routes]
      "routes/taskRoutes.js"[taskRoutes.js β€” Task routes]
      "routes/transactionRoutes.js"[transactionRoutes.js β€” Transaction routes]
      "routes/userRoutes.js"[userRoutes.js β€” User profile routes]
    Views
      "views/android-chrome-192x192.png"[android-chrome-192x192.png β€” Icon]
      "views/android-chrome-512x512.png"[android-chrome-512x512.png β€” Icon]
      "views/apple-touch-icon.png"[apple-touch-icon.png β€” Icon]
      "views/favicon.ico"[favicon.ico β€” Favicon]
      "views/favicon-16x16.png"[favicon-16x16.png β€” Icon]
      "views/favicon-32x32.png"[favicon-32x32.png β€” Icon]
      "views/home.html"[home.html β€” Homepage template]
      "views/manifest.json"[manifest.json β€” Web app manifest]
    Other
      "and many more files..."[Additional supporting files and directories]

Setup Instructions

Prerequisites

Local Installation

  1. Clone the repository:
    git clone https://github.com/hoangsonww/Budget-Management-Backend-API.git
    cd Budget-Management-Backend-API
    
  2. Install dependencies:
    npm install
    
  3. Set up environment variables:
    • Create a .env file in the root directory:
       MONGO_DB_URI=mongodb://localhost:27017/budget_manager
       POSTGRES_URI=postgres://user:password@localhost:5432/budget_manager
       REDIS_URL=redis://localhost:6379
       RABBITMQ_URL=amqp://localhost
       KAFKA_BROKER=localhost:9092
       JWT_SECRET=your_secret_key
      
    • Replace placeholders with your actual configuration.
  4. Start the application:
    npm start
    
  5. Access the application:
    • API: http://localhost:3000
    • Swagger: http://localhost:3000/docs

Available Endpoints

Endpoint Method Description
/api/auth/register POST Register a new user.
/api/auth/login POST Login and receive a JWT token.
/api/auth/logout POST Logout and invalidate the token.
/api/auth/verify-email POST Verify the user’s email address.
/api/auth/reset-password POST Reset the user’s password.
/api/users/profile GET Get the authenticated user’s profile.
/api/budgets GET Get all budgets.
/api/budgets POST Create a new budget.
/api/budgets/:id GET Get a specific budget.
/api/budgets/:id PUT Update a budget.
/api/budgets/:id DELETE Delete a budget.
/api/customers GET Get all customers.
/api/customers POST Create a new customer.
/api/customers/:id GET Get a specific customer.
/api/customers/:id PUT Update a customer.
/api/customers/:id DELETE Delete a customer.
/api/expenses GET Get all expenses.
/api/expenses POST Add a new expense.
/api/expenses/:budgetId GET Get all expenses for a budget.
/api/expenses/:id PUT Update an expense.
/api/expenses/:id DELETE Delete an expense.
/api/orders GET Get all orders.
/api/orders POST Create a new order.
/api/orders/:id GET Get a specific order.
/api/orders/:id PUT Update an order.
/api/orders/:id DELETE Delete an order.
/api/transactions GET Get all transactions.
/api/transactions POST Add a new transaction.
/api/transactions/:id GET Get a specific transaction.
/api/transactions/:id PUT Update a transaction.
/api/transactions/:id DELETE Delete a transaction.
/api/tasks GET Get all tasks.
/api/tasks POST Add a new task.
/api/tasks/:id GET Get a specific task.
/api/tasks/:id PUT Update a task.
/api/tasks/:id DELETE Delete a task.
/api/graphql POST Perform a GraphQL query.
/api/notifications POST Send a real-time notification.
/api/search POST Search for expenses using Elasticsearch.

Additionally, the root / endpoint provides a welcome message and information about the API.

Available Endpoints

More endpoints and features are available in the API. Refer to the Swagger documentation for detailed information.

Schemas

User

| Field | Type | Description | |β€”β€”β€”β€”-|β€”β€”β€”-|—————————–| | username | String | Unique username. | | email | String | Unique email address. | | password | String | Hashed password. | | createdAt | Date | User creation date. |

Budget

| Field | Type | Description | |β€”β€”β€”β€”-|β€”β€”β€”-|—————————–| | name | String | Budget name. | | limit | Number | Budget limit. | | createdAt | Date | Budget creation date. |

Expense

| Field | Type | Description | |β€”β€”β€”β€”β€”|β€”β€”β€”-|β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”| | budgetId | String | ID of the associated budget. | | description | String | Expense description. | | amount | Number | Expense amount. | | createdAt | Date | Expense creation date. |

Order

| Field | Type | Description | |————–|β€”β€”β€”-|——————————–| | customerId | String | ID of the associated customer. | | amount | Number | Order amount. | | status | String | Order status. | | createdAt | Date | Order creation date. |

Customer

| Field | Type | Description | |β€”β€”β€”β€”-|β€”β€”β€”-|β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”| | name | String | Customer name. | | email | String | Customer email address. | | phone | String | Customer phone number. |

Task

Field Type Description
description String Task description.
status String Task status.
createdAt Date Task creation date.

Features and Integrations

gRPC

GraphQL

WebSocket

Docker

Elasticsearch

RabbitMQ

Kafka

Redis

PostgreSQL

MongoDB

Nginx

Kubernetes

Prometheus and Grafana

Services Interaction

The Budget Management API interacts with various services and databases to provide a comprehensive backend solution. The architecture includes a frontend UI, a CLI tool, an API gateway, a gRPC server, and multiple external services. Here is a high-level overview of the service interaction:

flowchart LR
    FE[Frontend UI<br/>]
    CLI[CLI Tool / gRPC Clients]
    APIGW[API Gateway / Express.js]
    GRPC[gRPC Server]
    CORE[Application Core<br/>Controllers & Services]
    Mongo[MongoDB]
    Postgres[PostgreSQL]
    Elastic[Elasticsearch]
    Redis[Redis Cache]
    Queue[RabbitMQ / Kafka Queue]
    Stream[Kafka Event Streaming]
    External[External Services<br/>Email, SMS, etc.]

    FE -->|HTTP / GraphQL requests| APIGW
    CLI -->|CLI commands / gRPC calls| GRPC
    APIGW <--> GRPC
    APIGW -->|REST / WebSocket / GraphQL| CORE
    GRPC --> CORE

    CORE --> Mongo
    CORE --> Postgres
    CORE --> Elastic
    CORE --> Redis
    CORE --> Queue
    CORE --> Stream
    Queue -->|Asynchronous tasks| External
    Stream -->|Event-driven actions| External

Environment Variables

Ensure your .env file looks like this before getting started:

# Server Configuration
PORT=

# MongoDB Configuration
MONGO_DB_URI=
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=

# Redis Configuration
REDIS_HOST=
REDIS_PORT=
REDIS_URL=

# RabbitMQ Configuration
RABBIT_MQ_HOST=
RABBITMQ_URL=

# Kafka Configuration
KAFKA_BROKER=

# JWT Secret Key
JWT_SECRET=

# Elasticsearch Configuration
ELASTIC_SEARCH_URL=

# PostgreSQL Configuration
POSTGRES_URL=

CLI Usage

The budget-manager CLI provides a convenient way to interact with the application from the command line.

Follow these steps to use the CLI:

  1. Install globally:
    npm link
    
  2. Use commands:
    budget-manager seed
    budget-manager notify "Hello!"
    budget-manager add-task "Task description"
    
  3. View available commands:
     budget-manager --help
    

This will display a list of available commands and options:

  budget-manager --help
  Usage: budget-manager [options] [command]
  
  A CLI for managing budgets, tasks, orders, and more.
  
  Options:
    -V, --version                        output the version number
    -h, --help                           display help for command
  
  Commands:
    seed                                 Seed the MongoDB database with initial data
    notify <message>                     Send a real-time notification to WebSocket clients
    list-budgets                         List all budgets in the database
    add-task <description>               Add a new task to the task queue
    list-orders                          List all orders in the database
    add-order <customerId> <amount>      Add a new order
    list-customers                       List all customers in the database
    add-customer <name> <email> [phone]  Add a new customer
    search-expenses <query>              Search for expenses using a query
    graphql-query <query>                Perform a GraphQL query
    help [command]                       display help for command
  
  Examples:
    $ budget-manager seed
    $ budget-manager notify "Hello World!"
    $ budget-manager list-budgets
    $ budget-manager add-task "New Task Description"
  1. View version information:
    budget-manager --version
    

The CLI provides a simple way to interact with the backend API and perform various operations. It can be used for testing, debugging, and managing the application without a frontend interface or using the Swagger documentation.

Demo Frontend UI

The Budget Management API includes a demo frontend UI for interacting with the backend.

It gives developers an idea of how the API can be used in a real-world application. The frontend UI is built using React, Redux, and Material-UI components.

To run the frontend UI, follow these steps:

  1. Navigate to the frontend directory:
    cd frontend
    
  2. Install dependencies: (use npm install --legacy-peer-deps if you encounter peer dependency issues)
     npm install
    
  3. Start the development server:
    npm start
    
  4. Access the frontend UI at http://localhost:3001 (or whichever port is specified in the console).

Alternatively, it is also deployed live at https://budget-manage-app.vercel.app. Feel free to use the live version for testing and exploration.

For more information, refer to the Frontend README in the frontend directory to learn about the frontend UI components, features, and usage.

UI Images

Here are some screenshots of the frontend UI:

Home:

Frontend UI

Dashboard:

Frontend UI

Budgets:

Frontend UI

Expenses:

Frontend UI

Profile:

Frontend UI

Login:

Frontend UI

Register:

Frontend UI

Forgot Password:

Frontend UI

Users:

Frontend UI

Swagger Documentation

Swagger UI

GraphQL Integration

GraphiQL

query {
  budgets {
    id
    name
    limit
    createdAt
  }
}

Or:

query {
  expenses(budgetId: "64c9f8f2a73c2f001b3c68f4") {
    id
    description
    amount
    budgetId
    createdAt
  }
}

When you run these queries, you will receive a response with the requested data. GraphQL provides a flexible and efficient way to fetch and manipulate data from the backend. Here is an example:

GraphQL

NGINX Configuration

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

gRPC Integration

The Budget Management API includes support for gRPC to enable high-performance remote procedure calls.

Getting Started

  1. Start the gRPC Server: Run the following command:
    node grpcServer.js
    
  2. Use the gRPC Client: Execute the client to interact with the server:
    node grpcClient.js
    
  3. Proto File: The .proto file for defining gRPC services and messages is located in the protos directory.

That’s it! Your gRPC server and client should now be operational. πŸš€

Dockerization

The Budget Management API can be run in a Docker container for easy deployment and scaling.

You can build and run the app using Docker Compose:

docker-compose up --build

Kubernetes Deployment

The Budget Management API now supports advanced deployment strategies for production-grade deployments:

Deployment Strategies

  1. Blue-Green Deployment: Zero-downtime deployments with instant rollback capability
  2. Canary Deployment: Gradual traffic shifting for risk mitigation
  3. Rolling Deployment: Standard Kubernetes rolling updates

Quick Start

Blue-Green Deployment

# Deploy blue and green environments
kubectl apply -f kubernetes/backend-deployment-blue.yaml
kubectl apply -f kubernetes/backend-deployment-green.yaml
kubectl apply -f kubernetes/frontend-deployment-blue.yaml
kubectl apply -f kubernetes/frontend-deployment-green.yaml

# Deploy services
kubectl apply -f kubernetes/backend-service-blue-green.yaml
kubectl apply -f kubernetes/frontend-service-blue-green.yaml

# Apply production-ready configurations
kubectl apply -f kubernetes/hpa.yaml
kubectl apply -f kubernetes/pdb.yaml
kubectl apply -f kubernetes/network-policy.yaml
kubectl apply -f kubernetes/ingress.yaml
kubectl apply -f kubernetes/servicemonitor.yaml

# Switch traffic (after validation)
kubectl patch service backend-service -p '{"spec":{"selector":{"version":"green"}}}'

Canary Deployment

# Deploy stable version (90% traffic)
kubectl apply -f kubernetes/backend-deployment-canary-stable.yaml
kubectl apply -f kubernetes/frontend-deployment-canary-stable.yaml

# Deploy canary version (10% traffic)
kubectl apply -f kubernetes/backend-deployment-canary.yaml
kubectl apply -f kubernetes/frontend-deployment-canary.yaml

# Deploy services
kubectl apply -f kubernetes/backend-service-canary.yaml
kubectl apply -f kubernetes/frontend-service-canary.yaml

# Apply production-ready configurations
kubectl apply -f kubernetes/hpa.yaml
kubectl apply -f kubernetes/pdb.yaml
kubectl apply -f kubernetes/network-policy.yaml

Production-Ready Features

Documentation

For detailed deployment procedures and best practices, see:

Monitoring

Access metrics at:

View health endpoints:

Spring Boot Backends with Maven and Gradle

There is also a Spring Boot Java version of the Budget Management API available in the spring-boot directory. It is built using Maven and Gradle.

To run the Spring Boot Maven application, follow these steps:

  1. Navigate to the spring-boot directory:
    cd spring
    
  2. Build the application using Maven:
     mvn clean install
    
  3. Run the application:
     mvn spring-boot:run
    
  4. Access the Spring Boot application at http://localhost:8080.

To run the Spring Boot Gradle application, follow these steps:

  1. Navigate to the spring-boot directory:
    cd gradle
    
  2. Build the application using Gradle:
     ./gradlew build
    
  3. Run the application:
     ./gradlew bootRun
    
  4. Access the Spring Boot application at http://localhost:8080.

Dotnet Backend with C Sharp

There is also a Dotnet C# version of the Budget Management API available in the dotnet directory. It is built using ASP.NET Core.

To run the Dotnet C# application, follow these steps:

  1. Navigate to the dotnet directory:
    cd dotnet
    
  2. Build the application using the .NET CLI:
     dotnet build
    
  3. Run the application:
     dotnet run
    
  4. Access the Dotnet C# application at http://localhost:5000.

Continuous Integration and Deployment with Jenkins

The Budget Management API includes an advanced Jenkins pipeline with support for multiple deployment strategies and production-ready features.

Pipeline Features

  1. Multiple Deployment Strategies:
    • Rolling: Standard rolling updates with zero-downtime
    • Blue-Green: Instant traffic switching with zero-downtime
    • Canary: Gradual traffic shifting with configurable percentage
  2. Comprehensive Testing:
    • Unit tests with coverage reporting
    • Integration tests
    • Smoke tests post-deployment
    • Performance tests
    • Security audits
  3. Code Quality Checks:
    • ESLint for code linting
    • npm audit for security vulnerabilities
    • Dependency checking for outdated packages
  4. Docker Integration:
    • Automated Docker image building
    • Multi-stage builds for optimization
    • Push to Docker registry with versioning
    • Image tagging based on Git commit SHA
  5. Kubernetes Deployment:
    • Automated deployment to Kubernetes clusters
    • Health checks and validation
    • Automatic rollback on failure
    • Traffic switching for blue-green deployments
    • Canary analysis and promotion
  6. Monitoring and Notifications:
    • Slack notifications for build status
    • Deployment success/failure alerts
    • Real-time progress updates
    • Git tag creation for successful deployments

Pipeline Parameters

Configure the pipeline with these parameters:

Pipeline Stages

  1. Checkout: Clone repository and extract commit information
  2. Environment Setup: Configure Node.js environment
  3. Install Dependencies: Install npm packages for backend and frontend
  4. Code Quality & Security Checks: Run linting, security audit, and dependency checks in parallel
  5. Run Tests: Execute unit and integration tests in parallel
  6. Build: Build backend and frontend applications in parallel
  7. Build Docker Images: Create and push Docker images to registry
  8. Deploy: Execute chosen deployment strategy (rolling, blue-green, or canary)
  9. Smoke Tests: Validate deployment with smoke tests
  10. Health Check: Verify application health with automatic rollback on failure
  11. Performance Tests: Run performance validation

Example Usage

Blue-Green Deployment to Production

DEPLOYMENT_STRATEGY: blue-green
ENVIRONMENT: production
RUN_SMOKE_TESTS: true
AUTO_ROLLBACK: true

Canary Deployment with 10% Traffic

DEPLOYMENT_STRATEGY: canary
ENVIRONMENT: production
CANARY_PERCENTAGE: 10
RUN_SMOKE_TESTS: true
AUTO_ROLLBACK: true

Rolling Deployment to Staging

DEPLOYMENT_STRATEGY: rolling
ENVIRONMENT: staging
RUN_SMOKE_TESTS: true
AUTO_ROLLBACK: true

Setup Instructions

  1. Configure Credentials in Jenkins:
    • docker-registry-credentials: Docker registry username/password
    • kubeconfig-credentials: Kubernetes configuration file
    • slack-webhook: Slack webhook URL for notifications
  2. Create Pipeline Job:
    • New Item β†’ Pipeline
    • Configure parameters as described above
    • Pipeline script from SCM: Point to repository Jenkinsfile
  3. Configure Webhooks:
    • Add GitHub/GitLab webhook to trigger builds on push
    • Webhook URL: http://jenkins-server/github-webhook/

Advanced Features

Deployment Functions

The Jenkinsfile includes sophisticated deployment functions:

For detailed deployment procedures, see Deployment Guide.

GitHub Actions

The Budget Management API also includes a GitHub Actions workflow for continuous integration and deployment.

  1. Workflow Configuration: The .github/workflows/ci.yml file defines the CI/CD workflow, including steps for checking out code, installing dependencies, running tests, and building the application.
  2. Job Setup: The workflow is triggered on push and pull request events to the main branch.
  3. Automated Testing: The workflow runs npm test to ensure all tests pass before proceeding to the build or deployment stages.
  4. Deployment: The workflow can be configured to deploy the application to Render or other hosting services using GitHub Actions deployment steps.
  5. Environment Variables: Use GitHub Secrets to securely manage sensitive information like API keys and database credentials.
  6. Notifications: Configure GitHub Actions to send notifications to Slack or email on build and deployment statuses.

This setup allows for automated testing and deployment, ensuring that the application is always in a deployable state.

GitHub Actions

Testing

The Budget Management API includes unit tests for all endpoints and services.

To run the tests, use the following command:

npm test

The test results will be displayed in the console.

Watch Mode: To run tests in watch mode, which automatically re-runs tests on file changes, use:

npm test:watch

Coverage Report: To generate a code coverage report, use:

npm test:coverage

Mocha and Chai tests: In addition to the Jest tests, the project also includes Mocha and Chai tests for the application. These tests can be run using:

npm run test:mocha

Contributing

Contributions are welcome! Please fork the repository, create a feature branch, and submit a pull request:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make changes and commit them (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new pull request. We will review your changes and merge them if they look good.

Author

This project is built with ❀️ by:

For more information about me, you can visit my personal website or connect with me on LinkedIn:

Feel free to reach out if you have any questions or feedback! πŸš€


Thank you for using the Budget Management API. For questions, feedback, or support, please open an issue or contact me directly.

Created with ❀️ by Son Nguyen in 2024. All rights reserved.


⬆️ Back to Top