Пример #1
0
 this.error = function (err) {
   errors.respond(this.response, err);
 };
Пример #2
0
  this.run = function (req, resp) {
    
    // Set the 'currently processing req/resp vars to this pair -- these may
    // be used in the case of unhandled errors to hand the user back a 500
    geddy.request = req;
    geddy.response = resp;
    
    try {
      // capture the request start time for reporting
      resp.startTime = new Date().getTime();

      var url, base, qs, qsParams, method, params, cook, sess,
          constructor, controller, mixin, path, e, r
      
      // Let's start with the URL
      url = req.url;

      // Get the QS params, so we can check to see if there's a method override
      qs = geddy.util.url.getQS(url);
      qsParams = geddy.util.url.qsToObject(qs);
      
      // The method may be overridden by the _method param
      // TODO: Look for the x-http-method-override header
      method = (req.method.toUpperCase() == 'POST' && qsParams._method) ?
          qsParams._method : req.method;
      // Okay, let's be anal and force all the HTTP verbs to uppercase
      method = method.toUpperCase();
      
      // The base path -- the router doesn't need to know about QS params
      base = geddy.util.url.getBase(url);

      // =====
      // All the routing magic happens right here
      // =====
      params = geddy.router.first(base, method);      
      log.debug(method + ': ' + url);

      // The route matches -- we have a winner!
      if (params) {
        log.debug('Routed to ' + params.controller + ' controller, ' + params.action + ' action, ' + params.format + ' format');

        // Set up the cookies for this request so we can do the session thing
        cook = new cookies.CookieCollection(req);
        // Empty session object, ready to be initialized
        sess = new session.Session({
          app: this,
          request: req,
          cookies: cook
        });
        // Session init may involve async I/O (e.g., DB access, etc.)
        // so the actual action invocation and response happens
        // in the callback from session.init
        sess.init(function () {

          // Construct the full set of params from:
          // 1. Any request body 2. URL params 3. query-string params
          params = mergeParams(req, params, qsParams);
          log.debug('params: ' + JSON.stringify(params))

          // Instantiate the matching controller from the registry
          constructor = geddy.controllerRegistry[params.controller];
          // Give it all the base Controller fu
          constructor.prototype = new Controller({
            request: req,
            response: resp,
            name: params.controller,
            params: params,
            cookies: cook,
            session: sess
          });
          controller = new constructor();

          // Mix in any user-defined Application methods
          mixin = new geddy.controllerRegistry.Application();
          controller = geddy.util.meta.mixin(controller, mixin);
          
          // All righty, let's handle the action
          controller.handleAction(params.action, params);
        });

      }
      
      // Either static or 404 
      else {
        path = geddy.config.staticFilePath + base;
        fs.stat(path, function (err, stats) {
          // File not found, hand back the 404
          if (err) {
            e = new errors.NotFoundError('Page ' + req.url + ' not found.');
            r = new response.Response(resp);
            r.send(e.message, e.statusCode, {'Content-Type': 'text/html'});
            log.warn('ERROR: ' + req.url + ' not found.').flush();
          }
          // Otherwise it's a static file to serve
          else {
            r = new response.Response(resp);
            r.sendFile(path);
          }
        });
      }



    }
    catch (e) {
      errors.respond(resp, e);
    }

  };