Skip to content

This mono repository contains a sample of micro-services architecture built on top of gRPC protocol and TypeScript node.js applications

License

Notifications You must be signed in to change notification settings

x-technology/mono-repo-nodejs-svc-sample

 
 

Repository files navigation

mono-repo-nodejs-svc-sample

This mono repository contains a sample of micro-services architecture built on top of gRPC protocol and TypeScript node.js applications

Getting started

Install protoc for generating definitions based on .proto files

brew install protobuf
protoc --version  # Ensure compiler version is 3+

Prepare environment

yarn install
yarn lerna bootstrap

Build common packages, so we're able to use it for our services

yarn lerna run build

FAQ

How to create a new library?

  1. For example, we want to create a new logger library.
  2. Create a folder under ./packages/common/ path. For simplicity, just copy an existing lib and rename it.
mkdir ./packages/common/logger
  1. Go to the folder in the terminal
cd ./packages/common/logger
  1. Install dependencies
yarn install
  1. Make sure to define appropriate name in the package.json file:
"name": "@common/logger",

Let's follow a rule all common libraries have a prefix @common/ 5. Create our library in a src/index.js

export const debug = (message: string) => console.debug(message);
export const info = (message: string) => console.info(message);
export const error = (message: string) => console.error(message);

export default { debug, info, error };
  1. Make sure it builds successfully withing a command:
yarn build
  1. Let's connect our newly created library somewhere in the existing service:
yarn lerna add @common/logger --scope=@grpc/ecb-provider
  1. The final step, we need to use the library inside ecb-provider service. Let's amend file ./src/index.ts:
import logger from '@common/logger';

logger.debug('service has started');
  1. Re-build currency-converter to ensure the is not issues
yarn build

Yay! 🎉 It works!

How to create a new service?

  1. For example, we want to create a new crypto-compare-provider service, which is another currency rate provider returning cryptocurrencies.
  2. Create a folder under ./packages/services/grpc/crypto-compare-provider path. For simplicity, just copy an existing ecb-provider and rename it.
mkdir ./packages/services/grpc/crypto-compare-provider
  1. Go to the folder in the terminal
cd ./packages/services/grpc/crypto-compare-provider
  1. Install dependencies
yarn install
  1. Make sure to define appropriate name in the package.json file:
"name": "@grpc/crypto-compare-provider",

Let's follow a rule - all grpc services have a prefix @grpc/. 5. Create a service method file packages/services/grpc/crypto-provider/src/services/getRates.ts

import { currencyProvider } from '@common/go-grpc';

export default async (
  _: currencyProvider.GetRatesRequest,
): Promise<currencyProvider.GetRatesResponse> => {
  return new currencyProvider.GetRatesResponse({
    rates: [],
    baseCurrency: 'USD',
  });
};
  1. So next we need to use this method inside server.ts
import { Server, LoadProtoOptions, currencyProvider } from '@common/go-grpc';
import getRates from './services/getRates';

const { PORT = 50051 } = process.env;
const protoOptions: LoadProtoOptions = {
  path: `${__dirname}/../../../../../proto/crypto-compare-provider.proto`,
  // this value should be equvalent to the one defined in *.proto file as "package cryptoCompareProvider;"
  package: 'cryptoCompareProvider',
  // this value should be equvalent to the one defined in *.proto file as "service CryptoCompareProvider"  
  service: 'CryptoCompareProvider',
};

const server = new Server(`0.0.0.0:${PORT}`, protoOptions);
server
  .addService<currencyProvider.GetRatesRequest,
    Promise<currencyProvider.GetRatesResponse>>('GetRates', getRates);
export default server;
  1. Make sure it builds successfully withing a command:
yarn build
  1. Start the service with the command:
yarn start

About

This mono repository contains a sample of micro-services architecture built on top of gRPC protocol and TypeScript node.js applications

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 71.2%
  • JavaScript 10.9%
  • Perl 9.1%
  • Raku 8.7%
  • Shell 0.1%