2017-10-14 11:39:15 +00:00
|
|
|
const compression = require('compression');
|
|
|
|
const serveStatic = require('serve-static');
|
2017-11-04 16:59:48 +00:00
|
|
|
const bodyParser = require('body-parser');
|
2017-10-14 11:39:15 +00:00
|
|
|
const path = require('path');
|
2017-11-04 16:59:48 +00:00
|
|
|
const user = require('./user');
|
2017-10-14 11:39:15 +00:00
|
|
|
const github = require('./github');
|
2017-11-04 16:59:48 +00:00
|
|
|
const pdf = require('./pdf');
|
|
|
|
const pandoc = require('./pandoc');
|
|
|
|
|
|
|
|
const resolvePath = pathToResolve => path.join(__dirname, '..', pathToResolve);
|
2017-09-26 22:54:26 +00:00
|
|
|
|
2017-10-14 11:39:15 +00:00
|
|
|
module.exports = (app, serveV4) => {
|
2017-09-26 22:54:26 +00:00
|
|
|
// Use gzip compression
|
2017-09-27 20:27:12 +00:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2017-10-14 11:39:15 +00:00
|
|
|
app.all('*', (req, res, next) => {
|
2017-09-29 18:43:26 +00:00
|
|
|
// Force HTTPS on stackedit.io
|
2017-09-27 20:27:12 +00:00
|
|
|
if (req.headers.host === 'stackedit.io' && !req.secure && req.headers['x-forwarded-proto'] !== 'https') {
|
2017-10-14 11:39:15 +00:00
|
|
|
res.redirect(`https://stackedit.io${req.url}`);
|
|
|
|
return;
|
2017-09-27 20:27:12 +00:00
|
|
|
}
|
2017-09-29 18:43:26 +00:00
|
|
|
// Enable CORS for fonts
|
|
|
|
if (/\.(eot|ttf|woff|svg)$/.test(req.url)) {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*');
|
2017-10-14 11:45:35 +00:00
|
|
|
}
|
2017-09-27 20:27:12 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(compression());
|
|
|
|
}
|
2017-09-26 22:54:26 +00:00
|
|
|
|
2017-10-14 11:39:15 +00:00
|
|
|
app.get('/oauth2/githubToken', github.githubToken);
|
2017-11-04 16:59:48 +00:00
|
|
|
app.get('/userInfo', user.userInfo);
|
|
|
|
app.post('/pdfExport', pdf.generate);
|
|
|
|
app.post('/pandocExport', pandoc.generate);
|
2017-11-04 19:14:52 +00:00
|
|
|
app.post('/paypalIpn', bodyParser.urlencoded({
|
|
|
|
extended: false,
|
|
|
|
}), user.paypalIpn);
|
2017-11-04 16:59:48 +00:00
|
|
|
|
2017-09-29 18:43:26 +00:00
|
|
|
if (serveV4) {
|
2017-10-14 11:39:15 +00:00
|
|
|
/* eslint-disable global-require, import/no-unresolved */
|
2017-09-29 18:43:26 +00:00
|
|
|
app.post('/sshPublish', require('../stackedit_v4/app/ssh').publish);
|
|
|
|
app.post('/picasaImportImg', require('../stackedit_v4/app/picasa').importImg);
|
|
|
|
app.get('/downloadImport', require('../stackedit_v4/app/download').importPublic);
|
2017-10-14 11:39:15 +00:00
|
|
|
/* eslint-enable global-require, import/no-unresolved */
|
2017-09-29 18:43:26 +00:00
|
|
|
}
|
2017-09-26 22:54:26 +00:00
|
|
|
|
2018-01-04 20:19:10 +00:00
|
|
|
// Serve callback.html
|
2017-11-04 16:59:48 +00:00
|
|
|
app.get('/oauth2/callback', (req, res) => res.sendFile(resolvePath('static/oauth2/callback.html')));
|
2018-01-04 20:19:10 +00:00
|
|
|
// Google Drive action receiver
|
|
|
|
app.get('/googleDriveAction', (req, res) =>
|
|
|
|
res.redirect(`./app#providerId=googleDrive&state=${encodeURIComponent(req.query.state)}`));
|
2017-09-26 22:54:26 +00:00
|
|
|
|
|
|
|
// Serve static resources
|
2017-09-27 20:27:12 +00:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2017-09-29 18:43:26 +00:00
|
|
|
if (serveV4) {
|
|
|
|
// Serve landing.html in /
|
2017-11-04 16:59:48 +00:00
|
|
|
app.get('/', (req, res) => res.sendFile(resolvePath('stackedit_v4/views/landing.html')));
|
2017-09-29 18:43:26 +00:00
|
|
|
// Serve editor.html in /viewer
|
2017-11-04 16:59:48 +00:00
|
|
|
app.get('/editor', (req, res) => res.sendFile(resolvePath('stackedit_v4/views/editor.html')));
|
2017-09-29 18:43:26 +00:00
|
|
|
// Serve viewer.html in /viewer
|
2017-11-04 16:59:48 +00:00
|
|
|
app.get('/viewer', (req, res) => res.sendFile(resolvePath('stackedit_v4/views/viewer.html')));
|
2017-09-29 18:43:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 20:27:12 +00:00
|
|
|
// Serve index.html in /app
|
2017-11-04 16:59:48 +00:00
|
|
|
app.get('/app', (req, res) => res.sendFile(resolvePath('dist/index.html')));
|
|
|
|
|
|
|
|
// Serve style.css with 1 day max-age
|
|
|
|
app.get('/style.css', (req, res) => res.sendFile(resolvePath('dist/style.css'), {
|
|
|
|
maxAge: '1d',
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Serve the static folder with 1 year max-age
|
|
|
|
app.use('/static', serveStatic(resolvePath('dist/static'), {
|
|
|
|
maxAge: '1y',
|
|
|
|
}));
|
2017-09-26 22:54:26 +00:00
|
|
|
|
2017-11-04 16:59:48 +00:00
|
|
|
app.use(serveStatic(resolvePath('dist')));
|
2017-09-27 20:27:12 +00:00
|
|
|
|
2017-09-29 18:43:26 +00:00
|
|
|
if (serveV4) {
|
2017-11-04 16:59:48 +00:00
|
|
|
app.use(serveStatic(path.dirname(resolvePath('stackedit_v4/public/cache.manifest'))));
|
2017-09-29 18:43:26 +00:00
|
|
|
|
|
|
|
// Error 404
|
2017-11-04 16:59:48 +00:00
|
|
|
app.use((req, res) => res.status(404).sendFile(resolvePath('stackedit_v4/views/error_404.html')));
|
2017-09-29 18:43:26 +00:00
|
|
|
}
|
2017-09-27 20:27:12 +00:00
|
|
|
}
|
2017-09-26 22:54:26 +00:00
|
|
|
};
|