This code is using TypeScript, Express.js and Prisma
This monorepo is composed by 3 packages:
- api-service: Web-facing service that allows registering users, log in, request stock information, query user's history about stock requests and stats based on top 5 requested stocks;
- common: Codebase sheared between
api-service
andstock-service
; - stock-service: Internal service that gets stock information from stooq and returns to the service that has requested the information.
The api-service
and stock-service
are Contract-First, so it's using a middleware that reads the routes documentation and validates each route accordingly to it. This middleware validates requests and responses, so any new route or any change must start with the documentation.
The documentation is a Swagger file and can be found in the contracts
folder in the root folder of the api-service
or stock-service
. After you start api-service
or stock-service
you may see it accessing /docs
route.
To install the dependencies of the project, run the following command on the root folder:
$ yarn
The api-service
has three dependencies, postgres
, rabbitMq
and stock-service
, so before starting the service you'll need to start postgres
and rabbitMq
from the docker-compose.yml in the root folder of the repository:
$ docker-compose up
Before starting the api-service
, you'll need to start stock-service
. Check the next section to do it.
Once you have done the previous steps you may be able to start the services on development mode. All you'll need to do is is create a .env
accordantly to the .env.example
. These examples are located on the root folder from api-service
and stock-service
as well.
All done? go to the root folder of the repository and run:
# stock-service
$ yarn workspace stock-service run dev
Before starting api-service
you'll need to create the database and its tables, you can run the following in the root folder of the repository
$ yarn workspace api-service run prisma:db
Now you can run:
# api-service
$ yarn workspace api-service run dev
To start the services on production mode, you need to make sure that you already have created the .env
files in the root folder of the api-service
and stock-service
as well and has created the database/tables. See Development. Once you confirmed, go to the root folder of the repository and run:
# building
$ yarn workspaces run build
And now you can start the services:
# api-service
$ yarn workspace api-service run start
# stock-service
$ yarn workspace stock-service run start
- APP_PORT: Port the service is going to use;
- APP_SECRET: Secret used to create JWT token;
- TOKEN_EXPIRATION: Time in seconds that the JWT token will expire.
- STOCK_SERVICE_URL:
stock-service
url; - DATABASE_URL: The connection url including authentication info. Check Prisma. Ex:
postgresql://user:password@localhost:5432/mydb?schema=public
; - RABBITMQ_URL: The connection url. Ex:
amqp://localhost
; - RABBITMQ_PREFETCH: Number of unacknowledged messages on a channel. Ex: 10;
- STOCK_EXCHANGE: The name of the stock exchange;
- STOCK_QUEUE: The name of stock queue;
- STOCK_EXCHANGE_DLQ: The name of dlq exchange. Messages that have not been consumed successfully will be sent to this exchange.
- STOCK_DLQ: The name of dlq. Messages sent to STOCK_EXCHANGE_DLQ will be bound to this queue.
- APP_PORT: Port the service is going to use;
- STOCK_URL=The url from stoop;
-
Some routes from
api-service
require authentication, so you'll need a valid JWT token with this specification:Bearer <token>
. You can get one, after registering a userPOST users/register
and then logging in usingPOST users/login
. Detailed information can be found after you start up theapi-service
and go toGET /docs
. -
The information about the most requested stocks is created asynchronously. Each request executed on
GET stocks/:id
will be sent a payload to the message-broker (rabbitMq
) and eventually, this payload will be consumed and create or update the stock stats.