Skip to content

Latest commit

 

History

History
160 lines (137 loc) · 4.58 KB

File metadata and controls

160 lines (137 loc) · 4.58 KB

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

TMP How to make a test client?

var all = require('@common/go-grpc')
var testServerHost = 'localhost:50051'
// var testServerHost = 'ecb-provider:50051'
var c3 = new all.ecbProvider.EcbProviderClient( testServerHost, all.createInsecure());
var r3 = await c3.GetRates(new all.currencyProvider.GetRatesRequest())
r3.toObject()
// inside converter container
var all = require('@common/go-grpc')
var testServerHost = '0.0.0.0:50052'
var c2 = new all.currencyConverter.CurrencyConverterClient( testServerHost, all.createInsecure());
var r2 = await c2.Convert(new all.currencyConverter.ConvertRequest({ sellAmount: 100, sellCurrency: 'USD', buyCurrency: 'GBP' }));
r2.toObject()

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