This project is a NestJS-based application that provides a currency conversion API. It includes features such as caching using Redis, security measures (CSRF tokens), and locale-based currency formatting.
- Prerequisites
- Setup and Run with Docker
- Manual Setup and Run without Docker
- Deployment to production
- InfluxDB and Grafana Integration
- API Endpoints
- Documentation
Before starting, ensure that you have the following installed on your machine:
- Node.js(v20 or higher)
- npm (v7 or higher))
- Docker if you plan to run the application with Docker
- Redis (if you plan to run the application without Docker)
$ git clone <repository-url>
$ cd currency-converter
docker-compose up --build
This command will:
- Build the Docker images for the application.
- Start the application along with Redis in separate containers.
The application will be accessible at http://localhost:3000.
$ npm run test
$ npm run test:watch file_name
$ git clone <repository-url>
$ cd currency-converter
$ npm install
Follow the official Redis installation guide to install Redis on your machine.
$ docker run -p 6379:6379 --name redis -d redis
Create a .env file in the root of the project and configure it as follows:
# Application Settings
PORT=
# Redis Configuration
REDIS_HOST=
REDIS_PORT=
REDIS_PASSWORD=
# InfluxDB Configuration
INFLUXDB_ADMIN_USER=
INFLUXDB_ADMIN_PASSWORD=
INFLUXDB_DB=
INFLUX_ORG=
INFLUXDB_BUCKET=
INFLUX_URL=
INFLUX_TOKEN=
# SWOP Configuration
SWOP_API_KEY=
SWOP_API_ENDPOINT=
#LOCALE Configuration
LOCALE_API_ENDPOINT=https://restcountries.com/v3.1/alpha
$ npm run start:dev
$ npm run test
$ npm run test:watch file_name
To deploy your NestJS application in a production environment, we'll walk through the steps for building a Docker image, pushing it to a container registry (Docker Hub or Amazon ECR), and deploying it on AWS ECS (Elastic Container Service). Below is a detailed guide for each step.
- Navigate to Your Project Directory: Open your terminal and navigate to the root directory of your project.
cd currency-converter
- Build the Docker Image: Run the following command to build the Docker image for your application. Replace your-app-name with a suitable name for your application.
docker build -t your-app-name .
- Verify the Image: Once the image is built, you can verify it using the following command:
docker images
You should see your image listed with the name your-app-name.
- Create a Docker Hub Account: If you don't have a Docker Hub account, create one at hub.docker.com.
- Login to Docker Hub: Log in to your Docker Hub account from the command line:
docker login
You'll be prompted to enter your Docker Hub username and password.
- Tag Your Docker Image: Before pushing the image to Docker Hub, tag it with your Docker Hub repository name. Replace your-username with your Docker Hub username and your-app-name with your app name. Example name given below:
docker tag your-app-name your-username/your-app-name:latest
- Push the Image to Docker Hub: Push the tagged image to your Docker Hub repository:
docker push your-username/your-app-name:latest
You can now view your image in your Docker Hub account with details.
-
Create an ECR Repository: Go to the AWS Management Console, navigate to the ECR service, and create a new repository.
-
Authenticate Docker to ECR: Run the following command to authenticate Docker to your ECR registry. Replace aws-region with your AWS region.
aws ecr get-login-password --region aws-region | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<aws-region>.amazonaws.com
- Tag Your Docker Image for ECR: Tag the image for ECR, replacing
<aws_account_id>
and<aws-region>
with your actual account ID and region.
docker tag your-app-name:latest <aws_account_id>.dkr.ecr.<aws-region>.amazonaws.com/your-app-name:latest
- Push the Image to ECR: Push the image to your ECR repository:
docker push <aws_account_id>.dkr.ecr.<aws-region>.amazonaws.com/your-app-name:latest
-
Set Up ECS Cluster: In the AWS Management Console, go to the ECS service and create a new ECS cluster. You can choose between Fargate (serverless) or EC2 (managed) launch types. For simplicity, let's use Fargate.
-
Create an ECS Task Definition:
- Go to the ECS Task Definitions page and create a new task definition.
- Choose the Fargate launch type.
- Define the container settings:
- Image: Use the image URL from Docker Hub or ECR (e.g., your-username/your-app-name:latest or <aws_account_id>.dkr.ecr..amazonaws.com/your-app-name:latest).
- Memory and CPU: Allocate memory and CPU according to your application's needs.
- Port Mappings: Map port 3000 on the container to port 80 (or another port) on the host if you'd like to expose the application.
- Create an ECS Service:
- Go to the ECS Services section and create a new service within your cluster.
- Choose your task definition and Fargate as the launch type.
- Set the desired number of tasks (e.g., 1 for a single instance).
- Configure networking by selecting your VPC, subnets, and security groups. Make sure to allow inbound traffic on the port you've exposed (e.g., port 80).
- Deploy the Service:
- Once the service is created, ECS will automatically deploy the container using the image from Docker Hub or ECR.
- You can monitor the deployment status in the ECS dashboard.
- Access Your Application: After deployment, ECS will assign a public IP address or DNS name to your running task. You can access your application using this public endpoint (e.g.,
http://<public-ip>:80
).
- ECS Scaling: You can configure auto-scaling for your ECS service to adjust the number of running tasks based on CPU/memory usage or other metrics.
- CloudWatch Logs: Monitor your application logs in CloudWatch by configuring your task definition to forward logs to CloudWatch.
- Health Checks: Set up health checks in your ECS service to ensure that your containers are healthy and automatically replaced if they fail.
This project includes logging for currency conversions using InfluxDB, and you can monitor the data with Grafana dashboards. Below are the instructions to set up InfluxDB and Grafana.
InfluxDB is used to log the currency conversion events. The service will write the data into a specified InfluxDB bucket.
Ensure the following environment variables are configured in your .env
file:
# InfluxDB Configuration
INFLUXDB_ADMIN_USER=<your-influxdb-admin-user>
INFLUXDB_ADMIN_PASSWORD=<your-influxdb-admin-password>
INFLUXDB_DB=currencydb
INFLUX_ORG=<your-organization>
INFLUXDB_BUCKET=currencydb
INFLUX_URL=http://influxdb:8086
INFLUX_TOKEN=<your-influxdb-token>
These variables will allow the application to connect to your InfluxDB instance and log data for monitoring purposes.
Grafana is used to visualize and monitor the data logged in InfluxDB.
Run Grafana with Docker:
- If you are using Docker Compose, you can add the following service definition to your
docker-compose.yml
file to start Grafana:
grafana:
image: grafana/grafana:latest
ports:
- "3002:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
- grafana-storage:/var/lib/grafana
This will start Grafana on port 3002. You can access it at http://localhost:3002.
- Add InfluxDB as a Data Source in Grafana:
- Log in to the Grafana dashboard (default credentials: admin/admin).
- Navigate to Configuration (gear icon) > Data Sources > Add Data Source.
- Select InfluxDB.
- Configure the following settings:
- URL:
http://influxdb:8086
- Token: Use the token you set in the
.env
file. - Organization:
<your-organization>
- Bucket:
currencydb
- URL:
- Set the query language to Flux.
- Create a Dashboard:
-
After adding the InfluxDB data source, you can create a new dashboard by navigating to Create > Dashboard.
-
Add a new panel and write a Flux query to visualize the data. Here's an example query to inspect currency conversion data:
from(bucket: "currencydb")
|> range(start: -1h)
|> filter(fn: (r) => r["_measurement"] == "currency_conversion")
|> filter(fn: (r) => r["_field"] == "amount")
|> aggregateWindow(every: 1m, fn: mean)
|> yield(name: "mean_amount")
- Select your preferred visualization type (e.g., graph, table) and customize the panel settings as needed.
- Save the Dashboard:
- After creating the dashboard, save it to monitor currency conversion logs in real-time.
With these steps, you will have InfluxDB collecting logs and Grafana displaying them on a dashboard for real-time monitoring and analytics.
This section will guide users through the setup of InfluxDB for logging currency conversions and using Grafana to visualize the data. Adjust the placeholders with your actual configuration details.
- Endpoint: /currency/convert
- Method: GET
- Parameters:
- sourceCurrency: The currency code you want to convert from (e.g., USD).
- targetCurrency: The currency code you want to convert to (e.g., EUR).
- amount: The amount of sourceCurrency to convert.
- Example:
curl "http://localhost:3000/currency/convert?sourceCurrency=USD&targetCurrency=EUR&amount=100"
- Endpoint: /currency/csrf-token
- Method: POST
- Description: This endpoint generates a CSRF token that must be included in subsequent API requests.
http://localhost:3000/api
If you encounter any issues:
- Ensure that Redis is running and accessible at the configured host and port.
- Verify that all environment variables are correctly set in the .env file.
- Check Docker logs using docker-compose logs for detailed error messages.
- Before commiting any code make sure you write all test cases otherwise husky pre-commit will stop your code for commit if any test suite fails