Services Config

Objective


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

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


Enable services config


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

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.

config.js
'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"
         }
      }
   }
};
index.js
'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


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


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

Deploy Service
# 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

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!