2016-05-03 06:09:16 +02:00
|
|
|
import Express from 'express';
|
2016-05-13 22:04:16 +02:00
|
|
|
import mongoose from 'mongoose';
|
|
|
|
import bodyParser from 'body-parser';
|
|
|
|
import cookieParser from 'cookie-parser';
|
2017-03-23 19:53:16 +01:00
|
|
|
import cors from 'cors';
|
2016-05-13 22:04:16 +02:00
|
|
|
import session from 'express-session';
|
2017-02-22 20:29:35 +01:00
|
|
|
import connectMongo from 'connect-mongo';
|
2016-05-13 22:04:16 +02:00
|
|
|
import passport from 'passport';
|
2016-05-03 06:09:16 +02:00
|
|
|
import path from 'path';
|
2018-08-21 22:09:41 +02:00
|
|
|
import basicAuth from 'express-basic-auth';
|
2016-05-03 06:09:16 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
// Webpack Requirements
|
2016-05-03 06:09:16 +02:00
|
|
|
import webpack from 'webpack';
|
|
|
|
import webpackDevMiddleware from 'webpack-dev-middleware';
|
|
|
|
import webpackHotMiddleware from 'webpack-hot-middleware';
|
2018-09-26 22:14:06 +02:00
|
|
|
import config from '../webpack/config.dev';
|
2016-05-03 06:09:16 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
// Import all required modules
|
2016-06-09 02:52:59 +02:00
|
|
|
import users from './routes/user.routes';
|
2016-06-14 22:48:16 +02:00
|
|
|
import sessions from './routes/session.routes';
|
2016-06-17 20:11:52 +02:00
|
|
|
import projects from './routes/project.routes';
|
2016-07-14 00:53:56 +02:00
|
|
|
import files from './routes/file.routes';
|
2016-07-16 01:05:18 +02:00
|
|
|
import aws from './routes/aws.routes';
|
2016-06-19 00:33:49 +02:00
|
|
|
import serverRoutes from './routes/server.routes';
|
2016-09-05 22:08:08 +02:00
|
|
|
import embedRoutes from './routes/embed.routes';
|
2017-11-28 20:48:50 +01:00
|
|
|
import assetRoutes from './routes/asset.routes';
|
2017-06-26 19:58:58 +02:00
|
|
|
import { requestsOfTypeJSON } from './utils/requestsOfType';
|
2016-05-03 06:09:16 +02:00
|
|
|
|
2016-11-09 00:11:12 +01:00
|
|
|
import { renderIndex } from './views/index';
|
2017-01-06 19:08:03 +01:00
|
|
|
import { get404Sketch } from './views/404Page';
|
2016-11-09 00:11:12 +01:00
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
const app = new Express();
|
|
|
|
const MongoStore = connectMongo(session);
|
|
|
|
|
2018-08-21 23:52:42 +02:00
|
|
|
app.get('/health', (req, res) => res.json({ success: true }));
|
2018-08-21 23:39:34 +02:00
|
|
|
|
2018-08-21 22:09:41 +02:00
|
|
|
// For basic auth, in setting up beta editor
|
|
|
|
if (process.env.BASIC_USERNAME && process.env.BASIC_PASSWORD) {
|
|
|
|
app.use(basicAuth({
|
|
|
|
users: {
|
|
|
|
[process.env.BASIC_USERNAME]: process.env.BASIC_PASSWORD
|
|
|
|
},
|
|
|
|
challenge: true
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:53:16 +01:00
|
|
|
const corsOriginsWhitelist = [
|
|
|
|
/p5js\.org$/,
|
|
|
|
];
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
// Run Webpack dev server in development mode
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
const compiler = webpack(config);
|
2018-07-30 18:20:57 +02:00
|
|
|
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config[0].output.publicPath }));
|
2017-02-22 20:29:35 +01:00
|
|
|
app.use(webpackHotMiddleware(compiler));
|
2017-03-23 19:53:16 +01:00
|
|
|
|
|
|
|
corsOriginsWhitelist.push(/localhost/);
|
2017-02-22 20:29:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:14:06 +02:00
|
|
|
const mongoConnectionString = process.env.MONGO_URL;
|
2018-07-03 22:02:46 +02:00
|
|
|
app.set('trust proxy', true);
|
2018-06-26 01:13:31 +02:00
|
|
|
|
2017-03-23 19:53:16 +01: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-24 00:29:55 +02:00
|
|
|
// Body parser, cookie parser, sessions, serve public assets
|
2016-05-17 21:50:37 +02:00
|
|
|
|
2018-05-04 22:36:59 +02:00
|
|
|
app.use(Express.static(path.resolve(__dirname, '../dist/static'), {
|
2017-12-08 21:01:39 +01:00
|
|
|
maxAge: process.env.STATIC_MAX_AGE || (process.env.NODE_ENV === 'production' ? '1d' : '0')
|
|
|
|
}));
|
2017-05-10 21:07:40 +02:00
|
|
|
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
|
|
|
|
app.use(bodyParser.json({ limit: '50mb' }));
|
2016-05-13 22:04:16 +02:00
|
|
|
app.use(cookieParser());
|
2016-06-14 01:29:33 +02: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({
|
2018-06-26 01:13:31 +02:00
|
|
|
url: mongoConnectionString,
|
2016-06-14 01:29:33 +02:00
|
|
|
autoReconnect: true
|
|
|
|
})
|
|
|
|
}));
|
2017-06-26 19:58:58 +02:00
|
|
|
|
2016-05-13 22:04:16 +02:00
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
2017-06-26 19:58:58 +02:00
|
|
|
app.use('/api', requestsOfTypeJSON(), users);
|
|
|
|
app.use('/api', requestsOfTypeJSON(), sessions);
|
|
|
|
app.use('/api', requestsOfTypeJSON(), files);
|
2018-09-19 22:09:12 +02:00
|
|
|
app.use('/api', requestsOfTypeJSON(), projects);
|
2017-06-26 19:58:58 +02:00
|
|
|
app.use('/api', requestsOfTypeJSON(), aws);
|
2019-05-15 14:07:09 +02:00
|
|
|
|
|
|
|
// This is a temporary way to test access via Personal Access Tokens
|
|
|
|
// Sending a valid username:<personal-access-token> combination will
|
|
|
|
// return the user's information.
|
|
|
|
app.get('/api/auth/access-check', passport.authenticate('basic', { session: false }), (req, res) => res.json(req.user));
|
|
|
|
|
2017-11-28 20:48:50 +01:00
|
|
|
app.use(assetRoutes);
|
2016-06-24 00:29:55 +02:00
|
|
|
// this is supposed to be TEMPORARY -- until i figure out
|
2016-06-19 00:33:49 +02:00
|
|
|
// isomorphic rendering
|
2018-01-08 22:12:55 +01:00
|
|
|
app.use('/', serverRoutes);
|
2016-09-05 22:08:08 +02:00
|
|
|
|
2018-01-08 22:12:55 +01:00
|
|
|
app.use('/', embedRoutes);
|
2016-08-31 18:57:47 +02:00
|
|
|
app.get('/auth/github', passport.authenticate('github'));
|
|
|
|
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), (req, res) => {
|
2018-04-18 22:38:02 +02:00
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/auth/google', passport.authenticate('google'));
|
|
|
|
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), (req, res) => {
|
2016-08-31 18:57:47 +02:00
|
|
|
res.redirect('/');
|
|
|
|
});
|
2016-05-03 06:09:16 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
// configure passport
|
2016-06-27 19:09:18 +02:00
|
|
|
require('./config/passport');
|
|
|
|
// const passportConfig = require('./config/passport');
|
2016-05-17 21:50:37 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
// Connect to MongoDB
|
2019-03-01 21:47:17 +01:00
|
|
|
mongoose.Promise = global.Promise;
|
2018-09-05 21:34:53 +02:00
|
|
|
mongoose.connect(mongoConnectionString, { useMongoClient: true });
|
2016-06-09 19:56:23 +02:00
|
|
|
mongoose.connection.on('error', () => {
|
|
|
|
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
app.get('/', (req, res) => {
|
2016-11-09 00:11:12 +01:00
|
|
|
res.sendFile(renderIndex());
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-05-03 06:09:16 +02:00
|
|
|
|
2019-07-21 16:31:14 +02:00
|
|
|
// Handle API errors
|
|
|
|
app.use('/api', (error, req, res, next) => {
|
|
|
|
if (error && error.code && !res.headersSent) {
|
|
|
|
res.status(error.code).json({ error: error.message });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-01-06 19:08:03 +01: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 06:09:16 +02:00
|
|
|
// start app
|
2018-05-18 02:45:48 +02:00
|
|
|
app.listen(process.env.PORT, (error) => {
|
2016-05-03 06:09:16 +02:00
|
|
|
if (!error) {
|
2018-05-18 02:45:48 +02:00
|
|
|
console.log(`p5js web editor is running on port: ${process.env.PORT}!`); // eslint-disable-line
|
2016-05-03 06:09:16 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export default app;
|