This repository was archived by the owner on Aug 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
264 lines (236 loc) · 8.91 KB
/
app.js
File metadata and controls
264 lines (236 loc) · 8.91 KB
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
"use strict";
const config = require('./admin.ergo.js');
const base_uri = config.default_fields.base_uri || '/';
// helper utils
var path = require('path')
var posix = path.posix;
var debug = require('debug')('app')
var ergo = require('ergo-core');
var _ = require('ergo-utils')._;
var string = require('./lib/strings');
var fs = require('./lib/fs');
// Models
var Post = require('./models/post');
var project = ergo.config.newContext(path.resolve(__dirname, config.project))
debug('project base dir is: ' + project.getBasePath())
// koa middleware
var koa = require('koa');
var logger = require('koa-logger');
var lusca = require('koa-lusca');
var session = require('koa-session');
var compose = require('koa-compose');
var Router = require('koa-router');
var koaBody = require('koa-body');
var send = require('koa-send');
var render = require('koa-usematch')(
{
views: path.join(__dirname, 'views'),
partials: path.join(__dirname, 'partials'),
defaults: _.extend(require('./lib/filters'), config.default_fields)
});
var auth = require('./lib/auth')(config.auth)
var l = debug;
function setHeaders(res) {
// disable proxy caching
//if (!ctx.response.get('Cache-Control'))
res.setHeader('Cache-Control', 'private, proxy-revalidate, s-maxage=0, max-age=86400');
}
/////////////////////////
var router = new Router();
var publicRoutes = new Router();
const csrfOptions = {secret:"csrf:sess"};
var formGet = compose([lusca.csrf(csrfOptions)])
var formPost = compose([koaBody(), lusca.csrf(csrfOptions)])
var formMulti = compose([koaBody({formidable:{uploadDir: __dirname}}), lusca.csrf(csrfOptions)])
var pageData = function *(app_ctx, new_ctx) {
var data = {
user:app_ctx.session.user,
project_name: string.titleCase(path.basename(project.getBasePath())),
menu: { post_types: yield require('./models/post_types')(project) },
}
if (!!app_ctx.state && !!app_ctx.state._csrf)
data._csrf = app_ctx.state._csrf;
return _.extend({}, data, new_ctx)
}
// ROUTES
router.get(base_uri+'', function *(next) {
//debug(this)
var content = yield require('./models/home')(project);
var data = yield pageData(this, _.extend({title:'Home'}, content));
debug(data)
this.body = yield render('home', data);
});
router.get(base_uri+'settings', function *(next) {
var data = yield pageData(this,{title:'Settings', list:yield require('./models/settings')(project)});
debug(data)
this.body = yield render('keyed_list', data);
});
router.get(base_uri+'posts/', formGet, function *(next) {
var data = yield pageData(this,{title:'All Posts', files:yield require('./models/posts')(project) });
this.body = yield render('files', data);
});
router.get(base_uri+'posts/:post_type', formGet, function *(next) {
var title = string.pluralise(string.titleCase(this.params.post_type));
var data = yield pageData(this,{title:title, files:yield require('./models/posts')(project, this.params.post_type) });
this.body = yield render('files', data);
});
router.get(base_uri+'post/:filepath', formGet, function *(next) {
var f = path.join(project.getBasePath(),decodeURIComponent(this.params.filepath));
var post = new Post(project.getBasePath(), project.getSourcePath(), f)
yield post.load(project);
post.prepTemplate(config, project);
var data = yield pageData(this,{title:'Edit Post: ' + post.relPath, post:post});
this.body = yield render('post', data);
});
router.post(base_uri+'post/:filepath', formPost, function *(next) {
var f = path.join(project.getBasePath(),decodeURIComponent(this.params.filepath));
var post = new Post(project.getBasePath(), project.getSourcePath(), f);
delete this.request.body._csrf;
yield post.save(this.request.body, project)
post.prepTemplate(config, project);
var data = yield pageData(this,{title:'Edit Post: ' + post.relPath, post:post, saved:true});
this.body = yield render('post', data);
});
router.get(base_uri+'files/:where', formGet, function *(next) {
var files = yield require('./models/files')(project, this.params.where);
var data = yield pageData(this,{title:string.singular(string.titleCase(this.params.where)) + ' Files', files:files });
//debug(data)
this.body = yield render('files', data);
});
router.get(base_uri+'images', formGet, function *(next) {
var data = yield pageData(this,{title:'Images', preview:true, files:yield require('./models/images')(project) });
//debug(data)
this.body = yield render('files', data);
});
router.get(base_uri+'file/text-edit/:filepath', formGet, function *(next) {
var filename = decodeURIComponent(this.params.filepath);
var f = path.join(project.getBasePath(),filename);
var content = yield fs.readFileP(f, 'utf8');
var data = yield pageData(this,{title:filename, content:content, filePath:f });
this.body = yield render('file', data);
});
router.post(base_uri+'file/text-edit/:filepath', formPost, function *(next) {
var filename = decodeURIComponent(this.params.filepath);
var f = path.join(project.getBasePath(),filename);
//var content = yield fs.readFileP(f, 'utf8');
var content = this.request.body.content;
yield fs.writeFileP(f, content, 'utf8');
var data = yield pageData(this,{title:filename, saved:true, content:content, filePath:f });
this.body = yield render('file', data);
});
router.get(base_uri+'file/image-edit/:filepath', function *(next) {
//this.body = decodeURIComponent(this.params.filepath);
this.redirect(base_uri+'file/view/' + encodeURIComponent(decodeURIComponent(this.params.filepath)));
this.status = 302;
});
router.post(base_uri+'file/delete/:filepath', formPost, function *(next) {
// this is via an ajax request only
var filename = decodeURIComponent(this.params.filepath);
var f = path.join(project.getBasePath(),filename);
debug('file delete: ' + f)
var _this = this;
var p = fs.unlinkP(f);
p.then(function() {
_this.body = "OK " + filename;
}).catch(function(err) {
_this.body = "Failed to delete '" + filename + "'. Reason: " + err.toString();
})
yield p;
});
router.post(base_uri+'file/rename/:filepath', formPost, function *(next) {
// this is via an ajax request only
var filename = decodeURIComponent(this.params.filepath);
var tofilename = this.request.body.rename;
var f = path.join(project.getBasePath(),filename);
var fTo = path.join(path.dirname(f),tofilename);
debug('file rename: ' + f + ' to ' + fTo);
var _this = this;
var p = fs.renameP(f, fTo);
p.then(function() {
_this.body = "OK " + filename;
}).catch(function(err) {
_this.body = "Failed to rename '" + filename + "' to '"+tofilename+"'. Reason: " + err.toString();
})
yield p;
});
router.post(base_uri+'file/chown/:filepath', formPost, function *(next) {
// this is via an ajax request only
var filename = decodeURIComponent(this.params.filepath);
var f = path.join(project.getBasePath(),filename);
var chown_uid = this.request.body.uid;
var chown_gid = this.request.body.gid;
var chmod = this.request.body.mode;
debug('file chown: ' + f)
this.body = "TODO: OK " + decodeURIComponent(this.params.filepath);
});
router.get(base_uri+'file/view/:filepath', function *(next) {
var f = decodeURIComponent(this.params.filepath);
yield send(this, f, { root: project.getBasePath(), setHeaders:setHeaders});
});
// Login ROUTES
publicRoutes.get(base_uri+'login', formGet, function *(next) {
if (config.auth.auto_login) {
yield auth.validate.call(this, next);
this.redirect(base_uri+'');
this.status = 302;
return;
}
this.body = yield render('login', {_csrf:this.state._csrf});
});
publicRoutes.post(base_uri+'login', formPost, auth.validate, function *(next) {
l("%j", this.request.body);
this.redirect(base_uri+'');
this.status = 302;
});
publicRoutes.get(base_uri+'logout', function *(next) {
this.session.user = undefined;
this.redirect(base_uri+'login');
this.status = 302;
});
var app = koa();
app.keys = config.keys;
app.use(logger());
app.use(function *(next){
// Serve static files in public folder
var relPath = posix.relative(base_uri, this.path); // chop off the base_uri & leave us with a relative path only (eg 'css/styles.css')
const publicDir = path.join(__dirname, 'public');
try {
var stat = yield fs.statP(path.join(publicDir, relPath));
if (stat.isFile())
yield send(this, relPath, {root:publicDir, setHeaders:setHeaders});
else
yield next;
}
catch(e) {
yield next;
}
});
app.use(session(config.session, app));
//app.use(lusca.csp({/* ... */}));
app.use(lusca.xframe({ value: 'SAMEORIGIN' }));
//app.use(lusca.p3p({ value: 'ABCDEF' }));
//app.use(lusca.hsts({ maxAge: 31536000 });
app.use(lusca.xssProtection());
app.use(function *(next){
try {
yield next;
} catch (err) {
if (401 == err.status) {
this.redirect(base_uri+'login');
this.status = 302;
} else {
throw err;
}
}
});
app.use(publicRoutes.routes());
app.use(publicRoutes.allowedMethods());
// custom 401 handling
app.use(auth.check)
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(config.port || 3000);
console.log("Listening on "+(config.port || 3000)+". Press Ctrl+C to abort")
app.on('error', function (err) {
console.log('\n\n'+err.stack)
})