Contenu connexe Similaire à EWD 3 Training Course Part 16: QEWD Services (20) EWD 3 Training Course Part 16: QEWD Services1. Copyright © 2016 M/Gateway Developments Ltd
EWD 3 Training Course
Part 16
QEWD Services
Rob Tweed
Director, M/Gateway Developments Ltd
Twitter: @rtweed
2. Copyright © 2016 M/Gateway Developments Ltd
QEWD Services
• The back-end message handler functions
described so far exist in an application's
back-end module
– Our application was named demo1:
• So its back-end handler module is:
– node_modules/demo1.js
• Not re-usable across different applications
– Do you have to copy commonly-used handler
functions into each application's module?
3. Copyright © 2016 M/Gateway Developments Ltd
QEWD Services
• The back-end message handler functions
described so far exist in an application's
back-end module
– Application demo1:
• Back-end handler module: node_modules/demo1.js
• Not re-usable across different applications
– Do you have to copy commonly-used handler
functions into each application's module?
• No – you can use define them as Services
4. Copyright © 2016 M/Gateway Developments Ltd
Defining a QEWD Service
• A QEWD Service is simply a back-end
handler module that you create and name,
independently of any application
• By default, the Service name === the
module name
• eg: a Service named security would be
saved in node_modules/security.js
• All of its message type handler functions
can then be used by any application
5. Copyright © 2016 M/Gateway Developments Ltd
Example
• To create a Service named myService:
– C:qewdnode_modulesmyService.js or
– ~/qewd/node_modules/myService.js
module.exports = {
handlers: {
myTest: function(messageObj, session, send, finished) {
console.log('This is Robs Test Service!');
finished({robs: 'service done'});
}
}
}
6. Copyright © 2016 M/Gateway Developments Ltd
Invoking a Service
• In any application:
– 1) Authorise the application to use the service
• Essential to prevent a malicious hacker trying to
invoke critical service handler methods from an
arbitrary QEWD application
7. Copyright © 2016 M/Gateway Developments Ltd
Services allowed by application
• C:qewdnode_modulesdemo1.js or
• ~/qewd/node_modules/demo1.js
module.exports = {
servicesAllowed: {
myService: true
},
handlers: {
testButton: function(messageObj, session, send, finished) {
session.data.$('foo').value = 'bar';
finished({
ok: 'testButton message was processed successfully!'
});
}
}
};
The demo1 application
is now allowed to use the
myService QEWD Service
8. Copyright © 2016 M/Gateway Developments Ltd
Services allowed by application
• C:ewd3node_modulesdemo1.js
module.exports = {
servicesAllowed: {
myService: true,
myOtherService: true
},
handlers: {
testButton: function(messageObj, session, send, finished) {
session.data.$('foo').value = 'bar';
finished({
ok: 'testButton message was processed successfully!'
});
}
}
};
You can allow the use of
as many QEWD services
as you wish
9. Copyright © 2016 M/Gateway Developments Ltd
Invoking a Service
• In any application:
– 1) Authorise the application to use the service
• Essential to prevent a malicious hacker trying to
invoke critical service handler methods from any
ewd-xpress application
– 2) Add the service property to the EWD.send()
function within your application's front-end
app.js
10. Copyright © 2016 M/Gateway Developments Ltd
Edit app.js
$(document).ready(function() {
EWD.log = true;
EWD.on('ewd-registered', function() {
EWD.on('intermediate', function(responseObj) {
$('#content').text(responseObj.message.date);
});
EWD.on('socketDisconnected', function() {
$('#content').text('You have been logged out');
$('#testBtn').hide();
});
$('#testBtn').on('click', function(e) {
var message = {type: 'testButton'};
EWD.send(message, function(messageObj) {
$('#content').append('<br /> ' + messageObj.message.ok);
});
EWD.send({
type: 'myTest',
service: 'myService'
}, function(responseObj) {
console.log('response via Ajax: ' + JSON.stringify(responseObj));
});
});
});
EWD.start('demo1', $);
});
11. Copyright © 2016 M/Gateway Developments Ltd
Edit app.js
$(document).ready(function() {
EWD.log = true;
EWD.on('ewd-registered', function() {
EWD.on('intermediate', function(responseObj) {
$('#content').text(responseObj.message.date);
});
EWD.on('socketDisconnected', function() {
$('#content').text('You have been logged out');
$('#testBtn').hide();
});
$('#testBtn').on('click', function(e) {
var message = {type: 'testButton'};
EWD.send(message, function(messageObj) {
$('#content').append('<br /> ' + messageObj.message.ok);
});
EWD.send({
type: 'myTest',
service: 'myService'
}, function(responseObj) {
console.log('response via Ajax: ' + JSON.stringify(responseObj));
});
});
});
EWD.start('demo1', $);
});
This will invoke the
myTest handler in the
myService Service
module
12. Copyright © 2016 M/Gateway Developments Ltd
Dynamic Service Access Control
• eg: role-based control to services
• Performed via the QEWD Session
13. Copyright © 2016 M/Gateway Developments Ltd
Dynamic Service Control
• Give a user access to a service:
– In a back-end message-handler function:
• session.allowService(serviceName);
– eg session.allowService('security');
– Note: this can be done even if the
application's module doesn't specify that the
service is allowed
14. Copyright © 2016 M/Gateway Developments Ltd
Dynamic Service Control
• Prevent a user from accessing a service:
– session.disallowService(serviceName);
• eg session.disallowService('security');
– Note: this overrides the setting in the
application's servicesAllowed object
15. Copyright © 2016 M/Gateway Developments Ltd
Service Module Mapping
• By default, QEWD will assume that the
Service module has the same physical file
name as the service name
– eg: a Service named security would be
expected, by default, to be found in
• node_modules/security.js
• However, you can over-ride this and map
a Service name to any module name/path
16. Copyright © 2016 M/Gateway Developments Ltd
Service Module Mapping
• Service name mapping is done in your
QEWD start-up file
– Define a config property named moduleMap
• This is an object / hash mapping one or more
Service names to corresponding module
names/paths
17. Copyright © 2016 M/Gateway Developments Ltd
Service Module Mapping
• Example:
var config = {
managementPassword: 'keepThisSecret!',
serverName: 'My QEWD Server',
port: 8080,
poolSize: 2,
database: {
type: 'gtm'
},
moduleMap: {
security: 'mySecurityService'
}
};
var qewd = require('qewd').master;
qewd.start(config);
18. Copyright © 2016 M/Gateway Developments Ltd
Service Module Mapping
• Example:
var config = {
managementPassword: 'keepThisSecret!',
serverName: 'My QEWD Server',
port: 8080,
poolSize: 2,
database: {
type: 'gtm'
},
moduleMap: {
security: 'mySecurityService'
}
};
var qewd = require('qewd').master;
qewd.start(config);
This tells QEWD that in
order to handle messages
within the security Service,
it must first load a module using
require('mySecurityService')
19. Copyright © 2016 M/Gateway Developments Ltd
Service Module Mapping
• Example:
var config = {
managementPassword: 'keepThisSecret!',
serverName: 'My QEWD Server',
port: 8080,
poolSize: 2,
database: {
type: 'gtm'
},
moduleMap: {
security: '/path/to/mySecurityService'
}
};
var qewd = require('qewd').master;
qewd.start(config);
This tells QEWD that in
order to handle messages
within the security Service,
it must first load a module using
require('/path/to/mySecurityService')
20. Copyright © 2016 M/Gateway Developments Ltd
Service Module Mapping
• Example:
var config = {
managementPassword: 'keepThisSecret!',
serverName: 'My QEWD Server',
port: 8080,
poolSize: 2,
database: {
type: 'gtm'
},
moduleMap: {
security: '/path/to/mySecurityService',
tools: 'tools'
}
};
var qewd = require('qewd').master;
qewd.start(config);
You can map as many
Services as needed
21. Copyright © 2016 M/Gateway Developments Ltd
Service Module Mapping
• QEWD Services can therefore be defined
as standard Node.js modules
– Can be published to NPM
– Can be installed from NPM