Permalink
Browse files

added www server

  • Loading branch information...
caolan committed Apr 9, 2013
1 parent 4f5eb52 commit f2daf13a1d3238087b64e8e299dd09f8fb1c751c
Showing with 301 additions and 578 deletions.
  1. +34 −0 bin/start
  2. +43 −0 lib/app.js
  3. +107 −0 lib/config.js
  4. +0 −317 lib/hoodie-app.js
  5. +0 −35 lib/hoodie-installer.js
  6. +0 −36 lib/hoodie-log.js
  7. +0 −168 lib/hoodie-server.js
  8. +32 −0 lib/servers/loggers.js
  9. +38 −0 lib/servers/www.js
  10. +23 −0 lib/utils.js
  11. +24 −22 package.json
View
@@ -0,0 +1,34 @@
+#!/usr/bin/env node
+
+/* vim: set filetype=javascript : */
+
+/**
+ * Dependencies
+ */
+
+var config = require('../lib/config'),
+ app = require('../lib/app');
+
+
+/**
+ * Start a Hoodie server configured for the current
+ * platform and environment
+ */
+
+if (require.main === module) {
+ var project_dir = process.cwd();
+
+ var cfg = config.getConfig(
+ process.platform, // platform
+ process.env, // environment vars
+ project_dir // project directory
+ );
+
+ app.init(cfg, function (err) {
+ if (err) {
+ console.error(err);
+ process.exit(1);
+ }
+ console.log('Ready!');
+ });
+}
View
@@ -0,0 +1,43 @@
+/**
+ * Initializes directories, installs dependencies then starts all
+ * configured servers for the Hoodie application
+ */
+
+var path = require('path'),
+ async = require('async'),
+ utils = require('./utils'),
+ www = require('./servers/www');
+
+
+/**
+ * Initializes and starts a new Hoodie app server
+ */
+
+exports.init = function (config, callback) {
+ console.log('Initializing...');
+ exports.ensurePaths(config, function (err) {
+ if (err) {
+ return callback(err);
+ }
+ console.log('Starting www server');
+ www.start(config, function (err) {
+ if (err) {
+ return callback(err);
+ }
+ callback();
+ });
+ });
+};
+
+/**
+ * Makes sure the appropriate app directories exists
+ */
+
+exports.ensurePaths = function (config, callback) {
+ var paths = [
+ config.hoodie_path,
+ config.apps_path,
+ path.join(config.apps_path, config.app.name)
+ ];
+ async.map(paths, utils.ensureDir, callback);
+};
View
@@ -0,0 +1,107 @@
+/**
+ * This module returns appropriate config values for the provided
+ * platform, environment and project. It is used directly by the start
+ * script in the bin directory.
+ */
+
+var fs = require('fs'),
+ path = require('path'),
+ _ = require('underscore');
+
+
+/**
+ * Returns all config values for current environment
+ */
+
+exports.getConfig = function (platform, env, project_dir) {
+ // location of project's package.json
+ var pkgfile = path.resolve(project_dir, 'package.json');
+
+ // default platform-agnostic config
+ var config = {
+ project_dir: project_dir,
+ //host: '0.0.0.0',
+ app: JSON.parse(fs.readFileSync(pkgfile)),
+ domain: 'dev',
+ run_local_tld: false,
+ run_couch: true,
+ www_server: {
+ host: '127.0.0.1',
+ port: 8888
+ }
+ };
+
+ // add CouchDB paths to config
+ config = _.extend(config, exports.getCouch(env));
+ // add Hoodie paths to config
+ config = _.extend(config, exports.getHoodie(platform));
+
+ // do magic firewall stuff for .dev domains on mac
+ if (platform === 'darwin') {
+ config.run_local_tld = true;
+ }
+
+ // Nodejitsu config
+ if (exports.isNodejitsu(env)) {
+ config.run_couch = false;
+ config.domain = 'jit.su';
+ }
+
+ return config;
+}
+
+/**
+ * Attempts to detect a Nodejitsu environment
+ */
+
+exports.isNodejitsu = function (env) {
+ return !!(env.SUBDOMAIN);
+}
+
+/**
+ * Find CouchDB locations
+ */
+
+exports.getCouch = function (env) {

This comment has been minimized.

Show comment Hide comment
@janl

janl Apr 11, 2013

maybe use isaacs/node-which here?

@janl

janl Apr 11, 2013

maybe use isaacs/node-which here?

+ var cfg = {};
+ // check filesystem for likely couch paths
+ if (fs.existsSync('/usr/local/bin/couchdb')) {
+ cfg.couch_bin = '/usr/local/bin/couchdb';
+ cfg.couch_default_ini = '/usr/local/etc/couchdb/default.ini';
+ }
+ else if (fs.existsSync('/usr/bin/couchdb')) {
+ cfg.couch_bin = '/usr/bin/couchdb';
+ cfg.couch_default_ini = '/etc/couchdb/default.ini';
+ }
+ // override if environment vars set
+ if (env.COUCH_BIN) {
+ cfg.couch_bin = env.COUCH_BIN;
+ }
+ if (env.COUCH_DEFAULT_INI) {
+ cfg.couch_defualt_ini = env.COUCH_DEFAULT_INI;
+ }
+ return cfg;
+}
+
+/**
+ * Find Hoodie locations
+ */
+
+exports.getHoodie = function (platform, env) {
+ var home = process.env.HOME,
+ cfg = {};
+
+ if (process.platform === 'darwin') {

This comment has been minimized.

Show comment Hide comment
@janl

janl Apr 11, 2013

Just as a note:

I could have picked ~/.hoodie for Mac OS X as well, but given that we store db data in there, I put it into ~/Library as Mac usuers generally don’t expect dot file-dirs to fill up a lot.

We still might want to revisit this at some point and do ~/.hoodie on all unixes

@janl

janl Apr 11, 2013

Just as a note:

I could have picked ~/.hoodie for Mac OS X as well, but given that we store db data in there, I put it into ~/Library as Mac usuers generally don’t expect dot file-dirs to fill up a lot.

We still might want to revisit this at some point and do ~/.hoodie on all unixes

+ cfg.hoodie_path = path.resolve(home, "Library/Hoodie");
+ cfg.apps_path = path.resolve(cfg.hoodie_path, 'Apps');
+ return cfg;
+ }
+ else if (process.platform === 'linux') {
+ cfg.hoodie_path = path.resolve(home, '.hoodie');
+ cfg.apps_path = path.resolve(cfg.hoodie_path, 'apps');
+ return cfg;
+ }
+ else {
+ throw new Error('Unsuported platform: ' + process.platform);
+ }
+}
Oops, something went wrong.

0 comments on commit f2daf13

Please sign in to comment.