FC coding challenge
- docker engine (https://docs.docker.com/engine/install/)
- docker-compose (https://docs.docker.com/compose/install/)
- Clone repo
git clone https://github.com/ilyashatalov/cache-machine.git && cd cache-machine
- To start an application you need only to run docker-compose:
docker-compose up -d mongo app
There are 3 containers (app, mongodb, mongo-express). The application will work on 3000 port that are binded to host 80 port. If you need to make some configuration of the app (TTL time, max cache items) you can change it in docker-compose.yml. Default variables are:
RANDOM_STRING_LENGTH=14
CACHE_MAX_COUNT=5
KEY_TTL=3600
LOG_LEVEL=info
- Try to curl our application (there will be one test item):
Expected output:
curl localhost/keys
[{"_id":"63b59dd0cf59a59e34a4b0f1","__v":0,"key":"Test1","updatedAt":"2023-01-04T15:40:00.825Z","value":"Value1"}]
- Add one entry nd get HTTP status code:
Expected output:
curl -w "\n%{http_code}\n" -X POST localhost/keys -d '{"key": "Key 100", "value": "Value 100"}' -H 'Content-Type: application/json'
{"key":"Key 100","value":"Value 100","_id":"63bf616fb19f977385f6febb","updatedAt":"2023-01-12T01:25:03.228Z","__v":0}% 201
- Get unexisting Entry and get HTTP status code:
Expected output:
curl -w "\n%{http_code}\n" localhost/keys/TestUnexist
{"key":"TestUnexist","value":"oHFKMWxf75f1ZK","_id":"63bf6249b19f977385f6fed1","updatedAt":"2023-01-12T01:28:41.404Z","__v":0} 201
- Run the same command to get the existing Entry and check HTTP status code:
Expected output
curl -w "\n%{http_code}\n" localhost/keys/TestUnexist
{"_id":"63bf6249b19f977385f6fed1","key":"TestUnexist","value":"oHFKMWxf75f1ZK","updatedAt":"2023-01-12T01:28:41.404Z","__v":0} 200
- Delete one Entry
Expected output
curl -X DELETE -w "\n%{http_code}\n" localhost/keys/TestUnexist
{"_id":"63bf6249b19f977385f6fed1","key":"TestUnexist","value":"oHFKMWxf75f1ZK","updatedAt":"2023-01-12T01:28:41.404Z","__v":0} 200
- Delete all keys:
Expected output
curl -X DELETE -w "\n%{http_code}\n" localhost/keys
{"acknowledged":true,"deletedCount":1} 200
- nodeJS 19.3.0
- MongoDB 6.0 or Docker and docker-compose from the previous section
You need to configure the application to use your Mongo DB installation or you can use docker-compose included in this project.
-
Install docker and docker-compose:
- docker engine (https://docs.docker.com/engine/install/)
- docker compose (https://docs.docker.com/compose/install/)
-
Clone repo
git clone https://github.com/ilyashatalov/cache-machine.git && cd cache-machine
-
And start mongo and mongo express:
docker-compose up -d mongo mongo-express
It will start MongoDB 6.0 on your machine and map 27017 port (default for MongoDB) from the container to your host and mongo express on port 8081
- Or use MongoDB installation guide: https://www.mongodb.com/docs/manual/installation/
- Clone repo (if you skip it above)
git clone https://github.com/ilyashatalov/cache-machine.git && cd cache-machine
- Locate to NodeJS APP
cd cache-machine-server
- Edit DATABASE_URL parameter of env file in root of the project (and other if you need)
If you use docker-compose from this project you can use a string like:
DATABASE_URL = mongodb://fashiontest:[email protected]/fcdb?authSource=admin
- Rename it to .env
mv env .env
- Install all dependencies
npm install
- Run application
npm run dev
- Test application with curls from the previous section with port
APP_PORT
- Or run tests
- Edit file package.json test line with your DB credentials (notice that it's better to use another mongo database here like in the example)
vim package.json
- Run tests
Sample output:npm run test
> [email protected] test > tsc && cross-env CACHE_MAX_COUNT=10 KEY_TTL=60 DATABASE_URL=mongodb://fashiontest:Omio0raiOmio0rai@localhost/fcdb-test?authSource=admin LOG_LEVEL=debug NODE_ENV=test jest --runInBand --detectOpenHandles --forceExit --testTimeout=5000 {"level":"info","message":"Cache hit","timestamp":"2023-01-22T23:51:07.823Z"} {"level":"info","message":"Cache hit","timestamp":"2023-01-22T23:51:07.864Z"} {"level":"debug","message":"There're more then 10 items","timestamp":"2023-01-22T23:51:08.067Z"} {"level":"info","message":"Deleted: {\n _id: new ObjectId(\"63cdcbeca6beb701291fad66\"),\n key: 'TestMax1',\n value: 'n1R3Pl0G5r4QzT4K2OqsQgiEnLoIu3rR',\n updatedAt: 2023-01-22T23:51:08.029Z,\n __v: 0\n}","timestamp":"2023-01-22T23:51:08.070Z"} PASS src/tests/api.test.ts POST /keys ✓ should add testEntry to app (134 ms) GET /keys/<Entry.key> ✓ will create new key with 201 status code (52 ms) ✓ after creation should be 200 status code (52 ms) ✓ should update updatedAt field (40 ms) GET /keys ✓ should return all entries (41 ms) DELETE /keys/<Entry.key> ✓ should delete one entry (44 ms) DELETE /keys ✓ should delete all entries (34 ms) Check TTL ✓ check that the expireAfterSeconds field is set correctly (23 ms) Max entries count ✓ check max entries count and a deletion logic (66 ms) Test Suites: 1 passed, 1 total Tests: 9 passed, 9 total Snapshots: 0 total Time: 1.716 s, estimated 2 s Ran all test suites.