forked from DanKottke/midas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-koa.js
More file actions
157 lines (136 loc) · 4.67 KB
/
Copy pathapp-koa.js
File metadata and controls
157 lines (136 loc) · 4.67 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
const log = require('log')('app-koa');
const koa = require('koa');
const render = require('koa-ejs');
const serve = require('koa-static');
const path = require('path');
const parser = require('koa-bodyparser');
const CSRF = require('koa-csrf');
const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const passport = require('koa-passport');
const cacheControl = require('koa-cache-control');
const flash = require('koa-better-flash');
const md5File = require('md5-file');
const _ = require('lodash');
module.exports = (config) => {
// load configs
global.openopps = {
appPath: __dirname,
};
_.extend(openopps, require('./config/application'));
_.extend(openopps, require('./config/session'));
_.extend(openopps, require('./config/settings/auth'));
_.extend(openopps, require('./config/cache'));
_.extend(openopps, require('./config/version'));
_.extend(openopps, require('./config/fileStore'));
_.extend(openopps, require('./config/email'));
if(config) {
_.extend(openopps, config);
}
// easy loading of a feature
feature = function (name) {
return require(path.join(__dirname, 'api', name, 'controller'));
};
const app = new koa();
require('./lib/log/middleware')(app);
// initialize flash
app.use(flash());
// initialize body parser
app.use(parser());
// initialize cache controller
app.use(cacheControl(openopps.cache.public));
// configure session
app.proxy = true;
app.keys = [openopps.session.secret || 'your-secret-key'];
app.use(session(openopps.session, app));
// initialize authentication
require(path.join(__dirname, 'api/auth/passport'));
app.use(passport.initialize());
app.use(passport.session());
// configure receiving AWS SNS messages
app.use(feature('notification'));
// configure CSRF
app.use(new CSRF({
invalidSessionSecretMessage: { message: 'Invalid session' },
invalidSessionSecretStatusCode: 401,
invalidTokenMessage: JSON.stringify({ message: 'Invalid CSRF token' }),
invalidTokenStatusCode: 401,
excludedMethods: [ 'GET', 'HEAD', 'OPTIONS' ],
disableQuery: true,
}));
// for rendering .ejs views
render(app, {
root: path.join(__dirname, 'views'),
layout: 'layout',
viewExt: 'ejs',
cache: false,
debug: false,
});
// redirect any request coming other than openopps.hostName
app.use(async (ctx, next) => {
var hostParts = ctx.host.split(':');
if(!openopps.redirect || hostParts[0] === openopps.hostName) {
await next();
} else {
log.info('Redirecting from ' + ctx.host);
var url = ctx.protocol + '://' + openopps.hostName + (hostParts[1] ? ':' + hostParts[1] : '') + ctx.path + (ctx.querystring ? '?' + ctx.querystring : '');
ctx.status = 301;
ctx.redirect(url);
}
});
// allow external services to see if open opportunities is running
app.use(async (ctx, next) => {
if (ctx.path === '/server/status') {
ctx.body = '{"ok":10}';
ctx.type = 'json';
ctx.status = 200;
} else await next();
});
// serve our static resource files
app.use(serve(__dirname + '/dist'));
// CSRF Token
app.use(async (ctx, next) => {
if(ctx.path === '/csrfToken') {
ctx.cacheControl = openopps.cache.noStore;
ctx.body = { _csrf: ctx.csrf };
} else await next();
});
// load main/index.ejs unless api request
app.use(async function (ctx, next) {
ctx.cacheControl = openopps.cache.noStore;
// Throw 404 for undefined api routes
if(ctx.path.match('^/api/.*')) {
// JSON request for better-body parser are in request.fields
ctx.request.body = ctx.request.body || ctx.request.fields;
await next();
} else {
var data = {
systemName: openopps.systemName,
draftAdminOnly: openopps.draftAdminOnly,
version: openopps.version,
alert: null,
jsHash: md5File.sync(path.join(__dirname, 'dist', 'js', 'bundle.min.js')),
cssHash: md5File.sync(path.join(__dirname, 'dist', 'styles', 'main.css')),
};
await ctx.render('main/index', data);
}
});
// load our features (i.e. our api controllers)
app.use(feature('auth'));
app.use(feature('opportunity'));
app.use(feature('user'));
app.use(feature('autocomplete'));
app.use(feature('location'));
app.use(feature('admin'));
app.use(feature('volunteer'));
app.use(feature('activity'));
app.use(feature('comment'));
app.use(feature('document'));
const app_https = app;
app.listen(openopps.port);
console.log('App running at ' + openopps.hostName + ':' + openopps.port);
if (process.env.NODE_ENV == 'production') {
app_https.listen(443);
console.log('App running at ' + openopps.hostName + ':443');
}
};