Beispiel #1
0
module.exports = ooj.Class({
  construct: function(app) {
    this.app = app,
    this.configMap = {},
    this.configuration = {};
  },

  set: function(configFor, configFn) {
    if (!this.configMap[configFor]) {
      this.configMap[configFor] = [];
    }
    this.configMap[configFor].push(configFn);
  },

  _loadConfiguration: function(forEnv) {
    var self = this;
    if (this.configMap[forEnv]) {
      this.configMap[forEnv].forEach(function(envConfigFn) {
        (function(fn) {
          var config = {};
          fn.call({}, config);
          tools.merge(self.configuration, config);
        })(envConfigFn);
      });
    }
  },

  _configure: function(forEnv) {
    this._loadConfiguration("application");
    this._loadConfiguration(forEnv);
    this.app.set("port", this.configuration.port || process.env.PORT || 3000);
    if (this.configuration["view engine"]) {
      this.app.set("views", path.join(this.app.trees.applicationRoot, "app", "views"));
      this.app.set("view engine", this.configuration["view engine"]);
    }
    if (this.configuration.favicon && this.configuration.favicon === true)
      this.app.use(express.favicon());
    if (this.configuration.logger)
      this.app.use(express.logger(this.configuration.logger));
    if (this.configuration.compress && this.configuration.compress === true) { // Verify it's actually set
      this.app.use(express.compress());
    }
    if (this.configuration.cookies) {
      this.app.use(express.cookieParser(this.configuration.cookies));
    }
    if (this.configuration.cookies && this.configuration.sessions && this.configuration.sessions === true) {
      this.app.use(express.session());
    }
    if (!(this.configuration.bodyParser && this.configuration.bodyParser === false)) { // has to be configured off
      this.app.use(express.bodyParser());
    }
    this.app.use(express.methodOverride());
    this.app.use(this.app.router);
    this.app.use(express.static(path.join(this.app.trees.applicationRoot, "public")));
    if (this.configuration.errorHandler && this.configuration.errorHandler === true)
      this.app.use(express.errorHandler);
  },

  _loadConfigFiles: function() {
    require(path.join(this.app.trees.applicationRoot, "config", "application"));
    requireAll(path.join(this.app.trees.applicationRoot, "config", "environments"));
  }
});
Beispiel #2
0
module.exports = ooj.Class({
  construct: function(app) {
    var ctrlDir, ctrl;
    this.app = app;
    ctrlDir = path.join(this.app.trees.applicationRoot, "app", "controllers");
    this.controllers = requireAll(ctrlDir);
    this._configureViewPaths(this.controllers);
  },

  generateHandler: function(handler) {
    var ctrlKey, action, Controller;
    if (typeof handler === "string") {
      var temp = handler.split("#");
      if (temp.length !== 2)
        throw "Invalid handler string given to route handler";
      ctrlKey = temp[0];
      action = temp[1];
    }
    else if (handler.controller && handler.action) {
      ctrlKey = handler.controller;
      handler = handler.action;
    }
    else {
      throw "Invalid handler given to route handler";
    }

    Controller = this.controllers;

    ctrlKeys = ctrlKey.split("/");

    ctrlKeys.forEach(function(key) {
      Controller = Controller[key];
    });

    return function(req, res, next) {
      var viewPath, ctrlName = ctrlKeys[ctrlKeys.length - 1],
          ctrl = new Controller(ctrlName, action, req, res, next);
      ctrl[action]();
      if (!ctrl._completed && !res._headerSent) {
        ctrl.render(action);
      }
    };
  },

  _configureViewPaths: function(base, baseKey) {
    var ctrlKey, ctrl, basePath;
    baseKey = baseKey || "";
    for (ctrlKey in base) {
      ctrl = base[ctrlKey];
      if (baseKey.length === 0)
        basePath = ctrlKey;
      else
        basePath = [baseKey, "/", ctrlKey].join("");
      if (typeof ctrl === "function") {
        ctrl.prototype._viewPath = basePath;
      }
      else if (typeof ctrl === "object") {
        this._configureViewPaths(ctrl, basePath);
      }
    }
  }
});