The project is a modern web application employing microservices architecture with TypeScript, Node.js, React, and Next.js. Docker and Kubernetes facilitate efficient containerization and orchestration, ensuring scalability. Core functionalities encompass user authentication, ticket management, order processing, and secure payment transactions via PayPal APIs. The Expiration Service handles background tasks, utilizing Bull and Redis for job queue management. A responsive Next.js-based Client Service provides an intuitive front-end experience. MongoDB serves as a NoSQL database for multiple microservices, fostering data consistency. NATS Streaming Server enables seamless event-driven communication between microservices. The project's modular structure enhances maintainability, flexibility, and easy expansion. Overall, the combination of cutting-edge technologies and a microservices approach empowers a robust and adaptable web application.
To manage all the Docker containers inside the Kubernetes cluster and simplify development workflow the project uses Skaffold.
To run the app in development environment, make sure Docker, Kubernetes and Skaffold are installed on your local machine.
Before running the app environment variables inside the Kubernetes cluster must be set. Execute commands below to set these environment variables:
# kubectl create secret generic paypal-secret --from-literal=PAYPAL_CLIENT_ID=<your_paypal_key>
# kubectl create secret generic paypal-secret2 --from-literal=PAYPAL_CLIENT_SECRET=<your_paypal_secret_key>
# kubectl create secret generic jwt-secret --from-literal=JWT_KEY=<your_jwt_key>
Be sure to expose the ingress-nginx-controller with:
# kubectl expose deployment ingress-nginx-contoller --target-port=80 --type=NodePort -n kube-systemStart the app with skaffold dev.
-
To redirect requests coming to: ticketing.dev => localhost
-
only for local development purposes
-
MacOS/Linux:
modify/etc/hostsfile to include127.0.0.1 ticketing.dev -
Windows:
modifyC:\Windows\System32\Drivers\etc\hostsfile to include127.0.0.1 ticketing.dev -
To skip the unskippable HTTPS warning in Chrome:
try type thisisunsafe
The Kubernetes infrastructure for the application consists of various deployment files (YAML) that define the configuration for deploying microservices, databases, and other components. This setup enables the orchestration and management of containers in a Kubernetes cluster.
- Deployment configuration for the authentication service.
- Deployment configuration for the MongoDB database used by the authentication service.
- Deployment configuration for the Next.js-based client service.
- Deployment configuration for the expiration service.
- Deployment configuration for the Redis instance used by the expiration service.
- Configuration for the Ingress service, enabling external access to the deployed microservices.
- Deployment configuration for the NATS Streaming Server, used for event-driven communication between microservices.
- Deployment configuration for the orders service.
- Deployment configuration for the MongoDB database used by the orders service.
- Deployment configuration for the payments service.
- Deployment configuration for the MongoDB database used by the payments service.
- Deployment configuration for the tickets service.
- Deployment configuration for the MongoDB database used by the tickets service.
-
Apply the Kubernetes deployment files using the
kubectl applycommand. For example:kubectl apply -f auth-depl.yaml
All the commonly used classes, interfaces and middlewares, etc. are extracted into a published NPM Module.
-
@m-ticketing/common: See the codeContains commonly used Middlewares and Error Classes for ticketing microservices
The Client Service is a front-end microservice developed using Next.js, a React framework for building web applications. This service focuses on providing a user-friendly and interactive interface for the application. It communicates with the back-end microservices to fetch and display data, handle user authentication, and facilitate seamless interactions.
- Ensure that the
NEXT_PUBLIC_PAYPAL_CLIENT_IDandReCAPTCHA_SITEKEYenvironment variables are defined.
The Auth Service is a microservice within the larger application responsible for handling user authentication-related functionalities. It employs TypeScript for strong typing, Node.js with Express.js for building the server and MongoDB for storing user data. The service utilizes JSON Web Tokens (JWT) for secure and stateless authentication, providing a scalable solution in a microservices architecture. It includes various routes for user sign-up, sign-in, sign-out, and other authentication-related operations.
-
src: The source code directory containing the main application logic.
- models: Defines the TypeScript data model for user entities (
user.ts). - routes: Houses the route handlers for different authentication operations.
- test: Contains test files for route handlers.
current-user.test.ts: Tests for the current user route.signin.test.ts: Tests for the sign-in route.signout.test.ts: Tests for the sign-out route.signup.test.ts: Tests for the sign-up route.
change-password.ts: Route handler for changing user passwords.current-user.ts: Route handler for retrieving current user information.googleOauth.ts: Route handler for Google OAuth authentication.signin.ts: Route handler for user sign-in.signout.ts: Route handler for user sign-out.signup.ts: Route handler for user sign-up.update.ts: Route handler for updating user information.
- test: Contains test files for route handlers.
- services: Contains service logic for password-related operations.
passwords.ts: Service for managing password-related functionality.
- test: Houses common test setup configurations (
setup.ts). app.ts: Entry point for the Express.js application.index.ts: Main file to start the server.
- models: Defines the TypeScript data model for user entities (
-
.dockerignore: Specifies files and directories to be excluded from Docker builds.
-
Dockerfile: Defines instructions for building a Docker image for the Auth Service.
-
package-lock.json: Auto-generated file for package version tracking.
-
package.json: Configuration file that includes project metadata, dependencies, and scripts.
- Path:
/api/users/signup - Method:
POST - Description: Allows users to create a new account. It handles
google-recaptcha.
- Path:
/api/users/signin - Method:
POST - Description: Allows users to sign in to their accounts.
- Path:
/api/users/signout - Method:
POST - Description: Allows users to sign out of their accounts.
- Path:
/api/users/currentUser - Method:
GET - Description: Retrieves information about the currently authenticated user.
- Path:
/api/users/changePassword - Method:
POST - Description: Allows users to change their account password.
- Path:
/api/users/requestOauth-google - Method:
POST - Description: Handles Google OAuth authentication.
- Path:
/api/users/update - Method:
PATCH - Description: Updates user information including uploading a photo to the cloud.
The Auth Service provides a comprehensive set of routes to manage user authentication and authorization. These routes cover user registration, sign-in, sign-out, profile retrieval, password change, Google OAuth integration, and user information updates. The service is structured to be modular, making it easy to maintain and extend its functionality.
The Orders Service is a microservice designed to manage the lifecycle of orders within the larger application. It is implemented using TypeScript and Node.js with Express.js. The service incorporates event-driven programming with the help of NATS Streaming Server for handling events related to order creation, cancellation, and payment processing. Below is a detailed overview of the project structure, key files, and the exposed routes.
-
src: The source code directory containing the main application logic.
- mocks: Mock files for testing purposes.
nats-wrapper.ts: Mock implementation for NATS Streaming Server wrapper.
- events: Contains event-related logic.
- listeners: Event listeners for different events.
- test: Test files for event listeners.
expiration-completed-listener.ts: Tests for the expiration completed event listener.payment-created-listener.ts: Tests for the payment created event listener.ticket-created-listener.ts: Tests for the ticket created event listener.ticket-updated-listener.ts: Tests for the ticket updated event listener.
queue-group-name.ts: Defines the queue group name for event listeners.expiration-completed-listener.ts: Event listener for the expiration completed event.payment-created-listener.ts: Event listener for the payment created event.ticket-created-listener.ts: Event listener for the ticket created event.ticket-updated-listener.ts: Event listener for the ticket updated event.
- test: Test files for event listeners.
- publishers: Event publishers for different events.
order-cancelled-publisher.ts: Event publisher for the order cancelled event.order-created-publisher.ts: Event publisher for the order created event.
- listeners: Event listeners for different events.
- models: Defines TypeScript data models for orders and tickets.
order.ts: Model for order entities.ticket.ts: Model for ticket entities.
- routes: Houses the route handlers for order-related operations.
- test: Contains test files for route handlers.
delete.test.ts: Tests for the delete order route.index.test.ts: Tests for the index route.new.test.ts: Tests for the new order route.show.test.ts: Tests for the show order route.
delete.ts: Route handler for deleting an order.index.ts: Route handler for retrieving all orders.new.ts: Route handler for creating a new order.show.ts: Route handler for retrieving a specific order.
- test: Contains test files for route handlers.
- test: Houses common test setup configurations (
setup.ts). app.ts: Entry point for the Express.js application.index.ts: Main file to start the server.nats-wrapper.ts: Wrapper for connecting to NATS Streaming Server.
- mocks: Mock files for testing purposes.
-
.dockerignore: Specifies files and directories to be excluded from Docker builds.
-
Dockerfile: Defines instructions for building a Docker image for the Orders Service.
-
package-lock.json: Auto-generated file for package version tracking.
-
package.json: Configuration file that includes project metadata, dependencies, and scripts.
-
tsconfig.json: TypeScript configuration file.
- Path:
/api/orders - Method:
POST - Description: Creates a new order for purchasing a specified ticket.
- Path:
/api/orders/:orderId - Method:
GET - Description: Retrieves details about a specific order.
- Path:
/api/orders/:orderId - Method:
DELETE - Description: Cancels a specific order.
- Path:
/api/orders - Method:
GET - Description: Retrieves all active orders for the given user making the request.
- File:
order-cancelled-publisher.ts - Description: Publishes an event when an order is cancelled.
- File:
order-created-publisher.ts - Description: Publishes an event when a new order is created.
- File:
expiration-completed-listener.ts - Description: Listens for the expiration completed event.
- File:
payment-created-listener.ts - Description: Listens for the payment created event.
- File:
ticket-created-listener.ts - Description: Listens for the ticket created event.
- File:
ticket-updated-listener.ts - Description: Listens for the ticket updated event.
The Orders Service provides routes to manage the lifecycle of orders, including creating new orders, retrieving order details, canceling orders, and fetching all active orders for a user. The service is structured to handle events related to order creation, cancellation, and payment processing in an event-driven architecture.
The Tickets Service is a microservice responsible for managing ticket-related operations within the larger application. It utilizes TypeScript and Node.js with Express.js for server-side logic and NATS Streaming Server for event-driven communication. The service includes routes for creating, updating, and retrieving ticket information. Additionally, it implements event publishers and listeners to communicate with other microservices.
-
src: The source code directory containing the main application logic.
- mocks: Mock files for testing purposes.
nats-wrapper.ts: Mock implementation for NATS Streaming Server wrapper.
- events: Contains event-related logic.
- listeners: Event listeners for different events.
- test: Test files for event listeners.
order-cancelled-listener.test.ts: Tests for the order cancelled event listener.order-created-listener.test.ts: Tests for the order created event listener.
order-cancelled-listener.ts: Event listener for the order cancelled event.order-created-listener.ts: Event listener for the order created event.queue-group-name.ts: Defines the queue group name for event listeners.
- test: Test files for event listeners.
- publishers: Event publishers for different events.
ticket-created-publisher.ts: Event publisher for the ticket created event.ticket-updated-publisher.ts: Event publisher for the ticket updated event.
- listeners: Event listeners for different events.
- models: Defines TypeScript data models for tickets.
- test: Test files for the ticket model.
ticket.test.ts: Tests for the ticket model.
ticket.ts: Model for ticket entities.
- test: Test files for the ticket model.
- routes: Houses the route handlers for ticket-related operations.
- test: Contains test files for route handlers.
index.test.ts: Tests for the index route.new.test.ts: Tests for the new ticket route.show.test.ts: Tests for the show ticket route.update.test.ts: Tests for the update ticket route.
index.ts: Route handler for retrieving all tickets.new.ts: Route handler for creating a new ticket.show.ts: Route handler for retrieving a specific ticket.update.ts: Route handler for updating a ticket.
- test: Contains test files for route handlers.
- test: Houses common test setup configurations (
setup.ts). app.ts: Entry point for the Express.js application.index.ts: Main file to start the server.nats-wrapper.ts: Wrapper for connecting to NATS Streaming Server.
- mocks: Mock files for testing purposes.
-
.dockerignore: Specifies files and directories to be excluded from Docker builds.
-
Dockerfile: Defines instructions for building a Docker image for the Tickets Service.
-
package-lock.json: Auto-generated file for package version tracking.
-
package.json: Configuration file that includes project metadata, dependencies, and scripts.
-
tsconfig.json: TypeScript configuration file.
- Path:
/api/tickets/new - Method:
POST - Description: Creates a new ticket.
- Request Body:
{ "title": "Ticket Title", "price": "Ticket Price" }
- Path:
/api/tickets/:id - Method:
GET - Description: Retrieves details about a specific ticket.
- Path:
/api/tickets/:id - Method:
PUT - Description: Updates information about a specific ticket.
- Request Body:
{ "title": "New Title", "price": "New Price" }
- Path:
/api/tickets - Method:
GET - Description: Retrieves all tickets.
- File:
ticket-created-publisher.ts - Description: Publishes an event when a new ticket is created.
- File:
ticket-updated-publisher.ts - Description: Publishes an event when a ticket is updated.
- File:
order-cancelled-listener.ts - Description: Listens for the order cancelled event.
- File:
order-created-listener.ts - Description: Listens for the order created event.
The Tickets Service provides routes to manage the creation, updating, and retrieval of tickets. It also implements event publishers and listeners to communicate with other microservices, ensuring a seamless flow of information within the application.
The Payments Service is a microservice responsible for handling payment-related operations within the larger application. It utilizes TypeScript and Node.js with Express.js for server-side logic, and it integrates with PayPal for processing payments. The service includes routes for creating and capturing orders, and it implements event publishers and listeners for seamless communication with other microservices.
-
src: The source code directory containing the main application logic.
- mocks: Mock files for testing purposes.
nats-wrapper.ts: Mock implementation for NATS Streaming Server wrapper.
- events: Contains event-related logic.
- listeners: Event listeners for different events.
order-cancelled-event.ts: Listener for the order cancelled event.order-created-listener.ts: Listener for the order created event.queue-group-name.ts: Defines the queue group name for event listeners.
- publishers: Event publishers for different events.
payment-created-publisher.ts: Publisher for the payment created event.
- listeners: Event listeners for different events.
- models: Defines TypeScript data models for orders and payments.
order.ts: Model for order entities.payment.ts: Model for payment entities.
- routes: Houses the route handlers for payment-related operations.
- test: Contains test files for route handlers.
new.test.ts: Tests for the new payment route.paypal.test.ts: Tests for the PayPal route.
new.ts: Route handler for creating a new payment.paypal.ts: Route handler for interacting with PayPal.
- test: Contains test files for route handlers.
- test: Houses common test setup configurations (
setup.ts). - utils: Contains utility functions for working with PayPal.
paypal.ts: Utility functions for PayPal integration.
app.ts: Entry point for the Express.js application.index.ts: Main file to start the server.nats-wrapper.ts: Wrapper for connecting to NATS Streaming Server.
- mocks: Mock files for testing purposes.
-
.dockerignore: Specifies files and directories to be excluded from Docker builds.
-
Dockerfile: Defines instructions for building a Docker image for the Payments Service.
-
package-lock.json: Auto-generated file for package version tracking.
-
package.json: Configuration file that includes project metadata, dependencies, and scripts.
-
tsconfig.json: TypeScript configuration file.
The Payments Service integrates with PayPal to process payments securely. Below is a detailed explanation of the PayPal integration workflow, functions, and routes:
-
Path:
/api/payments/create-order -
Method:
POST -
Description: Creates a new order for payment.
-
Request Body:
{ "price": "Amount" }- Workflow:
- The client sends a POST request with the desired
priceto create a new payment order. - The server validates the request, ensuring the
priceis not empty. - The server generates an access token by calling
generateAccessTokenutility function. - Using the access token, the server makes a request to the PayPal API (
/v2/checkout/orders) to create a new order. - The PayPal API responds with the order details.
- The server returns the order details to the client.
- The client sends a POST request with the desired
- Workflow:
-
Path:
/api/payments/:paypalId/capture -
Method:
POST -
Description: Captures the payment for a specific order.
-
Params:
paypalId- The PayPal Order ID.- Workflow:
- The client sends a POST request with the
paypalIdto capture the payment for a specific order. - The server makes a request to the PayPal API (
/v2/checkout/orders/:paypalId/capture) to capture the payment. - The PayPal API responds with the captured payment details.
- The server returns the captured payment details to the client.
- The client sends a POST request with the
- Workflow:
- Path:
/api/payments - Method:
POST - Description: Create new payment.
-
Description: Generates an access token for making requests to the PayPal API.
- Workflow:
- The function constructs the basic authorization header using PayPal client credentials.
- The function makes a request to the PayPal API (
/v1/oauth2/token) to obtain an access token. - The function returns the access token.
- Workflow:
-
Description: Creates a new order with the specified price using the PayPal API.
- Workflow:
- The function generates an access token by calling
generateAccessToken. - The function constructs the payload for creating a new order.
- The function makes a request to the PayPal API (
/v2/checkout/orders) to create a new order. - The function returns the order details.
- The function generates an access token by calling
- Workflow:
-
Description: Captures the payment for a specific order using the PayPal API.
- Workflow:
- The function generates an access token by calling
generateAccessToken. - The function makes a request to the PayPal API (
/v2/checkout/orders/:paypalId/capture) to capture the payment. - The function returns the captured payment details.
- The function generates an access token by calling
- Workflow:
- File:
order-cancelled-event.ts - Description: Listens for the order cancelled event.
- File:
order-created-listener.ts - Description: Listens for the order created event.
- File:
payment-created-publisher.ts - Description: Publishes an event when a payment is created.
-
The event listeners and publishers enable seamless communication between the Payments Service and other microservices, ensuring a coordinated flow of information.
-
Ensure that NATS Streaming Server is properly configured and accessible for effective event handling.
The Payments Service facilitates payment processing by integrating with PayPal. It includes routes for creating and capturing orders, event listeners for handling order-related events, and event publishers for notifying other microservices about payment creation.
The Expiration Service is a microservice dedicated to handling the expiration of orders within the larger application. It utilizes TypeScript and Node.js with Bull for queue management and integrates with NATS Streaming Server for event-driven communication. The service includes event listeners, publishers, and a dedicated expiration queue to process background tasks efficiently.
-
src: The source code directory containing the main application logic.
- mocks: Mock files for testing purposes.
nats-wrapper.ts: Mock implementation for NATS Streaming Server wrapper.
- events: Contains event-related logic.
- listeners: Event listeners for different events.
order-created-listener.ts: Listener for the order created event.queue-group-name.ts: Defines the queue group name for event listeners.
- publishers: Event publishers for different events.
expiration-completed-publisher.ts: Publisher for the expiration completed event.
- listeners: Event listeners for different events.
- queues: Manages background job processing.
expiration-queue.ts: Handles tasks related to order expiration using Bull.
index.ts: Main file to start the Expiration Service.nats-wrapper.ts: Wrapper for connecting to NATS Streaming Server.
- mocks: Mock files for testing purposes.
-
.dockerignore: Specifies files and directories to be excluded from Docker builds.
-
Dockerfile: Defines instructions for building a Docker image for the Expiration Service.
-
package-lock.json: Auto-generated file for package version tracking.
-
package.json: Configuration file that includes project metadata, dependencies, and scripts.
-
tsconfig.json: TypeScript configuration file.
- File:
order-created-listener.ts - Description: Listens for the order created event.
- File:
queue-group-name.ts - Description: Defines the queue group name for event listeners.
- File:
expiration-completed-publisher.ts - Description: Publishes an event when the order expiration process is completed.
-
File:
expiration-queue.ts -
Description: Manages background job processing related to order expiration using Bull.
-
Initialization:
- The queue is created using the
Queueclass from Bull. - It is named
'order:expiration'to identify and process expiration-related tasks. - Configuration includes connecting to a
Redisinstance specified byREDIS_HOSTenvironment variable.
- The queue is created using the
-
Task Processing:
- The queue is set to process tasks asynchronously.
- Tasks are added to the queue when an order is created.
- The processing logic handles the completion of the order expiration process and triggers the
Expiration Completed Publisherto notify other microservices.
-
-
The Expiration Service plays a crucial role in managing the expiration of orders within the application.
-
Event listeners and publishers contribute to the event-driven architecture, ensuring seamless communication between microservices.
-
The expiration queue efficiently handles background tasks asynchronously, improving overall system responsiveness.
-
Redis is utilized as the storage backend for Bull queues, providing durability and reliability for managing tasks.
-
Ensure that the
REDIS_HOSTenvironment variable is correctly configured to connect to the Redis instance.
The Expiration Service enhances the application's architecture by handling order expiration through event-driven processing and efficient background task management.














