-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
286 lines (254 loc) · 7.01 KB
/
index.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict'
var _ = require('lodash')
var mkdirp = require('mkdirp')
var url = require('url')
var kadfs = require('kad-fs-thomas')
var MemStore = require('kad-memstore-thomas')
var winston = require('winston')
var winstonWrapper = require('winston-meta-wrapper')
var Platform = require('mm-platform')
var MulticastDNS = require('mm-services-mdns')
var Kademlia = require('mm-services-kademlia')
var ServiceManager = require('mm-services-manager')
var TenantService = require('mm-services-tenant')
var Flukso = require('mm-services-flukso')
var DevicesManager = require('mm-services-devices')
var StatusManager = require('mm-services-status')
var Events = require('mm-services-events')
var PouchDB = require('pouchdb')
var memdown = require('memdown')
var debug = require('winston-debug')
var Identity = require('./identity.js')
var Runtime = function (environment) {
this._environment = environment
this.logger = this._getLogger()
this.connectionInfo = this._getConnectionInfo()
this.identity = this._getIdentity()
this.services = {}
}
/* STORAGE FUNCTIONS */
Runtime.prototype._hasPersistence = function () {
return _.has(this._environment, 'PERSISTENCE')
}
Runtime.prototype.isDev = function () {
return _.has(this._environment, 'DEV')
}
Runtime.prototype.appendToPersistence = function (suffix) {
return this._environment.PERSISTENCE + '/' + suffix
}
Runtime.prototype._getKadStore = function (storeName) {
var uri = this._getStoreURI(storeName)
if (uri) {
return this._createKadStore(uri)
} else {
return new MemStore()
}
}
Runtime.prototype._getStoreURI = function (storeName) {
var envName = storeName.toUpperCase() + '_STORE'
if (this._environment[envName]) {
return this._environment[envName]
} else if (this._environment.PERSISTENCE) {
return this._environment.PERSISTENCE + '/' + storeName
}
}
Runtime.prototype._createKadStore = function (name) {
name = url.parse(name)
if (name.protocol === 'file:') {
mkdirp.sync(name.pathname)
return kadfs(name.pathname)
} else {
throw new Error('Unsupported storage location')
}
}
Runtime.prototype._getPouchStore = function (storeName) {
var uri = this._getStoreURI(storeName)
if (uri) {
return this._createPouchStore(uri)
} else {
return new PouchDB(storeName, {
db: require('memdown')
})
}
}
Runtime.prototype._createPouchStore = function (uri) {
var name = url.parse(uri)
if (name.protocol === 'file:') {
mkdirp.sync(name.pathname)
return new PouchDB(name.pathname)
} else {
throw new Error('Unsupported storage location')
}
}
/* LOGGER */
Runtime.prototype._getLogger = function () {
if (this._environment.DEV) {
return winstonWrapper(debug)
}
var level = 'info'
if (this._environment.DEBUG_LEVEL) {
level = 'debug'
}
var logstash = false
if (this._environment.LOGSTASH) {
logstash = true
}
var logger = new winston.Logger({
transports: [
new winston.transports.Console({
level: level,
timestamp: true,
logstash: logstash
})
]
})
return winstonWrapper(logger)
}
/* CONNECTION_INFO */
Runtime.prototype._getConnectionInfo = function () {
if (this._environment.PORT) {
var connectionInfo = [{
transportType: 'udp',
transportInfo: {
port: this._environment.PORT
}
}, {
transportType: 'tcp',
transportInfo: {
port: this._environment.PORT
}
}]
return connectionInfo
}
}
/* IDENTITY */
Runtime.prototype._getIdentity = function () {
if (this._environment.IDENTITY) {
var identity = new Identity(this._environment.IDENTITY)
return identity
}
}
/* CREATE PLATFORM */
Runtime.prototype.createPlatform = function () {
var self = this
var platform = new Platform({
storage: this._getKadStore('platform'),
connectionInfo: this.connectionInfo,
logger: this.logger,
identity: this.identity
})
this.platform = platform
platform.on('ready', function () {
if (process.env.LOG_CONTEXT) {
self.logger.addMeta({
node: platform.identity.getBoxId()
})
}
self._createServices()
})
if (this.identity) {
this.identity.setPlatform(platform)
}
}
/* CREATE SERVICES */
Runtime.prototype._createServices = function () {
var self = this
if (!this._environment.SERVICES) {
this.logger.warn('No services defined')
return
}
var services = this._environment.SERVICES.split(' ')
_.forEach(services, function (serviceName) {
self.createService(serviceName)
})
}
Runtime.prototype.hasService = function (serviceName) {
return _.has(this.services, serviceName)
}
Runtime.prototype.createService = function (serviceName) {
var factoryFunctions = {
'mdns': this._createmDNS,
'kademlia': this._createKademlia,
'flukso': this._createFlukso,
'devices': this._createDevices,
'serviceManager': this._createServiceManager,
'tenants': this._createTenantService,
'events': this._createEventsService,
'status': this._createStatusManager
}
if (_.has(factoryFunctions, serviceName)) {
if (!this.hasService(serviceName)) {
factoryFunctions[serviceName].call(this)
}
return true
}
return false
}
Runtime.prototype._createmDNS = function () {
this.services.mdns = new MulticastDNS({
platform: this.platform,
logger: this.logger
})
}
Runtime.prototype._createKademlia = function () {
this.services.kademlia = new Kademlia({
platform: this.platform,
storage: this._getKadStore('kademlia'),
directoryStorage: this._getKadStore('directory'),
telemetryStorage: this._getKadStore('kademlia-telemetry'),
logger: this.logger,
seeds: []
})
}
Runtime.prototype._createServiceManager = function () {
this.services.serviceManager = new ServiceManager({
platform: this.platform,
logger: this.logger,
runtime: this,
storage: this._getKadStore('serviceManager')
})
}
Runtime.prototype._createFlukso = function () {
this.services.flukso = new Flukso({
logger: this.logger,
platform: this.platform
})
}
Runtime.prototype._createStatusManager = function () {
this.services.status = new StatusManager({
logger: this.logger,
platform: this.platform
})
}
Runtime.prototype._createDevices = function () {
this.services.devices = new DevicesManager({
storage: this._getPouchStore('devices'),
logger: this.logger,
platform: this.platform
})
}
Runtime.prototype._createTenantService = function () {
this.services.tenants = new TenantService({
runtime: this,
runtimeClass: Runtime,
storage: this._getKadStore('tenants'),
secret: this._environment.SECRET,
platform: this.platform,
logger: this.logger
})
}
Runtime.prototype._createEventsService = function () {
this.services.events = new Events({
logger: this.logger,
platform: this.platform
})
}
Runtime.prototype.activateServices = function () {
var self = this
if (_.has(this.services, 'serviceManager')) {
_.forEach(this.services, function (object, key) {
self.services.serviceManager.activate(key)
})
}
}
module.exports = Runtime