/
wiki.js
188 lines (165 loc) · 4.23 KB
/
wiki.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
var appy = require('appy');
var async = require('async');
var uploadfs = require('uploadfs')();
var fs = require('fs');
var apos = require('apostrophe')();
var app, db;
// Server-specific settings to be merged with options
// See local.example.js
var local = require('./data/local.js');
var options = {
viewEngine: function(app) {
var nunjucks = require('nunjucks');
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader(__dirname + '/views'));
env.express(app);
},
auth: {
strategy: 'local',
options: {
users: {
admin: {
username: 'admin',
password: 'demo',
id: 'admin'
}
}
}
},
sessionSecret: 'whatever',
db: {
// host: 'localhost'
// port: 27017,
name: 'aposwiki',
collections: [
{ name: 'areas', index: { fields: { slug: 1 }, unique: true } },
{ name: 'pages', index: { fields: { slug: 1 }, unique: true } },
'files'
],
},
static: __dirname + '/public',
uploadfs: {
backend: 'local',
uploadsPath: __dirname + '/public/uploads',
uploadsUrl: local.uploadsUrl,
tempPath: __dirname + '/data/temp/uploadfs',
// apos needs these sizes to exist with these names
imageSizes: [
{
name: 'full',
width: 1140,
height: 1140
},
{
name: 'two-thirds',
width: 760,
height: 760
},
{
name: 'one-half',
width: 570,
height: 700
},
{
name: 'one-third',
width: 380,
height: 700
}
],
},
ready: function(appArg, dbArg)
{
app = appArg;
db = dbArg;
async.series([ createTemp, initUploadfs, initApos, setRoutes ], listen);
}
};
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 });
return apos.init({
files: appy.files,
areas: appy.areas,
pages: appy.pages,
app: app,
uploadfs: uploadfs,
permissions: aposPermissions,
}, callback);
}
function setRoutes(callback) {
// Other app-specific routes here.
// LAST ROUTE: pages in the wiki. We want these at the root level.
// If we haven't matched anything else, look for a page with content.
// This is a wiki so if there is no content just show an empty page
// so the user can edit and save and bring it into being.
// Note the leading slash is included. Express automatically supplies /
// if the URL is empty
app.get('*',
function(req, res, next) {
// Get content for this page
req.slug = req.params[0];
apos.getPage(req.slug, function(e, info) {
if (e) {
return fail(req, res);
}
if (!info) {
// No such page yet
info = { slug: req.slug, areas: {} };
}
req.page = info;
return next();
});
},
function(req, res, next) {
// Get the shared footer
apos.getArea('footer', function(e, info) {
if (e) {
return fail(req, res);
}
req.footer = info;
return next();
});
},
function (req, res) {
return res.render('page.html', {
slug: req.slug,
main: req.page.areas.main ? req.page.areas.main.items : [],
sidebar: req.page.areas.sidebar ? req.page.areas.sidebar.items : [],
user: req.user,
edit: req.user && req.user.username === 'admin',
footer: req.footer ? req.footer.items : []
});
}
);
return callback(null);
}
function listen(err) {
if (err) {
throw err;
}
console.log("Calling appy.listen");
appy.listen();
}
function fail(req, res) {
res.statusCode = 500;
res.send('500 error, URL was ' + req.url);
}
// Allow only the admin user to edit anything with Apos
function aposPermissions(req, action, fileOrSlug, callback) {
if (req.user && (req.user.username === 'admin')) {
// OK
return callback(null);
} else {
return callback('Forbidden');
}
}