Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: vhost router multi worker DEPRECATED #43

Open
frank-dspeed opened this issue Nov 26, 2017 · 0 comments
Open

Example: vhost router multi worker DEPRECATED #43

frank-dspeed opened this issue Nov 26, 2017 · 0 comments

Comments

@frank-dspeed
Copy link
Member

/*
	Usage Example:
dssrv.vhostrouter = require(express-domainrouter')
// Optain vhost config
dssrv.vhostrouter.locals.vhosts = worker.options.vhosts || [ 'default' ]
dssrv.vhostrouter.locals.vhosts.forEach(function(domain) {
    dssrv.debug('vhosts Registered: %s , %s', domain , 'replace')
    // dssrv.vhostrouter.locals.hostDictionary[domain] = replace
})
// dssrv.debug('vhosts Registered: %s , %s', 'default' , 'videoreplace')
// var apiAPP = require(worker.options.expressApps.bla.app)(worker.options.expressApps.bla.config)
// dssrv.vhostrouter.locals.hostDictionary['domain'] = apiAPP
*/


var express = require('express')
var vhostrouter = express('dssrv:worker:'+process.pid+':vhosts');
vhostrouter.locals.hostDictionary = vhostrouter.locals.hostDictionary || {}
vhostrouter.use(
	function replaceDevHosts(req, res, next) {
		// replace dev test hosts
		req.headers.originalHost = req.headers.Host || req.headers.host
		req.headers.Host = req.headers.originalHost.replace('.new','')
		req.headers.host = req.headers.originalHost.replace('.new','')
		req.headers.Host = req.headers.originalHost.replace('.local','')
		req.headers.host = req.headers.originalHost.replace('.local','')
		next()
	}, function vhost(req, res, next){
		  var debug = require('debug')('dssrv:worker:'+process.pid+':Vhost');
		  debug('VHOSTS EXEC: ' + JSON.stringify(req.headers))
		  // debug('VHOSTS EXEC: ' + JSON.stringify(req.app.locals.hostDictionary))
		  // console.log(req.app.locals.hostDictionary)
		  if (!req.headers.host) return next();
		  var host = req.headers.host.split(':')[0];
		  if (req.trustProxy && req.headers["x-forwarded-host"]) {
		    host = req.headers["x-forwarded-host"].split(':')[0];
		  }
		  var server = req.app.locals.hostDictionary[host];
		  if (!server){
		    server = req.app.locals.hostDictionary['*' + host.substr(host.indexOf('.'))];
		  }

		  if (!server){ 
		    server = req.app.locals.hostDictionary['default'];
		  }

		  // console.log(JSON.stringify(Vhost.hostDictionary))
		  // console.log()

		  if (!server) return next();
		  if ('function' == typeof server) return server(req, res, next);
		  // server.emit('request', req, res);
		  next()
	}
);

module.exports = vhostrouter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant