p5.js-web-editor/server/server.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-05-03 06:09:16 +02:00
import Express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import session from 'express-session';
2016-07-11 15:06:43 +02:00
const MongoStore = require('connect-mongo')(session);
import passport from 'passport';
2016-05-03 06:09:16 +02:00
import path from 'path';
2016-06-24 00:29:55 +02:00
// Webpack Requirements
2016-05-03 06:09:16 +02:00
import webpack from 'webpack';
2016-06-28 00:46:08 +02:00
import config from '../webpack.config.dev';
2016-05-03 06:09:16 +02:00
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
const app = new Express();
2016-06-28 20:41:15 +02: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));
}
2016-05-03 06:09:16 +02:00
2016-06-24 00:29:55 +02:00
// Import all required modules
2016-05-03 06:09:16 +02:00
import serverConfig from './config';
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';
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';
import embedRoutes from './routes/embed.routes';
2016-05-03 06:09:16 +02:00
import { renderIndex } from './views/index';
import { get404Sketch } from './views/404Page';
2016-06-24 00:29:55 +02:00
// Body parser, cookie parser, sessions, serve public assets
2016-05-13 22:49:56 +02:00
app.use(Express.static(path.resolve(__dirname, '../static')));
2016-06-24 00:29:55 +02:00
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
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({
url: process.env.MONGO_URL,
autoReconnect: true
})
}));
app.use(passport.initialize());
app.use(passport.session());
2016-06-19 00:33:49 +02:00
app.use('/api', users);
app.use('/api', sessions);
app.use('/api', projects);
app.use('/api', files);
2016-07-16 01:05:18 +02:00
app.use('/api', aws);
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
app.use('/', serverRoutes);
app.use('/', embedRoutes);
app.get('/auth/github', passport.authenticate('github'));
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), (req, res) => {
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-06-24 00:29:55 +02:00
// Connect to MongoDB
2016-06-09 19:56:23 +02: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-24 00:29:55 +02:00
app.get('/', (req, res) => {
res.sendFile(renderIndex());
2016-06-24 00:29:55 +02:00
});
2016-05-03 06:09:16 +02: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
app.listen(serverConfig.port, (error) => {
if (!error) {
console.log(`p5js web editor is running on port: ${serverConfig.port}!`); // eslint-disable-line
}
});
2016-06-24 00:29:55 +02:00
export default app;