Welcome to the Budget Management API, a robust backend application designed to manage budgets, expenses, users, orders, notifications, and more. This API integrates cutting-edge technologies and supports advanced features like gRPC, GraphQL, WebSockets, Elasticsearch, PostgreSQL, MySQL, Redis, RabbitMQ, Kafka, and Docker.
Moreover, it also includes a CLI tool for interacting with the backend and a comprehensive Swagger documentation for testing and exploring the API.
Below is a comprehensive guide to setting up, running, and utilizing this API.
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!
The Budget Management API is deployed live at https://budget-management-backend-api.onrender.com.
Additionally, the 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!
Note: 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!
Backup Frontend: https://budget-management-system.netlify.app
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. |
Budget-Management-Backend-API/
βββ .env # Environment variables configuration
βββ .env.example # Example environment configuration file
βββ .gitignore # Git ignore file
βββ .prettierrc # Prettier configuration for code formatting
βββ LICENSE # License information
βββ README.md # Documentation for the project
βββ app.test.js # Main test file for application
βββ cli.js # CLI tool for interacting with the backend
βββ docker-compose.yml # Docker Compose configuration
βββ Dockerfile # Dockerfile for containerizing the application
βββ grpcServer.js # gRPC server implementation
βββ index.js # Main entry point for the application
βββ nodemon.json # Nodemon configuration file
βββ openapi.yaml # OpenAPI specification for the API
βββ package.json # NPM package configuration file
βββ start.sh # Script to start the application
βββ prometheus.yml # Prometheus configuration for monitoring
βββ redis-mongo-flow/ # Directory for Redis-Mongo integration flow
β βββ app.js # Express app for Redis-Mongo flow
β βββ config.js # Configuration for Redis-Mongo flow
β βββ package.json # NPM configuration for this module
β βββ README.md # Documentation specific to Redis-Mongo flow
β βββ seed.js # Data seeder for Redis-Mongo flow
β βββ test.js # Test file for Redis-Mongo flow
βββ round-robin/ # Directory for round-robin load balancer
β βββ config.js # Configuration for round-robin implementation
β βββ index.js # Main entry point for round-robin logic
β βββ README.md # Documentation for round-robin functionality
βββ proto/ # Protocol Buffers directory
β βββ budget.proto # gRPC proto file for budgets
βββ nginx/ # NGINX configuration directory
β βββ docker-compose.yml # Docker Compose for NGINX
β βββ Dockerfile # Dockerfile for NGINX
β βββ nginx.conf # NGINX configuration file
β βββ start_nginx.sh # Script to start NGINX
β βββ README.md # Documentation for NGINX
βββ docs/ # Documentation directory
β βββ swaggerConfig.js # Swagger configuration for API docs
βββ graphql/ # GraphQL-related files
β βββ schema.js # GraphQL schema definition
βββ services/ # Services and utilities
β βββ dataSeeder.js # Seeder for MongoDB
β βββ elasticService.js # Elasticsearch client and utility functions
β βββ jwtService.js # JSON Web Token (JWT) utilities
β βββ postgresService.js # PostgreSQL client and utilities
β βββ rabbitMQService.js # RabbitMQ client and utilities
β βββ redisService.js # Redis client and utilities
β βββ websocketService.js # WebSocket server and utilities
βββ controllers/ # Route controllers for the API
β βββ authController.js # Authentication-related endpoints
β βββ budgetController.js # Budget management endpoints
β βββ customerController.js # Customer management endpoints
β βββ expenseController.js # Expense management endpoints
β βββ notificationController.js # Notification management endpoints
β βββ orderController.js # Order management endpoints
β βββ searchController.js # Search-related endpoints
β βββ taskController.js # Task management endpoints
β βββ transactionController.js # Transaction log endpoints
β βββ userController.js # User profile management endpoints
βββ middleware/ # Middleware utilities
β βββ authMiddleware.js # JWT authentication middleware
βββ models/ # Mongoose schemas
β βββ budget.js # Schema for budgets
β βββ customer.js # Schema for customers
β βββ expense.js # Schema for expenses
β βββ order.js # Schema for orders
β βββ task.js # Schema for tasks
β βββ user.js # Schema for users
βββ routes/ # Express router files
β βββ authRoutes.js # Routes for authentication
β βββ budgetRoutes.js # Routes for budgets
β βββ customerRoutes.js # Routes for customers
β βββ expenseRoutes.js # Routes for expenses
β βββ graphqlRoutes.js # Routes for GraphQL
β βββ index.js # Main router entry point
β βββ notificationRoutes.js # Routes for notifications
β βββ orderRoutes.js # Routes for orders
β βββ searchRoutes.js # Routes for Elasticsearch
β βββ taskRoutes.js # Routes for tasks
β βββ transactionRoutes.js # Routes for transactions
β βββ userRoutes.js # Routes for user profiles
βββ views/ # Static assets and templates
β βββ android-chrome-192x192.png # Android Chrome app icon
β βββ android-chrome-512x512.png # Android Chrome high-res icon
β βββ apple-touch-icon.png # Apple Touch icon
β βββ favicon.ico # Favicon
β βββ favicon-16x16.png # 16x16 favicon
β βββ favicon-32x32.png # 32x32 favicon
β βββ home.html # HTML template for homepage
β βββ manifest.json # Web app manifest
git clone https://github.com/hoangsonww/Budget-Management-Backend-API.git
cd Budget-Management-Backend-API
npm install
.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
npm start
http://localhost:3000
http://localhost:3000/docs
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.
More endpoints and features are available in the API. Refer to the Swagger documentation for detailed information.
| Field | Type | Description |
|ββββ-|βββ-|ββββββββββ|
| username
| String | Unique username. |
| email
| String | Unique email address. |
| password
| String | Hashed password. |
| createdAt
| Date | User creation date. |
| Field | Type | Description |
|ββββ-|βββ-|ββββββββββ|
| name
| String | Budget name. |
| limit
| Number | Budget limit. |
| createdAt
| Date | Budget creation date. |
| Field | Type | Description |
|βββββ|βββ-|ββββββββββ|
| budgetId
| String | ID of the associated budget. |
| description
| String | Expense description. |
| amount
| Number | Expense amount. |
| createdAt
| Date | Expense creation date. |
| Field | Type | Description |
|βββββ|βββ-|βββββββββββ|
| customerId
| String | ID of the associated customer. |
| amount
| Number | Order amount. |
| status
| String | Order status. |
| createdAt
| Date | Order creation date. |
| Field | Type | Description |
|ββββ-|βββ-|ββββββββββ|
| name
| String | Customer name. |
| email
| String | Customer email address. |
| phone
| String | Customer phone number. |
Field | Type | Description |
---|---|---|
description |
String | Task description. |
status |
String | Task status. |
createdAt |
Date | Task creation date. |
npm start
http://localhost:3000/graphql
./api/notifications
endpoint or CLI.docker-compose up --build
/api/search/expenses
.budget-manager
CLI to add tasks.localhost:9092
.redis://localhost:6379
.postgres://user:password@localhost:5432/budget_manager
.mongodb://localhost:27017/budget_manager
.nginx
directory.kubernetes
directory.kubectl apply -f kubernetes/
prometheus.yml
.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:
+--------------------+ +------------------+
| Frontend UI | | |
| (not impl.) | | CLI Tool / gRPC |
+--------------------+ +------------------+
| |
| HTTP/GraphQL Requests | CLI Commands / gRPC Calls
| |
+--------------------+ +------------------+
| API Gateway / |<--------------->| gRPC Server |
| Express.js | +------------------+
+--------------------+
|
| RESTful API / WebSocket / GraphQL Responses
|
+--------------------+
| Application Core |
|--------------------|
| Controllers / |
| Services |
+--------------------+
| | |
+------+ | +-------------+
| | |
+----------+ +-----------+ +----------------+
| MongoDB | | PostgreSQL | | Elasticsearch |
| NoSQL DB | | Relational | | Search Engine |
+----------+ +-----------+ +----------------+
| | |
| | |
+----------+ +-------------+ +----------------+
| Redis | | RabbitMQ / | | Kafka (Event |
| Cache | | Kafka Queue | | Streaming) |
+----------+ +-------------+ +----------------+
| | |
+--------------|-(Asynchronous Tasks)---+
|
+----------------------+
| External Services |
| (Email, SMS, etc.) |
+----------------------+
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=
The budget-manager
CLI provides a convenient way to interact with the application from the command line.
Follow these steps to use the CLI:
npm link
budget-manager seed
budget-manager notify "Hello!"
budget-manager add-task "Task description"
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"
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.
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:
frontend
directory:
cd frontend
npm install --legacy-peer-deps
if you encounter peer dependency issues)
npm install
npm start
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.
Here are some screenshots of the frontend UI:
Home:
Dashboard:
Budgets:
Expenses:
Profile:
Login:
Register:
Forgot Password:
Users:
/docs
.
http://localhost:3000/api/graphql
.
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:
nginx
directory contains an Nginx configuration for reverse proxy and load balancing.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;
}
}
The Budget Management API includes support for gRPC to enable high-performance remote procedure calls.
node grpcServer.js
node grpcClient.js
.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. π
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
kubectl apply -f kubernetes/
The Budget Management API includes a Jenkins pipeline for continuous integration and deployment.
Pipeline Configuration: The Jenkinsfile
defines the CI/CD pipeline stages, including code checkout, dependency installation, testing, building, and deployment. Add it to the root of the project.
Job Setup: Create a pipeline job in Jenkins, point it to the repository, and configure it to use the Jenkinsfile
.
Automated Testing: The pipeline runs npm test
to ensure all tests pass before proceeding to the build or deployment stages.
Environment Variables: Use Jenkins environment variables to securely manage secrets like API keys and credentials for services such as MongoDB, Redis, or Render.
Deployment: The pipeline supports deploying the application using Render or directly to a server using SSH and PM2.
Webhooks: Integrate GitHub/GitLab webhooks to trigger builds automatically on code changes.
Notifications: Add Slack or email notifications in the pipeline to inform team members about build and deployment statuses.
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.
Contributions are welcome! Please fork the repository, create a feature branch, and submit a pull request:
git checkout -b feature-branch
).git commit -am 'Add new feature'
).git push origin feature-branch
).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.