-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
135 lines (120 loc) · 3.97 KB
/
index.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
// Require External Packages
// =========================
var _ = require('lodash');
var VError = require('verror');
var nunjucks = require('nunjucks');
var debug = require('debug')('users:router');
module.exports = function (options) {
options = options || {};
var store;
var sanitizer;
var passwordChecker;
var passport;
var fixtures;
_.defaults(options, {
// default data to load
data: [],
// Where to find views
// this can be an array of views.
//
views: [],
// When using default template engine (Nunjunks), should view cache be enabled.
// Default is false to ease development.
cache: false,
store: null,
sanitizer: null,
passwordChecker: null,
passport: null
});
// prepare view related configuration
if (!options.views) {
options.views = [];
} else if (!_.isArray(options.views)) {
options.views = [options.views];
}
// alway fallback on default templates
options.views.push(__dirname + '/views');
// prepare store configuration
store = options.store;
if (!store && options.nedb) {store = 'nedb';}
if (!store && options.mongo) {store = 'mongo';}
switch (store) {
case 'memory':
store = require('./lib/stores/memory')();
break;
case 'nedb':
store = require('./lib/stores/nedb')(options.nedb);
break;
case 'mongo':
store = require('./lib/stores/mongo')(options.mongo);
break;
}
if (!store) {
throw new VError('store option is mandatory');
}
//prepare sanitizer configuration
sanitizer = options.sanitizer;
if (!sanitizer) {
sanitizer = require('./lib/sanitizer')(store);
}
// prepare password checker
passwordChecker = options.passwordChecker;
if (!passwordChecker) {
passwordChecker = 'salt';
}
switch (passwordChecker) {
case 'salt':
case 'sha1':
passwordChecker = require('./lib/password/salt')();
break;
case 'plain':
case 'plaintext':
passwordChecker = require('./lib/password/plain')();
break;
}
if (!passwordChecker) {
throw new VError('passwordChecker option is mandatory');
}
passport = options.passport;
if (!passport) {
passport = require('./lib/passport')(store, passwordChecker);
}
fixtures = require('./lib/fixtures')(store, sanitizer, passwordChecker);
if (options.data) {
// we don't care about async loading
fixtures.loadFixtures(options.data, function (err) {
if (err) {debug(err);}
});
}
// Template engine configuration
// -----------------------------
//
// Use Nunjuck internally.
// We don't rely on Express render system as we want the module to be
// reusable.
// It is still possible to override templates and even engine through
// configuration.
var nunjucksLoader = _.map(options.views, function (directory) {
return new nunjucks.FileSystemLoader(directory);
});
var nunjucksEnv = new nunjucks.Environment(nunjucksLoader, {
autoescape: true,
watch: !options.cache
});
// A reusable function to render a Nunjuck template/partial.
// This should be recursive.
function partial(name, data) {
if (typeof(data.partial) === 'undefined') {
data.partial = partial;
}
var tmpl = nunjucksEnv.getTemplate(name);
return tmpl.render(data);
}
// Render a template.
function render(res, template, data) {
// make global values available
info = _.extend({}, res.locals, data || {});
return res.send(partial(template, info));
}
return require('./lib/router')(sanitizer, store, passwordChecker, passport, render);
};