-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrig.js
More file actions
112 lines (99 loc) · 4.22 KB
/
rig.js
File metadata and controls
112 lines (99 loc) · 4.22 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*jslint nomen: true */
var path = require('path'),
Config = require('./lib/config'),
Router = require('./lib/router'),
Registry = require('./lib/registry'),
HBAdapter = require('./lib/hb-adapter'),
express = require('express'),
/**
* @constructor
* @param {Object} options
* @param {String} options.config the path of the json file containing the configurations
* @param {String} options.routes the path of the json file containing the routing configs
*/
Rig = function (options) {
'use strict';
var that = this,
cwd = process.cwd(),
staticPath,
templateEngine;
this.app = express();
// register all the middlewares
Rig.registry = new Registry(new Config(path.resolve(cwd, options.config)));
Rig.registry.register(path.resolve(__dirname, 'middleware'));
// special case for static relative path
staticPath = Rig.registry.getConfig('middleware.static');
if (staticPath) {
staticPath = path.resolve(cwd, staticPath);
}
// special case for hb-adapter
Rig.registry.register('lib.hb-adapter', HBAdapter);
Rig.registry.register({
'middleware.router' : function () {return that.app.router; },
'middleware.static' : express.static.bind(null, staticPath),
'middleware.logger' : express.logger,
'middleware.query' : express.query,
'middleware.bodyParser' : express.bodyParser,
'middleware.cookieParser' : express.cookieParser,
'middleware.session' : express.session,
'middleware.csrf' : express.csrf,
'middleware.errorHandler' : express.errorHandler,
'middleware.compress' : express.compress,
'middleware.basicAuth' : express.basicAuth,
'middleware.json' : express.json,
'middleware.urlencoded' : express.urlencoded,
'middleware.multipart' : express.multipart,
'middleware.timeout' : express.timeout,
'middleware.cookieSession' : express.cookieSession,
'middleware.methodOverride': express.methodOverride,
'middleware.responseTime' : express.responseTime,
'middleware.staticCache' : express.staticCache,
'middleware.directory' : express.directory,
'middleware.vhost' : express.vhost,
'middleware.favicon' : express.favicon,
'middleware.limit' : express.limit
});
this.router = new Router({
registry: Rig.registry,
routes : options.routes
});
templateEngine = Rig.registry.get(options.templateEngine || 'lib.hb-adapter');
if (templateEngine) {
this.app.engine('.html', templateEngine);
}
// If we have the resources, we're good to go!
if (options.resources) {
if (!(options.resources instanceof Array)) {
throw new Error('The the Rig constructor expects an Array in the "resource" parameter.');
}
options.resources.map(this.register);
this.route();
}
};
Rig.prototype.engine = function () {
'use strict';
this.app.engine.apply(this.app, arguments);
};
/**
* Configures and stores a resource into the registry
* @param {String} name the reference under which to register the resource. Will be used to lookup for configurations
* @param {Function} configurable a function that takes a configuration object and returns the resource to be registered
*/
Rig.prototype.register = function (name, configurable) {
'use strict';
Rig.registry.register(name, configurable);
};
/**
* Configures the app to route specific requests to specific middlewares according to the route config file
*/
Rig.prototype.route = function () {
'use strict';
console.log('[Rig] Mapping routes with registered resources:');
console.log(Object.keys(Rig.registry.get()));
this.router.map(this.app);
};
Rig.prototype.listen = function () {
'use strict';
this.app.listen.apply(this.app, arguments);
};
module.exports = Rig;