This is a simple REST API built as coding challenge made for TrueNorth.
See the requirement's list.
CircleCI status:
Coverage
Stack
- Node.js
- TypeScript
- MongoDB and Mongo Atlas as Database
- Redis and Redis Labs as Cache layer
- Jest
- AWS lambda
- Eslint
- Docker
- serverless and serverless-offline
Preparing the development environment using your current NodeJS installation
You need to install the project first:
Before running the API, you must have Mongo and Redis installed first.
You can manually install them, or, you can use docker to install it by running:
npm run docker:compose:daemon
Testing using local Mongo and Redis services
You probably want to run the current test suite implementation:
Running the application on local environment
It is recommended to set the NODE_ENV env var value to 'dev'
Add some users and operations to the database:
An then you can start the dev environment by running:
You can do login as admin user through the API by using curl:
curl -X POST http://localhost:3000/dev/users/login -H "Content-Type: application/json" -d '{"username": "admin@admin.com", "password": "123456"}'
Managing Mongo and Redis using Docker
npm run docker:compose:daemon
Stop Docker environment
Restart Docker environment
npm run docker:restart npm run docker:clean
Clean Docker environment
Testing on CI
When running on CI, the API connects to Mongo Atlas and Redis Labs.
Please make sure you have the proper accounts.
You can create for free, both accounts at https://redis.com/try-free/ and https://www.mongodb.com/cloud/atlas/register .
Setup the following env vars on CI platform:
NODE_ENV="ci" CODECOV_TOKEN="xxxxxxx" # for codecov service TOKEN_KEY="mysecret" # for jwt token REDIS_HOST_LABS="redis-16050.c10.us-east-1-2.ec2.cloud.redislabs.com" REDIS_PASSWORD_LABS="........" REDIS_PORT_LABS=16050 MONGO_ATLAS_URL="mongodb+srv://username:password@cluster0.mie7dav.mongodb.net/?retryWrites=true&w=majority" MONGO_DATABASE="arithmetic_calculator_api"
Run the default npm test command to run the test suite for CI.
Deploy
Environment Config
By default the application will assume the following stage names and config files:
- test
- prod
- stg
Each one of those will required a related config file:
- ./config/.env.test
- ./config/.env.prod
- ./config/.env.stg
Use the following template:
TOKEN_KEY=mysecret REDIS_HOST_LABS="redis-16050.c10.us-east-1-2.ec2.cloud.redislabs.com" REDIS_PASSWORD_LABS="password" REDIS_PORT_LABS=16050 MONGO_ATLAS_URL="mongodb+srv://username:password@cluster0.mie7dav.mongodb.net/?retryWrites=true&w=majority" MONGO_DATABASE="arithmetic_calculator_api"
Deploy on AWS, simply run
$ npm run deploy
# or
$ serverless deployUsage
send an HTTP request directly to the endpoint using a tool like curl
curl https://je6x0x8fa6.execute-api.us-east-2.amazonaws.com/test/users
Scaling
By default, AWS Lambda limits the total concurrent executions across all functions within a given region to 1000. The default limit is a safety limit that protects you from costs due to potential runaway or recursive functions during initial development and testing. To increase this limit above the default, follow the steps in To request a limit increase for concurrent executions.
Project Tech Debits
- Improve code coverage
- Implement test suite for unit testing.
- Implement test suite for e2e testing.
- Adopt the Data Repository Pattern.
- 100% Ensure the SOLID principles.
- Implement API documentation - swagger OAS.
- APM with datadog - custom error classes in 2 levels: service and controller