Permalink
Browse files

explicit api host declaration

  • Loading branch information...
1 parent ba01e18 commit 9f9cf116c32a1fcf7ee635129c35aabc15551b9d @jairajs89 committed Mar 7, 2013
Showing with 31 additions and 25 deletions.
  1. +7 −5 apis.js
  2. +1 −1 package.json
  3. +11 −3 run.js
  4. +12 −16 zerver.js
View
12 apis.js
@@ -12,7 +12,8 @@ var ROOT_DIR = process.cwd(),
INSERT_APIS = '{{__API_APIS__}}',
INSERT_ROOT = '{{__API_ROOT__}}',
INSERT_API = '{{__API_OBJ__}}',
- INSERT_FUNCTIONS = '{{__API_FUNCTIONS__}}';
+ INSERT_FUNCTIONS = '{{__API_FUNCTIONS__}}',
+ API_HOST;
var setupComplete = false,
scriptTemplate = fs.readFileSync(__dirname + '/' + CLIENT_JS) + '',
@@ -24,14 +25,15 @@ var setupComplete = false,
-exports.setup = function (apiDir, refresh, logging) {
+exports.setup = function (apiHost, apiDir, refresh, logging) {
if (setupComplete) {
throw Error('apis can be setup only once');
}
setupComplete = true;
- REFRESH = refresh;
- LOGGING = logging;
+ REFRESH = refresh;
+ LOGGING = logging;
+ API_HOST = apiHost;
//TODO: validate apiDir
@@ -110,7 +112,7 @@ exports.getScript = function (apiRoot, apiName, apiHost, apiDir) {
isRefresh = (apiRoot === 'refresh'),
isAPI = (apiRoot in apiScripts);
- apiHost = apiHost || 'localhost:8888';
+ apiHost = API_HOST || apiHost || 'localhost:8888';
var script;
View
@@ -2,7 +2,7 @@
"author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
"name": "zerver",
"description": "client-integrated webapp server",
- "version": "0.8.1",
+ "version": "0.8.3",
"repository": {
"type": "git",
"url": "git://github.com/jairajs89/zerver.git"
View
14 run.js
@@ -6,7 +6,7 @@ var path = require('path'),
var ZERVER = __dirname + '/zerver',
WATCHER = __dirname + '/watcher',
PACKAGE = __dirname + '/package.json',
- ENV_MATCHER = /(\w+)\=(\w+)/g,
+ ENV_MATCHER = /([^\,]+)\=([^\,]+)/g,
API_DIR = 'zerver',
CWD = process.cwd(),
CHANGE_TIMEOUT = 1000,
@@ -16,7 +16,8 @@ var ZERVER = __dirname + '/zerver',
PRODUCTION = false,
VERBOSE = false,
PORT = process.env.PORT || 8888,
- MANIFESTS = [];
+ MANIFESTS = [],
+ API_HOST;
@@ -96,6 +97,9 @@ function processFlags () {
console.warn('[WARNING] ignoring invalid env port=' + m[2]);
}
break;
+ case 'host':
+ API_HOST = m[2];
+ break;
default:
console.warn('[WARNING] ignoring invalid env '+m[1]+'='+m[2]);
break;
@@ -148,6 +152,10 @@ function processFlags () {
PORT = port;
});
+ flags.add('host', function (host) {
+ API_HOST = host;
+ });
+
flags.add('zerver-dir', function (dir) {
API_DIR = dir;
});
@@ -185,7 +193,7 @@ function main () {
var death = false,
apiDir = CWD + '/' + API_DIR,
apiCheck = new RegExp('^' + CWD + '/' + API_DIR),
- args = [ PORT, API_DIR, (DEBUG ? '1' : '0'), (REFRESH ? '1' : '0'), (LOGGING ? '1' : '0'), (VERBOSE ? '1' : '0'), MANIFESTS.join(','), (PRODUCTION ? '1' : '0')],
+ args = [ PORT, API_DIR, (DEBUG ? '1' : '0'), (REFRESH ? '1' : '0'), (LOGGING ? '1' : '0'), (VERBOSE ? '1' : '0'), MANIFESTS.join(','), (PRODUCTION ? '1' : '0'), (API_HOST || '')],
opts = { cwd : CWD },
child;
View
@@ -46,6 +46,7 @@ var ROOT_DIR = process.cwd(),
API_DIR,
API_URL,
API_URL_LENGTH,
+ API_HOST,
API_SCRIPT_MATCH;
var memoryCache = {},
@@ -57,13 +58,13 @@ var memoryCache = {},
/* Run server */
-exports.middleware = function (apiDir, apiURL) {
- configureZerver(8888, apiDir, apiURL, false, false, false, false, '', false);
+exports.middleware = function (apiDir, apiURL, apiHost) {
+ configureZerver(8888, apiDir, apiURL, apiHost, false, false, false, false, '', false);
return handleMiddlewareRequest;
};
-exports.run = function (port, apiDir, debug, refresh, logging, verbose, manifests, production) {
- configureZerver(port, apiDir, apiDir, debug, refresh, logging, verbose, manifests, production);
+exports.run = function (port, apiDir, debug, refresh, logging, verbose, manifests, production, apiHost) {
+ configureZerver(port, apiDir, apiDir, apiHost, debug, refresh, logging, verbose, manifests, production);
app = http.createServer(handleRequest).listen(PORT);
@@ -103,11 +104,12 @@ exports.run = function (port, apiDir, debug, refresh, logging, verbose, manifest
console.log('');
};
-function configureZerver (port, apiDir, apiURL, debug, refresh, logging, verbose, manifests, production) {
+function configureZerver (port, apiDir, apiURL, apiHost, debug, refresh, logging, verbose, manifests, production) {
PORT = port;
API_DIR = apiDir;
API_URL = apiURL;
API_URL_LENGTH = apiURL.length;
+ API_HOST = apiHost;
DEBUG = debug;
PRODUCTION = production;
REFRESH = refresh;
@@ -163,7 +165,7 @@ function configureZerver (port, apiDir, apiURL, debug, refresh, logging, verbose
function fetchAPIs () {
apis = require(__dirname + '/apis');
- apis.setup(API_DIR, REFRESH, LOGGING);
+ apis.setup(API_HOST, API_DIR, REFRESH, LOGGING);
}
function updateLastModifiedTime () {
@@ -256,12 +258,13 @@ function prefetchManifestFile (pathname, callback) {
}
function handleRequest (request, response) {
- var urlParts = url.parse(request.url),
+ var urlParts = url.parse(request.url, true),
handler = {
request : request ,
response : response ,
pathname : url.resolve('/', decodeURI(urlParts.pathname)) ,
query : urlParts.search ,
+ params : urlParts.query ,
hash : urlParts.hash ,
referrer : request.headers['referrer'] || request.headers['referer'] ,
time : process.hrtime() ,
@@ -1006,17 +1009,10 @@ function APIRequest (handler, pathname) {
}
function getRequest (handler, api) {
- var params = {};
-
- try {
- params = url.parse(handler.query, true).query;
- }
- catch (err) {}
-
var lock = false;
try {
- api.call(handler, params, callback);
+ api.call(handler, handler.params, callback);
}
catch (err) {
if ( !lock ) {
@@ -1246,7 +1242,7 @@ function setupAutoRefresh () {
/* Run in debug mode */
if (require.main === module) {
- exports.run(parseInt(process.argv[2]), process.argv[3], (process.argv[4]==='1'), (process.argv[5]==='1'), (process.argv[6]==='1'), (process.argv[7]==='1'), process.argv[8], (process.argv[9]==='1'));
+ exports.run(parseInt(process.argv[2]), process.argv[3], (process.argv[4]==='1'), (process.argv[5]==='1'), (process.argv[6]==='1'), (process.argv[7]==='1'), process.argv[8], (process.argv[9]==='1'), (process.argv[10]||undefined));
if (DEBUG && REFRESH) {
setupAutoRefresh();

0 comments on commit 9f9cf11

Please sign in to comment.