.env
file must be created in the root folder
PORT_1=3000 # PORT to run content service on
PORT_2=3001 # PORT to run user interaction service on
PORT_3=3002 # PORT to run user service on
DB_1=mongodb://127.0.0.1:27017/ContentService
DB_2=mongodb://127.0.0.1:27017/userInteractionService
DB_3=mongodb://127.0.0.1:27017/userService
TOTP_KEY=EFSYLHHHABKPIDKIYWCTLYXROROFVERXDJCM # api key generated using nanoid for accessing user interaction service internal api
HOST=localhost
Everything remains same as above. Only change is to replace 127.0.0.1
to mongo
for the DB URIs like this:
DB_1=mongodb://mongo:27017/ContentService
DB_2=mongodb://mongo:27017/userInteractionService
DB_3=mongodb://mongo:27017/userService
git clone https://github.com/Aditya-ds-1806/microservices.git
cd microservices
npm i
npm start
git clone https://github.com/Aditya-ds-1806/microservices.git
cd microservices
docker-compose build
docker-compose up
DBConnection
class - handle DB connectionUserSchema
,ContentSchema
,UserInteractionSchema
- define schemas for DBUserInteractionService
,ContentService
andUserService
- 3 workers to handle service registrationContentRoutes
,UserRoutes
,UserInteractionRoutes
- define endpoints for microservicesContentControllers
,UserControllers
,UserInteractionControllers
- handle business logic and return responses; if error forward to error handlerContentMiddleware
,UserMiddleware
,UserInteractionMiddleware
to manipulate and validate requests; don't return responses, forward errors to error handlerAPiError
- Error handler; extends native Error; Single point of entry for sending 4xx and 5xx errors
-
Middlewares
- check if userId is valid and user exists by contacting user service
- check if contentId is valid and content exists
- error handler
-
Controllers
- a controller each for CRUD on contents
- sort contents by top and new
- contact user interaction service's internal API that is secured via TOTP, to fetch likes and reads, and sort contents by
top
defined as(reads + likes)
TOTP_KEY
used for generatingTOTP
s is agreed upon before hand by the 2 services andTOTP
sent in headers- controller for inserting documents to DB from csv file
- internal method to sort by new, result is reused for sorting by
top
-
Middlewares
- check if userId is valid and user exists
- check if contentId is valid and content exists by contacting content service
- error handler
-
Controllers
- a controller each for CRUD on User
-
Middlewares
- check if userId is valid and user exists by contacting user service
- check if contentId is valid and content exists by contacting content service
- validate recieved
TOTP
forcontent/stats
endpoint, send401
if theTOTP
s don't match - error handler
-
Controllers
- read and update likes count
- read and update reads count
- internal API for returning likes and reads count on content to assist in sorting by
top
- Used conventional commits, husky and commitlint to write meaningful and organized commit messages
- Used eslint to enforce code style and check for errors