-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
194 lines (175 loc) · 6.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import 'cls-hooked';
import TransactionContext from './transaction-context';
import { RpcClient } from './rpc/client';
import { RpcServer } from './rpc/server';
import _ from 'lodash';
import {Logger as StandardLogger} from './logging/standard_logger';
import { Config as ConfigHelper } from './config';
import { initCredentials } from './credential_management'
import * as Singleton from './singleton';
import { LOG_CONSTANTS } from './logging/constants';
import RPC_CONSTANTS from './constants';
import Server from './server';
import { RpcFrameworkInterface } from './interface';
import { getLogger } from './logging';
const RpcFramework: RpcFrameworkInterface = {
/**
* @param service_id
*
* @return <config>
*/
initConfig: (service_id, options) => {
const config = ConfigHelper.initConfig(service_id, options);
Singleton.addToSingleton('Config', config);
return config;
},
//TODO: To be removed once services remove usage
initCredentials: (service_id) => {
return initCredentials(service_id);
},
//TODO: To be removed once services remove usage
createExternalClient: (service_id, external_service_id, config) => {
return RpcClient.createExternalClient(service_id, external_service_id, config);
},
/**
*
* Returns a logger for the service.
* @param service_id
* @return Logger { info(data), error(data) }
*/
initLogger: (options) => {
const loggerType = StandardLogger;
const logger = getLogger(loggerType, options);
Singleton.addToSingleton('LOG_CONSTANTS', LOG_CONSTANTS);
Singleton.addToSingleton('Logger', logger);
return logger;
},
/**
* This is to generate a transaction id which is passed in an api call. This is exposed for monoliths. For microservices, its inbuilt.
*/
initTransactionContext: (params) => {
const param = params || {};
TransactionContext.patchBluebird(param.bluebird);
Singleton.addToSingleton('TransactionContext', TransactionContext);
return TransactionContext;
},
/** createClient(client_id, auth_token, method_names, t_interface, server_host, server_port)
*
* @param service_id – Service ID of the 'caller' service
* @param called_service_id – Service ID of the 'called' service\
* @param schema – service's openapi schema.
* @param server_host – IP of the host service
* @param server_port – Port of the host service
* @param client_options - additional options for creating this client, includes:
* retry : retry options:
* retries
* retryAfterMs
* backOffFactor
* errorHandler
* timeoutInMs
* keep_alive: object with options for http connection keep-alive. Properties:
* enabled: set it to true if you want to use keep-alive
* maxSockets: max number of open sockets connected to server
* api_config: object containing API-level configuration like circuit_breaker_options,
* eg: "getCartItems": {
* "CIRCUIT_BREAKER_OPTIONS": {
* "ENABLE": true, // Circuit breaker will be bypassed if this is set to false
* "TIMEOUT": 2000, // in milliseconds. There will be an error thrown if timeout occurs
* "CIRCUIT_BREAKER_FORCE_CLOSED": true // if set to true, the circuit will never break, only the timeout will occur
* }
* }
*
* @return client { method1: function(..) , method2: function(..) , ....... }
*
* eg.
* var client = createClient(...);
* var header = createHeader(...); // read from the docs
* client.method( header, arg1, arg2, ... )
*/
createClient: (service_id, called_service_id, schema, server_host, server_port, client_options) => {
return RpcClient.createClient(service_id, called_service_id, schema, server_host, server_port, client_options);
},
/**
* Create the RPC (openapi) service.
*
* @param service_id
* @param auth_service_ids – [] Service's that are allowed to query. Undefined means no auth.
* @param schema – service's openapi schema.
* @param service – Implementation – { <method_name> : function(..) {..} }
* @param port
*
* @return server { end: function() }
*/
createServer: (service_id, auth_service_ids, schema, service, port) => {
return RpcServer.createServer(service_id, auth_service_ids, schema, service, port);
},
/**
* This allows you to store global objects.
* This is partiularly useful for storing initialised DB connections, config, logger, etc.
* How to use –
* import { getSingleton } from 'marc';
* Singleton = getSingleton();
* Logger = Singleton.Logger;
* Config = Singleton.Config;
* MongoMainStore = Singleton.mongoMainStore;
* MongoMainStore.getUser(...);
* Config.SERVICE_ID;
* Logger.SERVICE_ID;
*/
getSingleton: () => {
return Singleton.getSingleton();
},
/**
* Add to the singleton.
* Config (default)
* Logger (default)
*
* eg –
* DBs / Stores – eg. RedisMainStore, MongoMainStore, MysqlMainStore, MysqlMonetisationStore, etc
*/
addToSingleton: (key, value) => {
return Singleton.addToSingleton(key, value);
},
/** adds multiple key,value pairs */
addObjToSingleton: (obj) => {
return Singleton.addObjToSingleton(obj);
},
getDependencyConfig: () => {
return RPC_CONSTANTS.DEPENDENCY;
},
getGatewayConstants: () => {
return RPC_CONSTANTS.GATEWAY;
},
/**
* Run workflow tasks. src/workflow/index changes example:
* require('marc').initWorkflow();
*
*/
initWorkflow: () => {
return Server.initWorkflow(RpcFramework);
},
/**
* Run service. server.js file changes example:
* let RPCFramework = require('marc').initService()
*
*/
initService: () => {
return Server.initService(RpcFramework);
},
/**
* Run the service using using the Service object. server.js file changes example:
*
* let Service = require('marc').getService();
*
* Service.initDependency()
* .then(function () {
* let controller = require('./src/index');
* Service.initServer(controller);
* })
*
*/
getService: () => {
return new Server.service(RpcFramework);
}
}
export = RpcFramework