Skip to content

Commit

Permalink
Merge pull request #292 from skrenek/master
Browse files Browse the repository at this point in the history
Proxy server support
  • Loading branch information
skrenek committed Sep 17, 2014
2 parents 6d745f2 + 2a43071 commit 01a5d4a
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.init = function(options, cb) {
states: {
initial: {
stateStartup: function() {
// - index the application's files and directories
// - index the application's files and directories
indexer.index(options, function(err, indexedFiles) {
if (err) fsm.fire("error", err); else {
cache.setItem("feather-files", indexedFiles);
Expand All @@ -37,14 +37,14 @@ exports.init = function(options, cb) {
fsm.fire("generateRestProxy", indexedFiles);
} else {
fsm.fire("parseFeatherFiles", indexedFiles, null);
}
}

} else {
fsm.fire("parsingComplete");
}
}
});
},
}
}
});
},
generateRestProxy: function(indexedFiles) {
restProxy.generateProxy({
appOptions: options,
Expand All @@ -69,12 +69,12 @@ exports.init = function(options, cb) {
//guarantee all files get counted in semaphore
process.nextTick(function() {
parser.parseFile({
path: _path,
path: _path,
fileMetaData: indexedFiles.featherFiles[_path],
request: {page: _path.replace(/.*\/public\/(.*)$/, "$1")} //need a dummy request object for parser since there is no real request at this point
}, function(err, result) {
if (err) throw new Error(JSON.stringify(err)); else {

//TODO: figure out gracefully re-publishing changed files w/ watchers... (commenting out for now as a placeholder)
// if (!indexedFiles.featherFiles[_path].watchingFile) { //only wire the watcher once
// fileWatcher.watchFileMtime(_path, function(args) {
Expand All @@ -86,7 +86,7 @@ exports.init = function(options, cb) {
sem.execute();
}
});
});
});
});
sem.execute();
},
Expand All @@ -110,7 +110,7 @@ exports.init = function(options, cb) {
if (filename.indexOf('feather-res-cache') === -1 && !!filename.match(/\.(feather|template)\.html$/)) {
logger.info({ message: "Processing " + filename + " due to a change", category: "feather.server" });
if (filename.match(/\.template.html$/)) { // widget template. re-parse all pages that use it.

// Re-read the template file and replace in the index.
fs.readFile(filename, "utf-8", function(fileErr, templateData) {
indexedFiles.templateFiles[filename] = {
Expand All @@ -125,7 +125,7 @@ exports.init = function(options, cb) {
process.nextTick(function() {
parser.parseFile({
forceReparse: true,
path: featherFilePath,
path: featherFilePath,
fileMetaData: featherFileMetaData,
request: {page: filename.replace(/.*\/public\/(.*)$/, "$1")} //need a dummy request object for parser since there is no real request at this point
}, function(err, result) {
Expand All @@ -139,7 +139,7 @@ exports.init = function(options, cb) {

process.nextTick(function() {
parser.parseFile({
path: filename,
path: filename,
forceReparse: true,
fileMetaData: indexedFiles.featherFiles[filename],
request: {page: filename.replace(/.*\/public\/(.*)$/, "$1")} //need a dummy request object for parser since there is no real request at this point
Expand All @@ -164,9 +164,9 @@ exports.init = function(options, cb) {
return fsm.states.complete; //the actual server bindings should only happen in the workers
} else {
return fsm.states.processCss;
}
}
}
},
},
processCss: {
stateStartup: function() {
if (options.resources.publish.compileToSingleCss) {
Expand Down Expand Up @@ -209,7 +209,7 @@ exports.init = function(options, cb) {
case "redis":
var RedisStore = require("connect-redis")(Connect);
var redisOptions = null;
// If providerOptions is specified, look at the server value.
// If providerOptions is specified, look at the server value.
// If not, look to see if redis.servers.session exists. Otherwise just use defaults.
if (options.connect.session.providerOptions && options.connect.session.providerOptions.server) {
redisOptions = options.redis.servers[options.connect.session.providerOptions.server];
Expand All @@ -223,7 +223,7 @@ exports.init = function(options, cb) {
getMiddleware(sessionStore);
break;

case "custom":
case "custom":
if (typeof options.getSessionStore === "function") {
options.getSessionStore(function(err, sessionStore) {
if (err) throw err;
Expand Down Expand Up @@ -256,26 +256,26 @@ exports.init = function(options, cb) {
_.each(_middleware, function(ware) {
server.use(ware);
});

// configure session path ignores
if (options.connect.session.ignorePaths && server.session) {
var si = options.connect.session.ignorePaths.length-1;
while (si >= 0) {
server.session.ignore.push(options.connect.session.ignorePaths[si]);
si -= 1;
}
}
}

//start listening
var port = options.port;
if (options.ssl && options.ssl.enabled && options.ssl.port) port = options.ssl.port;

if (options.ssl && options.ssl.enabled) {
server.httpServer = https.createServer(tlsOptions, server).listen(port);
} else {
server.httpServer = http.createServer(server).listen(port);
}

// now that ports have been bound we can change the process user and group
if (options.daemon.runAsDaemon == true && options.daemon.runAsUser) {

Expand Down Expand Up @@ -321,8 +321,10 @@ exports.init = function(options, cb) {
//do the redirect
res.statusCode = 302;
var host = options.host;
var port = options.ssl.clientRedirectPort || options.ssl.port;

//if ssl port is non-standard (443), make sure it gets included in the redirect url
host += options.ssl.port === 443 ? "" : ":" + options.ssl.port;
host += port === 443 ? "" : ":" + port;
res.setHeader("Location", "https://" + host + req.url);
res.end();
}
Expand All @@ -331,14 +333,14 @@ exports.init = function(options, cb) {
} else if (options.ssl.routes) {
//ssl is defined as only _enforced_ for a subset of routes (all routes MAY still use https, but the configured routes MUST use it),
//therefore we must create a full mirror server that has logic to force-redirect to ssl for specific routes

deferCompletion = true; //indicate final server setup needs to be deferred

//get another copy of the middleware stack for the mirror (cannot use shared stack as each server needs its own)
middleware.getMiddleware(options, function(err, __middleware, __rest) {
//stash the mirror's rest interface
cache.setItem("feather-rest-mirror", __rest);

//add the SSL route enforcement middleware module at the top of the new stack
__middleware.unshift(connectRouter(function(app) {
_.each(options.ssl.routes, function(route) {
Expand All @@ -349,33 +351,34 @@ exports.init = function(options, cb) {
//do the redirect
res.statusCode = 302;
var host = options.host;
var port = options.ssl.clientRedirectPort || options.ssl.port;
//if ssl port is non-standard (443), make sure it gets included in the redirect url
host += options.ssl.port === 443 ? "" : ":" + options.ssl.port;
host += port === 443 ? "" : ":" + port;
res.setHeader("Location", "https://" + host + req.url);
res.end();
});
});
});
});
}));

//spin up mirror and complete server setup
mirror = Connect();
_.each(__middleware, function(ware) {
mirror.use(ware);
});

mirror.httpServer = http.createServer(mirror).listen(options.port);
completeServerSetup();

completeServerSetup();
});
}
}

if (!deferCompletion) completeServerSetup();
}
});
}

},
complete: function() {
return this.states.complete;
Expand All @@ -394,5 +397,5 @@ exports.init = function(options, cb) {
}
}
}
});
});
};

0 comments on commit 01a5d4a

Please sign in to comment.