Skip to content

Commit

Permalink
Issue #SB-21295 merge: Merge pull request #27 from JagadishPujari/SB-…
Browse files Browse the repository at this point in the history
…21295

Issue #SB-21295 feat:  Menu configuration deploy to blob refresh API to update config
  • Loading branch information
vinukumar-vs authored Jan 8, 2021
2 parents 1732e16 + 6e642dc commit 57dd170
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions helm_charts/router/values.j2
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ routerenv:
WHATSAPP_SECRET_KEY: "{{sunbird_whatsapp_secret_key | default("''")}}"
WHATSAPP_AUTH_TOKEN: "{{sunbird_whatsapp_auth_token | default("''")}}"
WHATSAPP_SOURCE: "{{sunbird_whatsapp_source | default("''")}}"
CONFIG_BLOB_PATH: "{{sunbird_public_storage_account_name}}"
51 changes: 51 additions & 0 deletions router/appRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var LOG = require('./log/logger')
var literals = require('./config/literals')
var config = require('./config/config')
var chatflow = require('./config/chatflow')
var reload = require('require-reload')(require),
chatflow = reload('./config/chatflow');
literals = reload('./config/literals');
var RasaCoreController = require('./controllers/rasaCoreController')
const telemetry = require('./api/telemetry/telemetry.js')
var UUIDV4 = require('uuid')
Expand Down Expand Up @@ -88,6 +91,54 @@ appBot.post('/whatsapp', function (req, res) {

})

// Update latest config from blob
appBot.post('/refresh', function(req, response) {
if(config.CONFIG_BLOB_PATH) {
var domian = "https://" + config.CONFIG_BLOB_PATH + ".blob.core.windows.net"
var url = domian + '/chatbot/router/config/'
var dest = 'router/config/'

var literalsStr = 'literals.js';
var chatflowStr = 'chatflow.js';
updateConfigFromBlob(url, dest, literalsStr, function(){
try {
literals = reload('./config/literals');
updateConfigFromBlob(url, dest, chatflowStr, function(){
try {
chatflow = reload('./config/chatflow');
response.send({'msg': 'Configuration is updated successfully for chatflow and literals!!!'})
} catch (e) {
//if this threw an error, the api variable is still set to the old, working version
console.error("Failed to reload chatflow.js! Error: ", e);
response.send({'msg': e.message})
}
})
} catch (e) {
//if this threw an error, the api variable is still set to the old, working version
console.error("Failed to reload literals.js! Error: ", e);
response.send({'msg': e.message})
}
})
} else {
response.send({'msg': 'ENV configuration blob path is not defined'})
}
})

var updateConfigFromBlob = function(url, dest, configName, cb) {
url = url + configName;
dest = dest + configName;
var file = fs.createWriteStream(dest);
var request = https.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};

function handler(req, res, data) {


Expand Down
3 changes: 3 additions & 0 deletions router/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ config.UNKNOWN_OPTION_FREEFLOW = 'unknown_option_freeFlow'
config.UNKNOWN_OPTION = 'unknown_option'

config.TELEGRAM_BOT_ENDPOINT = 'https://api.telegram.org/bot<bot_secret_key>/sendMessage'

config.CONFIG_BLOB_PATH = env.CONFIG_BLOB_PATH

module.exports = config;
2 changes: 2 additions & 0 deletions router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dateformat": "^3.0.3",
"elasticsearch": "^16.6.0",
"express": "^4.17.1",
"fs": "0.0.1-security",
"helmet": "^3.21.2",
"lodash": "^4.17.20",
"method-override": "^3.0.0",
Expand All @@ -23,6 +24,7 @@
"sb_logger_util": "file:libs/sb_logger_util",
"redis": "^3.0.2",
"request": "^2.88.0",
"require-reload": "^0.2.2",
"socket.io": "^2.3.0",
"tz-lookup": "^6.1.25",
"uuid": "^3.4.0",
Expand Down

0 comments on commit 57dd170

Please sign in to comment.