This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.js
138 lines (100 loc) · 3.31 KB
/
bootstrap.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var async = require('async')
, _ = require('lodash')
, glob = require('glob')
, path = require('path')
, static = require('serve-static')
, util = require('util')
, Waterline = require('waterline')
, orm = new Waterline()
, config = require('./config');
module.exports = function (app, callback) {
var pipeline = [];
// Load Controllers
pipeline.push(function (next) {
var cwd = config.paths.controllers
, controllers = {};
glob('*.js', { cwd: cwd }, function (err, files) {
if (err) return next(err);
_.each(files, function (file) {
var name = path.basename(file, '.js').toLowerCase();
controllers[name] = require( path.join(cwd, file) );
});
next(null, controllers);
});
}); // END Load Controllers
// Bind Routes
pipeline.push(function (controllers, next) {
_.each(config.routes, function (val, key) {
var route = key.split(' ')
, routeOptions = val.split('.')
, type = route[0].toLowerCase()
, path = route[1]
, controllerName = _.trim(routeOptions[0].toLowerCase())
, actionName = _.trim(routeOptions[1]) || 'index';
if (!controllers[controllerName]) {
return console.error(
util.format('"%s" Controller Not Found', controllerName)
);
}
if (!controllers[controllerName][actionName]) {
return console.error(
util.format('Action "%s" Not Found in "%s" Controller', actionName, controllerName)
);
}
app[type](path, controllers[controllerName][actionName]);
});
next(null);
}); // END Bind Routes
// Load Models
pipeline.push(function (next) {
var cwd = config.paths.models;
glob('*.js', { cwd: cwd }, function (err, files) {
if (err) return next(err);
_.each(files, function (file) {
var name = path.basename(file, '.js').toLowerCase()
, module = require( path.join(cwd, file) )
, data = _.extend({
identity: name,
connection: config.models.connection
}, module)
, model = Waterline.Collection.extend(data);
orm.loadCollection(model);
});
next(null);
});
}); // END Load Models
// Bind Default Routes
pipeline.push(function (next) {
app.use(static(config.paths.public));
app.use(function(req, res){
res.status(404);
if (req.accepts('html')) return res.render('404', {title: '404 Not Found'});
if (req.accepts('json')) return res.send({error: 'Not Found'});
res.type('txt').send('Not Found');
});
next(null);
}); // END Bind Default Routes
// Initialize ORM
pipeline.push(function (next) {
orm.initialize({
connections: config.connections,
adapters: {
'sails-mongo': require('sails-mongo')
},
defaults: {
migrate: config.models.migrate
}
}, function(err, models) {
if(err) return next(err);
_.each(models.collections, function (model) {
var modelName = _.upperFirst(model.identity);
GLOBAL[modelName] = model;
});
next(null);
});
}); // END Initialize ORM
async.waterfall(pipeline, function (err) {
if (err) console.error(err);
callback();
});
}