2018-09-30 14:50:09 -04:00
|
|
|
/**
|
|
|
|
* Server-side Express application
|
|
|
|
* - API methods
|
|
|
|
* - entry-point to shared React App
|
|
|
|
*
|
|
|
|
*/
|
2018-09-09 17:22:44 -04:00
|
|
|
import express from 'express';
|
|
|
|
|
2018-09-13 14:54:16 -04:00
|
|
|
import session from 'express-session';
|
|
|
|
import pgConnect from 'connect-pg-simple';
|
2018-09-09 17:22:44 -04:00
|
|
|
|
2018-09-30 11:18:57 -04:00
|
|
|
import db from './db';
|
2019-02-24 07:17:59 -05:00
|
|
|
import tileserver from './tiles/tileserver';
|
2019-08-14 09:05:49 -04:00
|
|
|
import apiServer from './api/api';
|
2019-09-06 14:13:45 -04:00
|
|
|
import frontendRoute from './frontendRoute';
|
2018-09-09 17:22:44 -04:00
|
|
|
|
|
|
|
// create server
|
|
|
|
const server = express();
|
|
|
|
|
|
|
|
// disable header
|
|
|
|
server.disable('x-powered-by');
|
|
|
|
|
|
|
|
// serve static files
|
|
|
|
server.use(express.static(process.env.RAZZLE_PUBLIC_DIR));
|
|
|
|
|
|
|
|
// handle user sessions
|
2018-09-13 14:54:16 -04:00
|
|
|
const pgSession = pgConnect(session);
|
2019-08-09 13:49:43 -04:00
|
|
|
const sess: any = { // TODO: remove any
|
2018-09-29 14:09:48 -04:00
|
|
|
name: 'cl.session',
|
|
|
|
store: new pgSession({
|
2018-09-30 11:18:57 -04:00
|
|
|
pgPromise: db,
|
2019-02-24 14:28:11 -05:00
|
|
|
tableName: 'user_sessions'
|
2018-09-29 14:09:48 -04:00
|
|
|
}),
|
|
|
|
secret: process.env.APP_COOKIE_SECRET,
|
|
|
|
saveUninitialized: false,
|
|
|
|
resave: false,
|
|
|
|
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
|
2018-09-09 17:22:44 -04:00
|
|
|
};
|
|
|
|
if (server.get('env') === 'production') {
|
2018-09-29 14:09:48 -04:00
|
|
|
// trust first proxy
|
|
|
|
server.set('trust proxy', 1)
|
|
|
|
// serve secure cookies
|
|
|
|
sess.cookie.secure = true
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
server.use(session(sess));
|
|
|
|
|
2018-09-10 05:44:32 -04:00
|
|
|
server.use('/tiles', tileserver);
|
2019-08-14 09:05:49 -04:00
|
|
|
server.use('/api', apiServer);
|
2019-08-14 09:17:47 -04:00
|
|
|
server.use(frontendRoute);
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
export default server;
|