-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
326 lines (278 loc) · 9.44 KB
/
app.ts
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
import * as bodyParser from "body-parser";
import * as c from "./src/context";
import * as Tokens from "csrf";
import * as express from "express";
import * as handlebars from "express-handlebars";
import session = require("cookie-session"); // CommonJS Typescript workaround: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
import * as fs from "fs";
import * as shortid from "shortid";
import * as request_funcs from "./src/request_funcs";
import * as db_impl from "./src/db";
import config, {Config} from "./src/config";
import * as password_checker from "./src/password";
import * as pg from "./src/db-pg";
import * as wa_api from "./src/wild_apricot";
import * as yargs from "yargs";
import * as http from "http";
import * as logger from "logger";
import {
authentication_middleware,
BearerTokenProvider,
SessionProvider,
TestUserProvider,
user_required,
user_required_or_redirect,
} from './src/auth';
import createConnection, {Connection} from "./src/typeorm_db";
// Need to declare our injected properties on the Request object
declare global {
namespace Express {
interface Application {
logger: logger.Logger;
db: db_impl.DB;
orm: Connection;
}
}
}
let make_logger = (logger) => {
let request_id = shortid.generate();
let log_func = function (level, args) {
let date = new Date();
args.unshift( "(" + request_id + ")" );
args.unshift( "[" + date.toISOString() + "]" );
return logger[level]( ...args );
};
let request_logger = {
fatal: function(...args) { log_func( "fatal", args ) }
,error: function(...args) { log_func( "error", args ) }
,warn: function(...args) { log_func( "warn", args ) }
,info: function(...args) { log_func( "info", args ) }
,debug: function(...args) { log_func( "debug", args ) }
};
return request_logger;
};
function context_middleware( logger, conf, db, wa )
{
return function(req, res, next) {
const request_logger = make_logger( logger );
request_logger.info( "Begin request to", req.method, req.path );
const checker = new password_checker.Checker(
conf['preferred_password_crypt_method']
,db
);
req.ctx = new c.Context( conf, request_logger, checker, wa );
if(! req.session.csrf_secret ) {
const tokens = new Tokens();
request_logger.info( "Making CSRF secret for new client" );
req.session.csrf_secret = tokens.secretSync();
}
next()
}
}
function error_handler_builder( logger ) {
return ( err, req, res, next ) => {
logger.error( err.toString() );
logger.error( "Stack trace: ", err.stack );
if( res.headersSent ) return next( err );
res.status( 500 );
};
};
function init_app(
conf
,db
,typeorm_connection
,logger
,wa: wa_api.WA
)
{
let app = setup_app_params( conf, db, typeorm_connection, logger );
setup_app_routes( conf, db, logger, app, wa );
return app;
}
function setup_app_params( conf, db, typeorm_connection, logger )
{
let use_secure_cookie = (conf['deployment_type'] == "prod");
let session_options = {
secret: conf.session_secret
,maxAge: conf.session_length_sec
,sameSite: true
,secure: use_secure_cookie
,httpOnly: true
};
let app = express();
app.use( session( session_options ) );
app.use( bodyParser.json() );
app.use( bodyParser.raw({
type: "image/*"
,limit: conf['photo_size_limit']
}) );
app.use( bodyParser.urlencoded({ extended: true }) );
app.use( express.static( 'public' ) );
app.engine( 'handlebars', handlebars({
defaultLayout: 'main'
}) );
app.set( 'view engine', 'handlebars' );
app.set( "trust proxy", conf.is_behind_reverse_proxy );
// Init context
logger.setLevel( conf.log_level );
logger.format = ( level, date, message ) => message;
app.use( error_handler_builder( logger ) );
// // Install the request helpers
app.logger = logger;
app.db = db;
app.orm = typeorm_connection;
return app;
}
function setup_app_routes(
conf
,db
,logger
,app
,wa: wa_api.WA
)
{
app.use(context_middleware( logger, conf, db, wa ));
const api = express.Router();
// API requires bearer token authorization
api.use(authentication_middleware([BearerTokenProvider, TestUserProvider]));
api.use(user_required(logger));
api.get('/', request_funcs.get_versions );
const api_v1 = express.Router();
api_v1.put( '/member',
request_funcs.put_member );
api_v1.get( '/member/:member_id',
request_funcs.get_member );
api_v1.route( '/member/:member_id/address')
.put(request_funcs.put_member_address )
.get(request_funcs.get_member_address );
api_v1.route( '/member/:member_id/is_active')
.put(request_funcs.put_member_is_active )
.get(request_funcs.get_member_is_active );
api_v1.put( '/member/:member_id/rfid',
request_funcs.put_member_rfid );
api_v1.post( '/member/:member_id/send_signup_email',
request_funcs.post_member_signup_email );
api_v1.post( '/member/:member_id/send_group_signup_email',
request_funcs.post_group_member_signup_email );
api_v1.get( '/members/pending',
request_funcs.get_members_pending );
api_v1.put( '/member/:member_id/wildapricot',
request_funcs.put_member_wildapricot );
api_v1.put( '/member/:member_id/google_group_signup',
request_funcs.put_member_google_group );
api_v1.route( '/member/:member_id/photo')
.put(request_funcs.put_member_photo )
.get(request_funcs.get_member_photo );
api_v1.get( '/rfid/:rfid',
request_funcs.get_member_rfid );
api_v1.get( '/rfids',
request_funcs.get_rfid_dump );
api_v1.post( '/rfid/log_entry/:rfid/:is_allowed',
request_funcs.post_log_rfid );
api.use('/v1', api_v1);
// Install the API router
app.use('/api', api);
// Undocumented route for the callback on Google's OAuth token
// No longer used
//app.get( '/api/v1/google_oauth',
// request_funcs.google_oauth );
const views = express.Router();
views.use(authentication_middleware([SessionProvider, TestUserProvider]));
// Unauthenticated routes
views.get( '/', request_funcs.tmpl_view( 'home' ) );
views.get( '/user/is-logged-in',
request_funcs.is_user_logged_in );
views.post( '/user/login',
request_funcs.login_user );
// Authenticated routes
const session_required = user_required_or_redirect(logger, "/");
views.get( '/members/pending', session_required,
request_funcs.tmpl_view( 'members-pending' ) );
views.get( '/member/signup', session_required,
request_funcs.member_signup );
views.get( '/members/active', session_required,
request_funcs.members_active );
views.get( '/member/show/:member_id', session_required,
request_funcs.member_info );
views.post( '/user/logout', session_required,
request_funcs.logout_user );
views.get( '/rfid/log', session_required,
request_funcs.rfid_log );
views.get( '/tokens', session_required,
request_funcs.tokens );
views.post( '/tokens/add', session_required,
request_funcs.add_token );
views.post( '/tokens/delete', session_required,
request_funcs.delete_token );
// Install the View routers
app.use(views);
// 404 handler, must be last in the list
app.use( (req, res, next) => {
logger.error( "Route {" + req.method + " " + req.path
+ "} not found" );
res
.status( 404 )
.render( 'not-found', {
method: req.method
,path: req.path
});
});
}
function default_db(conf): db_impl.DB
{
let db: db_impl.DB = new pg.PG(
conf.db_host
,conf.db_port
,conf.db_name
,conf.db_user
,conf.db_password
);
return db;
}
export function default_conf(): Config
{
return config();
}
function default_wa(conf): wa_api.WA
{
let wa = new wa_api.WildApricot(
conf['wa_api_client']
,conf['wa_api_secret']
,conf['wa_account_id']
);
return wa;
}
export async function createApp(
typeorm_connection: Connection,
db?: db_impl.DB
,conf?: Config
,wa?: wa_api.WA
): Promise<express.Application>
{
if(! conf) conf = default_conf();
if(! db) db = default_db( conf );
if(! wa) wa = default_wa( conf );
const log = logger.createLogger( conf["log_file"] );
// Fix hanging connections for certain external requests. See:
// https://stackoverflow.com/questions/16965582/node-js-http-get-hangs-after-5-requests-to-remote-site
http.globalAgent.maxSockets = 1000;
// Init app
return init_app( conf, db, typeorm_connection, log, wa );
}
export async function start(
db?: db_impl.DB
,conf?: Config
,wa?: wa_api.WA
): Promise<http.Server>
{
if(! conf) conf = default_conf();
const typeorm_connection = await createConnection( conf );
const app = await createApp( typeorm_connection, db, conf, wa );
const httpServer = require( 'http' ).createServer( app );
let port = conf["port"];
// Start server running
httpServer.listen( port );
app.logger.info( "Server running on port", port );
return httpServer;
}
if( process.argv[2] == "start" ) start();