summaryrefslogtreecommitdiff
path: root/app/node_modules/okserver/index.js
blob: 3428565653b074c81fde793229b21e7e086da90d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var specificity = require('route-order')();
var slash = require('express-slash');

function OKServer(options) {
  if (!(this instanceof OKServer)) return new OKServer(options);
  options = options || {};
  if (!options.express)
    throw new Error('No Express provided to OKServer');
  if (!options.app)
    throw new Error('No Express app provided to OKServer')
  if (!options.views)
    throw new Error('No views provided to OKServer');
  if (!options.root)
    throw new Error('No app root directory provided to OKServer');
  if (!options.adminRoot)
    throw new Error('No admin root directory provided to OKServer');
  if (!options.adminPath)
    throw new Error('No admin path provided to OKServer');
  if (!options.errorHandler)
    throw new Error('No error handler provided to OKServer');
  var root = options.root;
  var adminRoot = options.adminRoot;
  var adminPath = options.adminPath;
  var views = options.views;
  var express = options.express;
  var app = this._app = options.app;
  var router = express.Router({
    strict: app.get('strict routing')
  });
  var error = options.errorHandler;
  var services = options.services || {};
  Object.keys(views)
    // Sort such that more general routes are matched last
    .sort(specificity)
    // Add the views
    .forEach(function(route) {
      var view = views[route];
      var mount = view.mount;
      if (!mount)
        throw Error('View doesn\'t specify a mount point');
      var handler = view.middleware();
      if (!handler)
        throw new Error('View doesn\'t provide middleware');
      router[mount](route, handler);
    });

  /**
   * Mount our middleware.
   * Order is important here! Requests go down the middleware
   * chain until they are handled with a response, which could
   * happen anywhere in the chain. Watch out for middleware shadowing
   * other middleware.
   */

  // Serve user static files
  app.use(express.static(root));
  // Serve admin interface static files
  app.use(adminPath, express.static(adminRoot));
  // Application router
  app.use(router);
  // Add services
  if (services.s3) {
    app.use('/_services/s3', services.s3.middleware());
  }
  if (services.twitter) {
    app.use('/_services/twitter', services.twitter.middleware())
  }
  // Make sure this lady is last. Checks whether the desired
  // route has a trailing-slash counterpart and redirects there
  app.use(slash());
  // Otherwise it's a 404
  app.use(function(req, res) {
    error(req, res, 404)(new Error('No matching route'));
  });
}

OKServer.prototype.listen = function listen(port) {
  this._app.listen(port || 1337);
  return this;
};

module.exports = OKServer;