Skip to content

Commit

Permalink
feat: Implement methods to get static and template, tempate image
Browse files Browse the repository at this point in the history
- Add methods in WebAdapter
  • Loading branch information
tcnguyen committed Mar 14, 2018
1 parent 84f1a08 commit bfa015f
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions packages/botfuel-dialog/src/adapters/web-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ const express = require('express');
const exphbs = require('express-handlebars');
const rp = require('request-promise-native');
const bodyParser = require('body-parser');
const url = require('url');
const querystring = require('querystring');
const logger = require('logtown')('WebAdapter');
const MissingImplementationError = require('../errors/missing-implementation-error');
const Adapter = require('./adapter');

// absolute urls to static and template folders
const PORT = process.env.PORT || process.env.BOTFUEL_ADAPTER_PORT || 5000;
const BOT_URL = process.env.BOT_URL || `http://localhost:${PORT}`;
const STATIC_BASE_URL = url.resolve(BOT_URL, 'static/');
const TEAMPLATE_BASE_URL = url.resolve(BOT_URL, 'templates/');

// screenshot service url
const SCREENSHOT_SERVICE_URL = 'https://botfuel-screenshot-service.herokuapp.com/';

/**
* Generic web adapter (to be subclassed), it serves:
* - /webhook : requests to the bot
Expand All @@ -41,8 +52,7 @@ class WebAdapter extends Adapter {
app.set('view engine', 'handlebars');
app.set('views', 'src/templates');
this.createRoutes(app);
const port = process.env.PORT || process.env.BOTFUEL_ADAPTER_PORT || 5000;
app.listen(port, () => logger.info('run: listening on port', port));
app.listen(PORT, () => logger.info('run: listening on port', PORT));
}

/**
Expand Down Expand Up @@ -137,6 +147,52 @@ class WebAdapter extends Adapter {
getBody() {
throw new MissingImplementationError();
}

/**
* Get absolute url for resource inside static folder
* @async
* @param {String} resourcePath - resource path relative to static, ex: images/logo.png
* @returns {null}
*/
static getStaticUrl(resourcePath) {
logger.debug('getStaticUrl', resourcePath);
return url.resolve(STATIC_BASE_URL, resourcePath);
}

/**
* Get absolute path for template
* @async
* @param {String} templateName - handlebars template name
* @param {Object} params - parameters to be passed to the template
* @returns {null}
*/
static getTemplateUrl(templateName, params) {
logger.debug('getTemplateUrl', templateName, params);
const templateUrl = url.resolve(TEAMPLATE_BASE_URL, templateName);
return `${templateUrl}?${querystring.stringify(params)}`;
}

/**
* Get absolute path for the screenshot template image
* @async
* @param {String} templateName - handlebars template name
* @param {Object} params - parameters to be passed to the template
* @param {Number} width - image width
* @param {Number} height - image height
* @param {Number} quality - image quality
* @returns {null}
*/
static getTemplateImageUrl(templateName, params, width = 800, height = 600, quality = 100) {
const templateUrl = getTemplateUrl(templateName, params);
const screenshotParams = {
url: templateUrl,
quality,
width,
height,
};

return `${SCREENSHOT_SERVICE_URL}?${querystring.stringify(screenshotParams)}`;
}
}

module.exports = WebAdapter;

0 comments on commit bfa015f

Please sign in to comment.