service-msgtemplates2-node
v1.0.0
Published
Message templates microservice in Node.js / ES2017 V2
Maintainers
Readme
Message Templates Microservice for Node.js / ES2017 V2
This is a message templates microservice from the Pip.Services library.
- Shows inspirational message templates to users on various topics
The microservice currently supports the following deployment options:
- Deployment platforms: Standalone process, AWS Lambda function
- External APIs: HTTP/REST, gRPC
- Persistence: In-memory, flat files, MongoDB
This microservice has no dependencies on other microservices.
Quick Links
- Contract
- Download
- Develop:
- Configure:
- Run:
- Use
- Acknowledgements
- Source Documentation
- Client Libraries:
Contract
The logical contract of the microservice is presented below. For physical implementation (HTTP/REST, gRPC, Lambda), please refer to the documentation of the specific protocol.
class MessageTemplateV1 implements IStringIdentifiable {
public id: string;
public name: string;
public from: string;
public subject: MultiString;
public text: MultiString;
public html: MultiString;
public status: string;
}
interface IMessageTemplatesService {
getTemplates(ctx: IContext, filter: FilterParams, paging: PagingParams): Promise<DataPage<MessageTemplateV1>>;
getTemplateById(ctx: IContext, id: string): Promise<MessageTemplateV1>;
getTemplateByIdOrName(ctx: IContext, idOrName: string): Promise<MessageTemplateV1>;
createTemplate(ctx: IContext, template: MessageTemplateV1): Promise<MessageTemplateV1>;
updateTemplate(ctx: IContext, template: MessageTemplateV1): Promise<MessageTemplateV1>;
deleteTemplateById(ctx: IContext, id: string): Promise<MessageTemplateV1>;
}Download
The main way to get the microservice is to check it out directly from the Bitbucket repository:
git clone [email protected]:entinco/eic-services-content2.gitThe microservice is also available as a NPM package under the same name. You can add a dependency to the microservice into the package.json file of your project:
{
...
"dependencies": {
...
"service-msgtemplates2-node": "^1.0.*",
...
}
}Develop
Environment Setup
This is a Node.js project and you have to install Node.js tools. You can download them from the official Node.js website: https://nodejs.org/en/download/
After Node.js is installed, you can verify its version:
node --versionThen you need to install some node tools (recommended to be global):
# Install typescript compiler
npm install typescript -g
# Install mocha test runner
npm install mocha -gTo work with the code repository, you need to install Git: https://git-scm.com/downloads
If you are planning to develop and test using persistent storages other than flat files, you may need to install database servers:
- Install MongoDB: https://www.mongodb.org/downloads
Installing
After your environment is ready, you can copy the microservice source code from the repository:
git clone [email protected]:entinco/eic-services-content2.gitThen go to the project folder and install dependent modules:
# Install dependencies
npm installIf you have worked with the microservice before, you can check out the latest changes and update the dependencies:
# Update source code
git pull
# Update dependencies
npm updateBuilding
This microservice is written in TypeScript language which is transcompiled into JavaScript. So, if you make changes to the source code, you need to compile it before running or committing. The process will output compiled JavaScript files into the "obj" folder:
tscWhen you do continuous edit-build-test cycles, you can run the TypeScript compiler with "--watch" to detect and compile changes you make automatically:
tsc --watchTesting
Before you execute tests, you may need to set configuration options into a config.yml file, located by default in the "config" folder in the root of the project. For more information, see the Configure guide.
Run unit tests:
npm testExecute benchmarks:
npm run benchmarkContributing
Developers interested in contributing should read the following instructions:
Please do not ask general questions in an issue. Issues are only to report bugs, request enhancements, or request new features. For general questions and discussions, use the Contributors Forum.
It is important to note that for each release, the Changelog is a resource that will itemize all:
- Bug Fixes
- New Features
- Breaking Changes
Configure
As a starting point, you can use this example to create your own config.yml file:
---
- descriptor: "pip-services:context-info:default:default:1.0"
name: "service-msgtemplates2"
description: "Message templates microservice for pip-services V2"
- descriptor: "pip-services:logger:console:default:1.0"
level: "trace"
- descriptor: "msgtemplates:persistence:file:default:1.0"
path: "../data/msgtemplates.json"
- descriptor: "msgtemplates:service:default:default:1.0"
- descriptor: "pip-services:endpoint:http:default:1.0"
connection:
protocol: "http"
host: "0.0.0.0"
port: 8080
- descriptor: "msgtemplates:controller:commandable-http:default:1.0"Afterwards, check all configuration options. Specifically, pay attention to connection options for database and dependent microservices.
For more information on this section, read the Pip.Services Configuration Guide.
Persistence
The microservice supports three types of persistence: in-memory, flat files, or MongoDB. In-memory and flat files are great for development and testing, while MongoDB is a good option with outstanding performance and scalability, suitable for demanding production installations. You can choose and configure the option that suits your needs.
Memory
Memory persistence has the following configuration properties:
- options: object - (optional) Miscellaneous configuration options
- max_page_size: number - (optional) Maximum number of items per page (default: 100)
Example:
- descriptor: "msgtemplates:persistence:memory:default:1.0"
options:
max_page_size: 100File
Flat file persistence has the following configuration properties:
- path: string - file path where class objects are stored. The object are written into the file in JSON format.
- options: object - (optional) Miscellaneous configuration options
- max_page_size: number - (optional) Maximum number of items per page (default: 100)
Example:
- descriptor: "msgtemplates:persistence:file:default:1.0"
path: "./data/msgtemplates.json"MongoDB
MongoDB persistence has the following configuration properties:
- collection: string - MongoDB collection name
- connection(s): object - MongoDB connection properties
- uri: string - MongoDB URI
- host: string - IP address/hostname binding (default: 'localhost')
- port: number - MongoDB port number (default: 27017)
- database: string - MongoDB database name
- credential: object - MongoDB user credentials
- username: string - user name
- password: string - user password
- options: object - (optional) MongoDB connection options. See http://mongoosejs.com/docs/connections.html for more details.
- debug: boolean - (optional) Enables or disables connection debugging
Example:
- descriptor: "msgtemplates:persistence:file:default:1.0"
collection: "test_obj"
connection:
uri: "mongodb://localhost/pipservicestest"
host: "localhost"
port: 27017
database: "test_app"
options:
server:
poolSize: 4
socketOptions:
keepAlive: 1
connectTimeoutMS: 5000
auto_reconnect: trueService
Service has no configuration properties.
Example:
- descriptor: "msgtemplates:service:default:default:1.0"Controllers
The controller components (also called "endpoints") expose external microservice API for consumers. Each microservice can expose multiple APIs (HTTP/REST, gRPC) and multiple versions of the same API type. At least one controller is required for the microservice to run successfully.
HTTP
HTTP/REST controller has the following configuration properties:
- connection: object - HTTP transport configuration options
- protocol: string - HTTP protocol - 'http' or 'https' (default: 'http')
- host: string - IP address/hostname binding (default: '0.0.0.0')
- port: number - HTTP port number
- swagger: object - (optional) Swagger controller integration options
- enable: boolean - (optional) Enables or disables Swagger integration
- auto: boolean - (optional) Enables or disables automatic Swagger integration configuration
Example:
# Common HTTP endpoint
- descriptor: "pip-services:endpoint:http:default:1.0"
connection:
protocol: "http"
host: "0.0.0.0"
port: 8080
# HTTP controller
- descriptor: "msgtemplates:controller:commandable-http:default:1.0"
swagger:
enable: true
auto: truegRPC
gRPC controller has the following configuration properties:
- connection: object - gRPC transport configuration options
- protocol: string - gRPC protocol - 'http' or 'https' (default: 'http')
- host: string - IP address/hostname binding (default: '0.0.0.0')
- port: number - gRPC port number
Example:
# Common GRPC endpoint
- descriptor: "pip-services:endpoint:grpc:default:1.0"
connection:
protocol: "http"
host: "0.0.0.0"
port: 8090
# GRPC controller
- descriptor: "msgtemplates:controller:grpc:default:1.0"
# Commandable GRPC controller
- descriptor: "msgtemplates:controller:commandable-grpc:default:1.0"For more information on this section, read the Pip.Services Configuration Guide.
Run
Standalone Process
The simplest way to deploy the microservice is to run it as a standalone process. This microservice is implemented in JavaScript and requires installation of Node.js. You can get it from the official site at https://nodejs.org/en/download
Step 1. Download the microservice by following the Download instructions.
Step 2. Add a config.yml file to the "config" folder in the root of the project and set configuration parameters. See the Configure guide for details.
Step 3. Start the microservice:
node ./bin/main.jsAWS Lambda Function
This microservice can also be packaged and deployed to AWS as a Lambda function. This microservice is implemented in JavaScript and requires the installation of Node.js. You can download it from the official Node.js website: https://nodejs.org/en/download/
Step 1. Download the microservice by following the Download instructions.
Step 2. Add a config.yml file to the "config" folder in the root of the project and set configuration parameters. See the Configure guide for details.
Step 3. Go to the project folder and install the dependent modules:
npm installStep 4. Package the project folder into a .zip file:
zip -r lambda_function.zip .Step 5. Go to the AWS Lambda console and create a new Lambda function.
- Name: "service-msgtemplates2-node"
- Runtime: Choose "Node.js 14.x" or newer
- Function code: Upload the .zip package of the project folder
Step 6: Configure any triggers necessary to invoke the function or invoke it via the AWS Lambda console.
Use
The easiest way to work with the microservice is to use the client library. The complete list of available client libraries for different languages is listed in the Quick Links.
If you use Node.js, then you can add a dependency to the client library into the package.json file of your project:
{
...
"dependencies": {
...
"client-msgtemplates2-node": "^1.0.*",
...
}
}Then install the dependency:
# Install new dependencies
npm install
# Update already installed dependencies
npm updateInside your code, get the reference to the client library:
let node_lib = new require('client-msgtemplates2-node');Define client configuration parameters that match the configuration of the microservice external API:
// Client configuration
let config = {
connection: {
protocol: 'http',
host: 'localhost',
port: 8080
}
};Instantiate the client and open a connection to the microservice:
// Create the client instance
let client = node_lib.MessageTemplatesCommandableHttpClientV1(config);
// Connect to the microservice
try {
await client.open(null);
// Work with the microservice
...
}
catch (err) {
console.error('Connection to the microservice failed');
console.error(err);
}Now the client is ready to perform operations:
// Create a new msgtemplate
let template = {
name: 'Welcome',
subject: { en: 'Welcome to our product' },
text: { en: 'Welcome <%= name %>!' },
html: { en: '<h1>Welcome <%= name %>!<h1>' },
status: 'completed'
};
template = await client.createTemplate(
null,
template
);// Get welcome message template
template = await client.getTemplateByIdOrName(
null, 'Welcome',
);Acknowledgements
This microservice was created by Sergey Seroukhov and is currently maintained by Michael Seroukhov.
