-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
117 lines (95 loc) · 2.98 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
'use strict';
var express = require('express'),
session = require('express-session'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
routes = require('./lib/controllers/main'),
_ = require('lodash'),
Collection = require('./lib/models/Collection'),
Strategy = require('./lib/strategy'),
Options = require('./lib/options');
var admin = express();
// this session store is only used if parent app
// DOES NOT already have a session store.
admin.use(session({
secret: 'Siracha!',
saveUninitialized: true,
resave: false
}));
admin.set('view engine', 'jade');
admin.set('views', __dirname + '/lib/views');
admin.use(bodyParser.urlencoded({
extended: false
}));
// allow delete method
admin.use(methodOverride(function(req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method
delete req.body._method
return method
}
}));
admin.use('/static', express.static(__dirname + '/lib/static'));
admin.use('/components', express.static(__dirname + '/components'));
module.exports = function(userDefined) {
var userDefined = userDefined || {};
var options = Options(userDefined);
var Models;
var collectionNames;
var collections;
var docs;
var strategy;
// models default to all models in a Mongoose connection
if (options.models.length > 0) {
Models = options.models.map(function(name) {
return mongoose.models[name];
});
} else {
Models = mongoose.models;
};
// create map of plural collection names for easy lookup
// e.g. {'users': 'User', ...}
collectionNames = _.mapValues(_.mapKeys(Models, function(model) {
return model.collection.name;
}), function(model) {
return model.modelName;
});
// extend models with methods for use with Siracha
collections = _.mapValues(Models, function(model) {
return Collection(model, options);
});
admin.locals._mongooseModels = Models;
admin.locals.collectionNames = collectionNames;
admin.locals.collections = collections;
// get mount path for use in routing static
admin.use(function(req, res, next) {
debugger;
var mountpath = admin.mountpath;
if(mountpath === '/') {
mountpath = '';
}
admin.locals.appPath = mountpath;
next();
});
admin.use(function(req, res, next) {
req.session.message = req.session.message || {
error: [],
success: [],
info: []
};
admin.locals.message = req.session.message;
next();
});
strategy = new Strategy(options);
admin.use(strategy.middleware.bind(strategy));
admin.get('/', routes.main);
admin.post('/login', strategy.login.bind(strategy));
admin.post('/logout', strategy.logout);
admin.get('/:collection', routes.collection);
admin.post('/:collection/suggest', routes.suggest);
admin.all('/:collection/new', routes.newDoc);
admin.all('/:collection/:doc', routes.doc);
return admin;
};