forked from apostrophecms/apostrophe-sandbox
/
app.js
329 lines (288 loc) · 8.95 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
var appy = require('appy');
var async = require('async');
var uploadfs = require('uploadfs')();
var fs = require('fs');
var apos = require('apostrophe')();
var _ = require('underscore');
var extend = require('extend');
var app, db;
var demo;
var pages;
var snippets;
var blog;
var events;
var map;
var people;
var groups;
var sections;
// Server-specific settings to be merged with options
// See local.example.js
var local = require('./data/local.js');
local.db = local.db || {};
var options = {
// Don't bother with viewEngine, we'll use apos.partial() if we want to
// render anything directly
auth: {
strategy: 'local',
options: {
users: {
admin: {
type: 'person',
username: 'admin',
password: 'demo',
_id: 'admin',
// Without this login is forbidden
login: true,
permissions: [ 'admin' ]
}
},
// A user is just a snippet page with username and password properties.
// (Yes, the password property is hashed and salted.)
collection: 'aposPages',
template: function(data) {
return pages.decoratePageContent({ content: apos.partial('login', data) });
}
}
},
// Make sure we check the .login flag so people who have profiles but no
// login privileges are not allowed to log in
beforeSignin: function(user, callback) {
if (user.type !== 'person') {
// Whaaat the dickens this page is not even a person
return callback('error');
}
if (!user.login) {
return callback({ message: 'user does not have login privileges' });
} else {
return callback(null);
}
},
sessionSecret: 'whatever',
db: {
// 127.0.0.1 connects much faster than localhost when offline on macs,
// goes to the same place
host: local.db.host || '127.0.0.1',
port: local.db.port || 27017,
name: local.db.name || 'apostrophe-sandbox',
collections: [
// Handy way to get appy to create mongodb collection objects for you,
// see the appy docs
]
},
// Supplies LESS middleware by default
static: __dirname + '/public',
// Where uploaded images go. This can be s3 or any other backend thanks to uploadfs.
// Note you can't use the local backend with Heroku (Heroku does not have a persistent
// writable filesystem)
uploadfs: {
backend: 'local',
uploadsPath: __dirname + '/public/uploads',
uploadsUrl: local.uploadsUrl,
tempPath: __dirname + '/data/temp/uploadfs',
// Register Apostrophe's standard image sizes. Notice you could
// concatenate your own list of sizes if you had a need to
imageSizes: apos.defaultImageSizes.concat([])
},
ready: function(appArg, dbArg)
{
app = appArg;
db = dbArg;
async.series([ createTemp, initUploadfs, initApos, setRoutes ], listen);
}
};
// Allow Express locals to come from the options object above or
// from data/local.js
var locals = options.locals || {};
extend(true, locals, local.locals || {});
var demo = locals.demo;
appy.bootstrap(options);
function createTemp(callback) {
if (!fs.existsSync(__dirname + '/data/temp')) {
fs.mkdir(__dirname + '/data/temp', callback);
} else {
callback(null);
}
}
function initUploadfs(callback) {
uploadfs.init(options.uploadfs, callback);
}
function initApos(callback) {
require('apostrophe-twitter')({ apos: apos, app: app });
require('apostrophe-rss')({ apos: apos, app: app });
async.series([initAposMain, initAposPages, initAposSnippets, initAposBlog, initAposEvents, initAposMap, initAposPeople, initAposGroups,initAposSections, initAposPageTypesMenu, initAposAppAssets], callback);
function initAposMain(callback) {
return apos.init({
db: db,
app: app,
uploadfs: uploadfs,
permissions: aposPermissions,
locals: local.locals,
// Allows us to extend shared layouts
partialPaths: [ __dirname + '/views/global' ],
minify: local.minify
}, callback);
}
function initAposPages(callback) {
var pageTypes = [
{ name: 'default', label: 'Default (Two Column)' },
{ name: 'onecolumn', label: 'One Column' },
{ name: 'home', label: 'Home Page' },
{ name: 'largeSlideshow', label: 'Large Slideshow' }
];
// This feature isn't styled adequately for the demo site yet
if (!demo) {
pageTypes.push({ name: 'sectioned', label: 'Page With Sections' });
}
pages = require('apostrophe-pages')({ apos: apos, app: app, types: pageTypes }, callback);
}
function initAposSnippets(callback) {
snippets = require('apostrophe-snippets')({ apos: apos, pages: pages, app: app }, callback);
}
function initAposBlog(callback) {
blog = require('apostrophe-blog')({ apos: apos, pages: pages, app: app }, callback);
}
function initAposEvents(callback) {
// This feature hasn't been styled adequately for
// the official demo site yet
if (demo) {
return callback(null);
}
events = require('apostrophe-events')({ apos: apos, pages: pages, app: app }, callback);
}
// We could subclass the blog module in lib/modules/blog/index.js so that we can supply alternative templates.
// function initAposBlog(callback) {
// blog = require('./lib/modules/blog/index.js')({
// apos: apos,
// pages: pages,
// app: app,
// widget: true,
// browser: {
// construct: 'MyBlog'
// }
// }, callback);
// }
function initAposMap(callback) {
map = require('apostrophe-map')({ apos: apos, pages: pages, app: app }, callback);
// Start the background geocoder.
//
// NOTE: if you are using multiple processes and/or servers,
// call this from only ONE to avoid exceeding Google's rate limits
map.startGeocoder();
}
function initAposPeople(callback) {
people = require('apostrophe-people')({
apos: apos,
pages: pages,
app: app,
widget: true
}, callback);
}
function initAposGroups(callback) {
if (!demo) {
groups = require('apostrophe-groups')({
apos: apos,
pages: pages,
app: app,
people: people,
widget: true
}, function(err) {
people.setGroups(groups);
return callback(err);
});
} else {
return callback(null);
}
}
function initAposSections(callback) {
sections = require('apostrophe-sections')({ apos: apos, app: app }, callback);
}
// Now that all of the types are set up, we can change our minds
// about which ones are actually on the dropdown for making a new
// page, or change the order. In this case we get rid of
// "Snippets" as a page type, because they are mostly useful as
// a widget to be inserted in other pages
function initAposPageTypesMenu(callback) {
var pageTypesMenu = [
{ name: 'default', label: 'Default (Two Column)' },
{ name: 'onecolumn', label: 'One Column' },
{ name: 'home', label: 'Home Page' },
{ name: 'largeSlideshow', label: 'Large Slideshow' }
];
if (!demo) {
pageTypesMenu.push({ name: 'sectioned', label: 'Page With Sections' });
}
pageTypesMenu = pageTypesMenu.concat([
{ name: 'blog', label: 'Blog' },
{ name: 'map', label: 'Map' }
]);
if (!demo) {
pageTypesMenu = pageTypesMenu.concat([
{ name: 'groups', label: 'Directory' }
]);
}
pages.setMenu(pageTypesMenu);
return callback(null);
}
function initAposAppAssets(callback) {
pushAsset('stylesheet', 'site', { when: 'always' });
pushAsset('script', 'site', { when: 'always' });
return callback();
function pushAsset(type, name, options) {
options.fs = __dirname;
options.web = '';
return apos.pushAsset(type, name, options);
}
}
}
function setRoutes(callback) {
// Other app-specific routes here.
// LAST ROUTE: pages in the wiki. We want these at the root level.
// pages.serve does all the work. Just supply callbacks for some things
// we'd like to do in addition.
var load = [
// Shared page with things like the footer
'global',
// Modules that introduce their own loaders
snippets.loader,
blog.loader,
map.loader,
people.loader,
pages.searchLoader
];
// Add this one if it's enabled
if (events) {
load.push(events.loader);
}
if (groups) {
load.push(groups.loader);
}
app.get('*', pages.serve({
templatePath: __dirname + '/views/pages',
tabOptions: { depth: 2 },
load: load
}));
return callback(null);
}
function listen(err) {
if (err) {
throw err;
}
// Command line tasks
if (apos.startTask()) {
// Chill and let the task run until it's done, don't try to listen or exit
return;
}
appy.listen();
}
// Allow only the admin user to edit anything with Apostrophe,
// let everyone view anything
function aposPermissions(req, action, object, callback) {
if (req.user && (req.user.username === 'admin')) {
// OK
return callback(null);
} else if (action.match(/^view/)) {
return callback(null);
} else {
return callback('Forbidden');
}
}