1.0.3 • Published 6 years ago

basicservice v1.0.3

Weekly downloads
3
License
N/A
Repository
-
Last release
6 years ago

basicservice

npm license github-issues(https://img.shields.io/github/issues/the repositories url.svg) Circle CI build status(https://circleci.com/gh/the repositories url.svg?style=svg)

nodei.co

travis-status(https://img.shields.io/travis/the repositories url.svg) stars(https://img.shields.io/github/stars/the repositories url.svg) forks(https://img.shields.io/github/forks/the repositories url.svg)

forks(https://img.shields.io/github/forks/the repositories url.svg)

![](https://david-dm.org/the repositories url/status.svg) ![](https://david-dm.org/the repositories url/dev-status.svg)

Features

  • Common service for node js based mavenir microservices
  • Has built in support for DirectoryServcer registration (based on pub-sub)
    • Establishes connection to directory server (on start up). Will fail to load if directory server is not availble
    • Listens for "get service" requests (based on service type), will issue a request to DS and will locate to best service avalible
  • Manage depepncies for all common packages for nodes based micro-services

Install

npm install --save basicservice

Scripts

  • npm run start : node dist/index.js
  • npm run test : LOGGER=npm ./node_modules/.bin/mocha --compilers ts:ts-node/register --recursive ./test/**/*.spec.ts
  • npm run build : tsc
  • npm run readme : node ./node_modules/.bin/node-readme

Dependencies

PackageVersionDev
express^4.16.3
json-serialize^1.1.3
json-serializer^2.0.9
mculogger^1.0.6
module-alias^2.0.6
node-uuid^1.4.8
os-utils0.0.14
readme-docs^0.1.1
serialize-javascript^1.5.0
thrift^0.11.0
thrift-serializer^1.1.0
ts-promise^1.0.0
typescript-map0.0.6
winston^2.4.2
assert^1.4.1
@types/chai^4.1.3
@types/express^4.11.1
@types/mocha^5.2.0
@types/node^9.6.6
chai^4.1.2
mocha^5.1.1
node-readme^0.1.9
node-typescript-compiler^1.1.1
nodemon^1.17.3
npmlog^4.1.2
ts-mockito^2.3.0
ts-node^6.0.0
typescript^2.8.3

Author

Mavenir

License

  • N/A : null

Usage

Install

  • npm install basicservice-

Service

Once package is installed, you will need to extend two objects Service and an http service (not mandatory)

Service - DefaultService

  • Create YourService class
  • Extend DefaultService
  • Override:

    • getServicesList: Add to the default list you own apis (methods) as key value => key = thrift methods name, value your own implementation method name (best practice make it the same -:))

      public getServicesList(){
          let srvList = super.getServicesList();
          srvList["getAppDescription"] = this.getAppDescription;
          return srvList;
      }
    • getVersion should return the version (constant) from the thrift file

      -getServicePort should return the service port (constant) from the thrift file

    • getServiceType should return the service type (constant) from the thrift file

    • getAPI retruns the module name containing you own thrift generate code if you call super, it will look for common services (directory service) and throw an error if not supported. 1st look for your own service then call super.

          public getAPI(serviceType : number){
          switch (serviceType) {
              case 101:
              case 202:
                  return myservice;
              default:
                  return super.getAPI(serviceType);
          }
      }

HttpService - DefaultHttpService

  • Based on express package

  • Create YourHttpService class

  • Extend DefaultHttpService
  • Override configureRoutes to add more restfult api

    export class MyHttpService extends DefaultHttpService{
        constructor(service: DefaultService, port: number){
            super(service, port);
        }
    protected configureRoutes(): void {
            this.router.get('/dosomthing', (req, res) => {
                    res.status(200).send(JSON.stringify("ok"));
            });
    
            super.configureRoutes();
        }
    }
    • Note that http servie creation is not mandatory, If your service does not expose http (REST api) this step can be skipped.

Application - main

  • in your main script of your nodejs application, initalize the service objects. Init the Application object and start it. It will run hte thrift server
Application - service creation
  • Create a service (your.thrift) file
  • generate the relevant thrift conversions to nodejs
    • You will have to generate the thrift files for all the basic service dependencies plus your own.
    • Generate thrift based code (auto generated) for all services
      thrift1 -r --gen js:node pubsub_nostream.thrift 
      thrift1 -r --gen js:node directoryserverplugin.thrift
      thrift1 -r --gen js:node default.thrift
      thrift1 -r --gen js:node <myservice>.thrift
Application - main
  • Asuming you haev a service created (see above)
  • Instanciate Your Service & Http service (http is not a mandatory class)
  • Instanciate an application
  • start application

    var service : MyService = new MyService(adress);
    var http : MyHttpService = new MyHttpService(service, parseInt(port));
    
    //In this case http service is not exposed
    var application = new Application(MythriftService, service, undefined);
    
    application.start();