3.0.7 • Published 3 years ago

pip-services3-container-node v3.0.7

Weekly downloads
279
License
MIT
Repository
github
Last release
3 years ago

IoC container for Node.js

This module is a part of the Pip.Services polyglot microservices toolkit. It provides an inversion-of-control (IoC) container to facilitate the development of services and applications composed of loosely coupled components.

The module containes a basic in-memory container that can be embedded inside a service or application, or can be run by itself. The second container type can run as a system level process and can be configured via command line arguments. Also it can be used to create docker containers.

The containers can read configuration from JSON or YAML files use it as a recipe for instantiating and configuring components. Component factories are used to create components based on their locators (descriptor) defined in the container configuration. The factories shall be registered in containers or dynamically in the container configuration file.

The module contains the following packages:

  • Core - Basic in-memory and process containers
  • Build - Default container factory
  • Config - Container configuration components
  • Refer - Inter-container reference management (implementation of the Referenceable pattern inside an IoC container)

Quick links:

Use

Install the NPM package as

npm install pip-services3-container-node --save

Create a factory to create components based on their locators (descriptors).

import { Factory } from 'pip-services3-components-node';
import { Descriptor } from 'pip-services3-commons-node';

export class MyFactory extends Factory {
  public static MyComponentDescriptor = new Descritor("myservice", "mycomponent", "default", "*", "1.0");

  public constructor() {
    super();
    
    this.registerAsType(MyFactor.MyComponentDescriptor, MyComponent);
  }
}

Then create a process container and register the factory there. You can also register factories defined in other modules if you plan to include external components into your container.

import { ProcessContainer } from 'pip-services3-container-node';
import { DefaultRpcFactory } from 'pip-services3-rpc-node'; 

export class MyProcess extends ProcessContainer {
  public constructor() {
    super('myservice', 'My service running as a process');
    
    this._factories.add(new DefaultRpcFactory());
    this._factories.add(new MyFactory());
  }
}

Define YAML configuration file with components and their descriptors. The configuration file is pre-processed using Handlebars templating engine that allows to inject configuration parameters or dynamically include/exclude components using conditional blocks. The values for the templating engine are defined via process command line arguments or via environment variables. Support for environment variables works well in docker or other containers like AWS Lambda functions.

---
# Context information
- descriptor: "pip-services:context-info:default:default:1.0"
  name: myservice
  description: My service running in a process container

# Console logger
- descriptor: "pip-services:logger:console:default:1.0"
  level: {{LOG_LEVEL}}{{^LOG_LEVEL}}info{{/LOG_LEVEL}}

# Performance counters that posts values to log
- descriptor: "pip-services:counters:log:default:1.0"
  
# My component
- descriptor: "myservice:mycomponent:default:default:1.0"
  param1: XYZ
  param2: 987
  
{{#if HTTP_ENABLED}}
# HTTP endpoint version 1.0
- descriptor: "pip-services:endpoint:http:default:1.0"
  connection:
    protocol: "http"
    host: "0.0.0.0"
    port: {{HTTP_PORT}}{{^HTTP_PORT}}8080{{/HTTP_PORT}}

 # Default Status
- descriptor: "pip-services:status-service:http:default:1.0"

# Default Heartbeat
- descriptor: "pip-services:heartbeat-service:http:default:1.0"
{{/if}}

To instantiate and run the container we need a simple process launcher.

let MyProcess = require('./MyProcess').MyProcess;

try {
    let proc = new MyProcess();
    proc._configPath = "./config/config.yml";
    proc.run(process.argv);
} catch (ex) {
    console.error(ex);
}

And, finally, you can run your service launcher as

node ./service.js

Develop

For development you shall install the following prerequisites:

  • Node.js 8+
  • Visual Studio Code or another IDE of your choice
  • Docker
  • Typescript

Install dependencies:

npm install

Compile the code:

tsc

Run automated tests:

npm test

Generate API documentation:

./docgen.ps1

Before committing changes run dockerized build and test as:

./build.ps1
./test.ps1
./clear.ps1

Contacts

The Node.js version of Pip.Services is created and maintained by:

  • Volodymyr Tkachenko
  • Sergey Seroukhov
  • Mark Zontak

The documentation is written by:

  • Mark Makarychev
@everything-registry/sub-chunk-2439iqs-services-eventgeneration-nodepip-services5-aws-nodepip-templates-microservice-nodepip-clients-comments-nodepip-services-statistics-nodepip-services-tags-nodepip-services-tips-nodepip-services-transducerdata-nodepip-services3-aws-nodepip-services-accounts-nodepip-services-activities-nodepip-services-announcements-nodepip-services-applications-nodepip-services-attachments-nodepip-services-beacons-nodepip-services-blobs-nodepip-services-clusters-nodepip-services-comments-nodepip-services-creditcards-nodepip-services-dashboards-nodepip-services-email-nodepip-services-emailsettings-nodepip-services-eventlog-nodepip-services-feedbacks-nodepip-services-organizations-nodepip-services-orgroles-nodepip-services-passwords-nodepip-services-paymentmethods-nodepip-services-payments-nodepip-services-perfmon-nodepip-services-positions-nodepip-services-purchaseorders-nodepip-services-quotes-nodepip-services-roles-nodepip-services-routeanalysis-nodepip-services-routes-nodepip-services-sessions-nodepip-services-settings-nodepip-services-sms-nodepip-services-smssettings-nodepip-services-guides-nodepip-services-help-nodepip-services-imagesets-nodepip-services-invitations-nodepip-services-jobs-nodepip-services-logging-nodepip-services-metrics-nodepip-services-microfrontends-nodepip-services-msgdistribution-nodepip-services-msgtemplates-nodepip-clients-metrics-nodepip-services3-facade-node@infinitebrahmanuniverse/nolb-pipiqs-facade-public-nodeiqs-services-agreements-nodeiqs-services-attendance-nodeiqs-services-cloudwatch-nodeiqs-services-controlobjects-nodeiqs-services-corrections-nodeiqs-services-currdevicestates-nodeiqs-services-currobjectstates-nodeiqs-services-dataprofiles-nodeiqs-services-deviceconfigs-nodeiqs-services-deviceprofiles-nodeiqs-bindles-infrastructure-nodeiqs-bundles-configuration-nodeiqs-bundles-content-nodeiqs-services-eventrules-nodeiqs-services-eventtemplates-nodeiqs-services-gateways-nodeiqs-services-incidents-nodeiqs-services-locations-nodeiqs-services-mqttgateway-nodeiqs-services-objectgroups-nodeiqs-services-objectstates-nodeiqs-services-opevents-nodeiqs-services-resolutions-nodeiqs-services-restgateway-nodeiqs-services-devices-nodeiqs-services-emergencyplans-nodeiqs-services-rosters-nodeiqs-services-shifts-nodeiqs-services-signals-nodeiqs-services-stateupdates-nodeiqs-services-teltonikagateway-nodeiqs-services-zones-node
3.0.7

3 years ago

3.0.5

3 years ago

3.0.4

5 years ago