Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Objective


 SOAJS SOAJS provides the ability to store data per tenant. What this means is that each tenant has a config object, and this object is only accessed by the users of that specific tenant.


This space will show you:



Warning
titleWarning

Please make sure you have installed the SOAJS Dashboard before continuing!



Enable services config
Anchor
enable
enable


Since the services config is tied to the tenant, our service must be multitenant, so we have to set extKeyRequired to true. Let's setup a simple microservice to demonstrate this.


Note
titleNote

Remember that a service is composed, at least, of a config.js and index.js file. Click here to remember how to build a service.



Code Block
languagejs
titleconfig.js
linenumberstrue
'use strict';

module.exports = {
   "serviceVersion": 1,
   "serviceName": "servicesConfig",
   "serviceGroup": "SOAJS Services Config Example",
   "servicePort": 4130,
   "requestTimeout": 30,
   "requestTimeoutRenewal": 5,
   "extKeyRequired": true,
   "errors": {},
   "schema": {
      "/get": {
         "_apiInfo": {
            "l": "get name"
         }
      }
   }
};



Code Block
languagejs
titleindex.js
linenumberstrue
'use strict';
var soajs = require('soajs');
var config = require('./config.js');

var service = new soajs.server.service(config);

service.init(function() {
   service.get("/get", function(req, res) {
      var name = req.soajs.servicesConfig.name;
      res.json(req.soajs.buildResponse(null, "Hello " + name));
   });
   
   service.start();
});




Configure services config from Dashboard
Anchor
configure
configure


We need to create at least two tenants to see how the response will differ based on which key we provide the request.

Check out the Multitenancy page to learn how to create a tenant and configure its services config.



Example
Anchor
example
example


Now that everything is set up, let's test our service though an example.

Code Block
languagebash
titleDeploy Service
linenumberstrue
# Go to the directory where you created your config.js and index.js files
cd /opt/soajs/node_modules/servicesConfig.example


# Export necessary environment variable
export SOAJS_ENV=dev 
 
# Run the service
node .

The service is now running and listening for requests. 

Using a terminal window, Postman, Insomnia, etc... we can test our service.


Note
titleNote

Make sure that the keys in the header of your requests are the keys that you created and not the one form the screenshot!


We see that depending on which key we use in the header, the response returns the name specified in the service config of each tenant!