add mailer
This commit is contained in:
		
							parent
							
								
									52a6627e84
								
							
						
					
					
						commit
						648c136fcf
					
				
							
								
								
									
										74
									
								
								lib/apis.js
									
									
									
									
									
								
							
							
						
						
									
										74
									
								
								lib/apis.js
									
									
									
									
									
								
							@ -11,6 +11,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
 | 
			
		||||
  // TODO xconfx.apispath
 | 
			
		||||
  xconfx.restPath = path.join(__dirname, '..', '..', 'packages', 'rest');
 | 
			
		||||
  xconfx.appApiGrantsPath = path.join(__dirname, '..', '..', 'packages', 'client-api-grants');
 | 
			
		||||
  xconfx.appConfigPath = path.join(__dirname, '..', '..', 'var');
 | 
			
		||||
 | 
			
		||||
  function notConfigured(req, res) {
 | 
			
		||||
    var msg = "api package '" + req.pkgId + "' not configured for client uri '" + req.experienceId + "'"
 | 
			
		||||
@ -54,6 +55,12 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function getSiteConfig(clientUrih) {
 | 
			
		||||
    return fs.readFileAsync(path.join(xconfx.appConfigPath, clientUrih + '.json'), 'utf8').then(function (text) {
 | 
			
		||||
      return JSON.parse(text);
 | 
			
		||||
    }).then(function (data) { return data; }, function (/*err*/) { return {}; });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function loadRestHelper(myConf, pkgId) {
 | 
			
		||||
    var pkgPath = path.join(myConf.restPath, pkgId);
 | 
			
		||||
 | 
			
		||||
@ -229,30 +236,55 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
 | 
			
		||||
          return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (localCache.rests[pkgId]) {
 | 
			
		||||
          localCache.rests[pkgId].handler(req, res, next);
 | 
			
		||||
          hasBeenHandled = true;
 | 
			
		||||
        return getSiteConfig(clientUrih).then(function (siteConfig) {
 | 
			
		||||
          /*
 | 
			
		||||
          Object.defineProperty(req, 'siteConfig', {
 | 
			
		||||
            enumerable: true
 | 
			
		||||
          , configurable: false
 | 
			
		||||
          , writable: false
 | 
			
		||||
          , value: siteConfig
 | 
			
		||||
          });
 | 
			
		||||
          */
 | 
			
		||||
          Object.defineProperty(req, 'getSiteMailer', {
 | 
			
		||||
            enumerable: true
 | 
			
		||||
          , configurable: false
 | 
			
		||||
          , writable: false
 | 
			
		||||
          , value: function getSiteMailer() {
 | 
			
		||||
              var nodemailer = require('nodemailer');
 | 
			
		||||
              var transport = require('nodemailer-mailgun-transport');
 | 
			
		||||
              //var mailconf = require('../../../com.daplie.mailer/config.mailgun');
 | 
			
		||||
              var mailconf = siteConfig['mailgun.org'];
 | 
			
		||||
              var mailer = PromiseA.promisifyAll(nodemailer.createTransport(transport(mailconf)));
 | 
			
		||||
 | 
			
		||||
          if (now - localCache.rests[pkgId].createdAt > staleAfter) {
 | 
			
		||||
            localCache.rests[pkgId] = null;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!localCache.rests[pkgId]) {
 | 
			
		||||
          //return doesThisPkgExist
 | 
			
		||||
 | 
			
		||||
          return loadRestHandler(xconfx, pkgId).then(function (myHandler) {
 | 
			
		||||
            if (!myHandler) {
 | 
			
		||||
              notConfigured(req, res);
 | 
			
		||||
              return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now };
 | 
			
		||||
            if (!hasBeenHandled) {
 | 
			
		||||
              myHandler.handler(req, res, next);
 | 
			
		||||
              return mailer;
 | 
			
		||||
            }
 | 
			
		||||
          });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
          if (localCache.rests[pkgId]) {
 | 
			
		||||
            localCache.rests[pkgId].handler(req, res, next);
 | 
			
		||||
            hasBeenHandled = true;
 | 
			
		||||
 | 
			
		||||
            if (now - localCache.rests[pkgId].createdAt > staleAfter) {
 | 
			
		||||
              localCache.rests[pkgId] = null;
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          if (!localCache.rests[pkgId]) {
 | 
			
		||||
            //return doesThisPkgExist
 | 
			
		||||
 | 
			
		||||
            return loadRestHandler(xconfx, pkgId).then(function (myHandler) {
 | 
			
		||||
              if (!myHandler) {
 | 
			
		||||
                notConfigured(req, res);
 | 
			
		||||
                return;
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now };
 | 
			
		||||
              if (!hasBeenHandled) {
 | 
			
		||||
                myHandler.handler(req, res, next);
 | 
			
		||||
              }
 | 
			
		||||
            });
 | 
			
		||||
          }
 | 
			
		||||
        });
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user