S4

Introduction


Finally, we arrive the the last service in this section: S4. 

Service S4's main focus is to demonstrate how a same API can respond completely differently based on which tenant calls it.

Let's start S4::

# go to correct directory
cd /opt/soajs/node_modules/soajs.jsconf/services/s4


# export necessary environment variables
export SOAJS_PROFILE=/opt/soajs/node_modules/soajs.utilities/data/jsconf/profile.js
export SOAJS_ENV=test
export SOAJS_SRVIP=127.0.0.1


# start service
node .

Code Walkthrough


This page shows you how to interact with the s4 service after you install it and run it.

The code walkthrough is located in a sub page and explained in depth. Click here to learn more about how the code works


Interacting with S4 service



curl -X POST -H "key: 4f9b4dbc4c8178a3983b8c0d42cd42d30e63f910ac5e4e51843b542c34d1f6790eda4c8b425470cb71ad6eed58787f59d1b9d8abd9cb43ddc1086641779752348c436a5e6d79c74b2aa59feaf4ecf1db868c7f77383d33b30208c8e31729b857" -H "Content-Type: application/json" -d '{"username": "mike","email":[{"address":"team@soajs.org","primary": true}]}' "http://test-api.mydomain.com/jsconf4/multi/add"

Response

Response
{"result":true,"data":true,"soajsauth":"Basic c29hanM6QzAzTUhmVU1Jb3c0M3RzZDEtWFhQZ2hNRFo4ajB5TTdFNWQ="}

Now let's try the same API hit but with a different tenant. To reflect this tenant change, we will use a different key in our request:

curl -X POST -H "key: d1e3b418bb1a18f35954c590d0cf06ff2d9e1f0b46b5c7d540f038efcde4430862a4ef9f71e74d8d45401441397b1c0be7e765b13266d63f509de3cd72cff059d048b2957d4aff259340f0b14f60823631a39ee57ce242fada8ee6741b594931" -H "Content-Type: application/json" -d '{"username": "mike","email":[{"address":"team@soajs.org","primary": true}]}' "http://test-api.mydomain.com/jsconf4/multi/add"

Response

Response
{"result":true,"data":true}

We see that the framework is allowing both tenants access to the /multi/add API successfully. This exhibits how we can use a multitenant db in SOAJS.

What we have here is a framework that allows different tenants access to the same API in the same way.

But what if we had multiple tenants, accessing the same API, but getting completely different responses based on which tenant they are?

This next demonstration will begin to shed light on the true power that SOAJS offers:


curl -X GET -H "key: 9ee308d7b67d2e58a8770b99c8c0320c8d7262a72fc9516e09395bfa39f91b95190bfde9986f4e902ad5ba9de35573dbc5d087c1699c36632c1fccb91663c77529f633c8247366074d399ab326bfdeaa7211ce8c63b968c73cea7aab46296629" "http://test-api.mydomain.com/jsconf4/hybrid"

Response

Response
{"result":true,"data":"Hello i am a message!"}

When the first tenant makes a call to the /hybrid API, they get a response with a message saying "Hello i am a message".

Let's see what happens when the second tenant calls the same API:

curl -X GET -H "key: 4f9b4dbc4c8178a3983b8c0d42cd42d30e63f910ac5e4e51843b542c34d1f6790eda4c8b425470cb71ad6eed58787f59d1b9d8abd9cb43ddc1086641779752348c436a5e6d79c74b2aa59feaf4ecf1db868c7f77383d33b30208c8e31729b857" "http://test-api.mydomain.com/jsconf4/hybrid"

Response

Response
{"result":true,"data":"mike"}

The second tenant got a completely different response!

Let's see the response that tenant three will get:

curl -X GET -H "key: d1e3b418bb1a18f35954c590d0cf06ff2d9e1f0b46b5c7d540f038efcde4430862a4ef9f71e74d8d45401441397b1c0be7e765b13266d63f509de3cd72cff059d048b2957d4aff259340f0b14f60823631a39ee57ce242fada8ee6741b594931" "http://test-api.mydomain.com/jsconf4/hybrid"

Response

Response
{"result":true,"data":{"name":"mike","email":"team@soajs.org"}}

Once again, we see that the third tenant got a different response that the other two tenants!


Conclusion


Multitenancy allows you to protect your service using key security, it also enables you to configure different ACL and custom tenant configuration and your service will use that when these tenants invoke its APIs and finally it allows your service to even behave differently based on the tenant invoking its APIs; the same API returned 3 different message with 2 different response schemas when it was called using keys for Tenant 1, 2 & 3.