2016-05-03 04:09:16 +00:00
|
|
|
import Express from 'express';
|
2016-05-13 20:04:16 +00:00
|
|
|
import mongoose from 'mongoose';
|
|
|
|
import bodyParser from 'body-parser';
|
|
|
|
import cookieParser from 'cookie-parser';
|
2017-03-23 18:53:16 +00:00
|
|
|
import cors from 'cors';
|
2016-05-13 20:04:16 +00:00
|
|
|
import session from 'express-session';
|
2017-02-22 19:29:35 +00:00
|
|
|
import connectMongo from 'connect-mongo';
|
2016-05-13 20:04:16 +00:00
|
|
|
import passport from 'passport';
|
2016-05-03 04:09:16 +00:00
|
|
|
import path from 'path';
|
2017-06-26 17:58:58 +00:00
|
|
|
import csurf from 'csurf';
|
2016-05-03 04:09:16 +00:00
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
// Webpack Requirements
|
2016-05-03 04:09:16 +00:00
|
|
|
import webpack from 'webpack';
|
|
|
|
import webpackDevMiddleware from 'webpack-dev-middleware';
|
|
|
|
import webpackHotMiddleware from 'webpack-hot-middleware';
|
2017-02-22 19:29:35 +00:00
|
|
|
import config from '../webpack.config.dev';
|
2016-05-03 04:09:16 +00:00
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
// Import all required modules
|
2016-05-03 04:09:16 +00:00
|
|
|
import serverConfig from './config';
|
2016-06-09 00:52:59 +00:00
|
|
|
import users from './routes/user.routes';
|
2016-06-14 20:48:16 +00:00
|
|
|
import sessions from './routes/session.routes';
|
2016-06-17 18:11:52 +00:00
|
|
|
import projects from './routes/project.routes';
|
2016-07-13 22:53:56 +00:00
|
|
|
import files from './routes/file.routes';
|
2016-07-15 23:05:18 +00:00
|
|
|
import aws from './routes/aws.routes';
|
2016-06-18 22:33:49 +00:00
|
|
|
import serverRoutes from './routes/server.routes';
|
2016-09-05 20:08:08 +00:00
|
|
|
import embedRoutes from './routes/embed.routes';
|
2017-06-26 17:58:58 +00:00
|
|
|
import { requestsOfTypeJSON } from './utils/requestsOfType';
|
2016-05-03 04:09:16 +00:00
|
|
|
|
2016-11-08 23:11:12 +00:00
|
|
|
import { renderIndex } from './views/index';
|
2017-01-06 18:08:03 +00:00
|
|
|
import { get404Sketch } from './views/404Page';
|
2016-11-08 23:11:12 +00:00
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
const app = new Express();
|
|
|
|
const MongoStore = connectMongo(session);
|
|
|
|
|
2017-03-23 18:53:16 +00:00
|
|
|
const corsOriginsWhitelist = [
|
|
|
|
/p5js\.org$/,
|
|
|
|
];
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
// Run Webpack dev server in development mode
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
const compiler = webpack(config);
|
|
|
|
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }));
|
|
|
|
app.use(webpackHotMiddleware(compiler));
|
2017-03-23 18:53:16 +00:00
|
|
|
|
|
|
|
corsOriginsWhitelist.push(/localhost/);
|
2017-02-22 19:29:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 18:53:16 +00:00
|
|
|
// Enable Cross-Origin Resource Sharing (CORS) for all origins
|
|
|
|
const corsMiddleware = cors({
|
|
|
|
credentials: true,
|
|
|
|
origin: corsOriginsWhitelist,
|
|
|
|
});
|
|
|
|
app.use(corsMiddleware);
|
|
|
|
// Enable pre-flight OPTIONS route for all end-points
|
|
|
|
app.options('*', corsMiddleware);
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
// Body parser, cookie parser, sessions, serve public assets
|
2016-05-17 19:50:37 +00:00
|
|
|
|
2016-05-13 20:49:56 +00:00
|
|
|
app.use(Express.static(path.resolve(__dirname, '../static')));
|
2017-05-10 19:07:40 +00:00
|
|
|
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
|
|
|
|
app.use(bodyParser.json({ limit: '50mb' }));
|
2016-05-13 20:04:16 +00:00
|
|
|
app.use(cookieParser());
|
2016-06-13 23:29:33 +00:00
|
|
|
app.use(session({
|
|
|
|
resave: true,
|
|
|
|
saveUninitialized: false,
|
|
|
|
secret: process.env.SESSION_SECRET,
|
|
|
|
proxy: true,
|
|
|
|
name: 'sessionId',
|
|
|
|
cookie: {
|
|
|
|
httpOnly: true,
|
|
|
|
secure: false,
|
|
|
|
},
|
|
|
|
store: new MongoStore({
|
|
|
|
url: process.env.MONGO_URL,
|
|
|
|
autoReconnect: true
|
|
|
|
})
|
|
|
|
}));
|
2017-06-26 17:58:58 +00:00
|
|
|
|
|
|
|
// Enables CSRF protection and stores secret in session
|
|
|
|
app.use(csurf());
|
|
|
|
// Middleware to add CSRF token as cookie to some requests
|
|
|
|
const csrfToken = (req, res, next) => {
|
|
|
|
res.cookie('XSRF-TOKEN', req.csrfToken());
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
2016-05-13 20:04:16 +00:00
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
2017-06-26 17:58:58 +00:00
|
|
|
app.use('/api', requestsOfTypeJSON(), users);
|
|
|
|
app.use('/api', requestsOfTypeJSON(), sessions);
|
|
|
|
app.use('/api', requestsOfTypeJSON(), projects);
|
|
|
|
app.use('/api', requestsOfTypeJSON(), files);
|
|
|
|
app.use('/api', requestsOfTypeJSON(), aws);
|
2016-06-23 22:29:55 +00:00
|
|
|
// this is supposed to be TEMPORARY -- until i figure out
|
2016-06-18 22:33:49 +00:00
|
|
|
// isomorphic rendering
|
2017-06-26 17:58:58 +00:00
|
|
|
app.use('/', csrfToken, serverRoutes);
|
2016-09-05 20:08:08 +00:00
|
|
|
|
2017-06-26 17:58:58 +00:00
|
|
|
app.use('/', csrfToken, embedRoutes);
|
2016-08-31 16:57:47 +00:00
|
|
|
app.get('/auth/github', passport.authenticate('github'));
|
|
|
|
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), (req, res) => {
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
2016-05-03 04:09:16 +00:00
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
// configure passport
|
2016-06-27 17:09:18 +00:00
|
|
|
require('./config/passport');
|
|
|
|
// const passportConfig = require('./config/passport');
|
2016-05-17 19:50:37 +00:00
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
// Connect to MongoDB
|
2016-06-09 17:56:23 +00:00
|
|
|
// mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
|
|
|
|
mongoose.connect(serverConfig.mongoURL);
|
|
|
|
mongoose.connection.on('error', () => {
|
|
|
|
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
app.get('/', (req, res) => {
|
2016-11-08 23:11:12 +00:00
|
|
|
res.sendFile(renderIndex());
|
2016-06-23 22:29:55 +00:00
|
|
|
});
|
2016-05-03 04:09:16 +00:00
|
|
|
|
2017-01-06 18:08:03 +00:00
|
|
|
// Handle missing routes.
|
|
|
|
app.get('*', (req, res) => {
|
|
|
|
res.status(404);
|
|
|
|
if (req.accepts('html')) {
|
|
|
|
get404Sketch(html => res.send(html));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req.accepts('json')) {
|
|
|
|
res.send({ error: 'Not found.' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.type('txt').send('Not found.');
|
|
|
|
});
|
|
|
|
|
2016-05-03 04:09:16 +00:00
|
|
|
// start app
|
|
|
|
app.listen(serverConfig.port, (error) => {
|
|
|
|
if (!error) {
|
|
|
|
console.log(`p5js web editor is running on port: ${serverConfig.port}!`); // eslint-disable-line
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export default app;
|