2017-10-14 19:39:15 +08:00
|
|
|
const compression = require('compression');
|
|
|
|
const serveStatic = require('serve-static');
|
2017-11-05 00:59:48 +08:00
|
|
|
const bodyParser = require('body-parser');
|
2017-10-14 19:39:15 +08:00
|
|
|
const path = require('path');
|
2017-11-05 00:59:48 +08:00
|
|
|
const user = require('./user');
|
2017-10-14 19:39:15 +08:00
|
|
|
const github = require('./github');
|
2022-05-23 11:29:54 +08:00
|
|
|
const gitee = require('./gitee');
|
2017-11-05 00:59:48 +08:00
|
|
|
const pdf = require('./pdf');
|
|
|
|
const pandoc = require('./pandoc');
|
2019-06-30 00:33:21 +08:00
|
|
|
const conf = require('./conf');
|
2017-11-05 00:59:48 +08:00
|
|
|
|
|
|
|
const resolvePath = pathToResolve => path.join(__dirname, '..', pathToResolve);
|
2017-09-27 06:54:26 +08:00
|
|
|
|
2021-03-30 00:33:29 +08:00
|
|
|
module.exports = (app) => {
|
2017-09-28 04:27:12 +08:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2018-01-20 08:58:21 +08:00
|
|
|
// Enable CORS for fonts
|
2017-10-14 19:39:15 +08:00
|
|
|
app.all('*', (req, res, next) => {
|
2018-04-06 06:55:51 +08:00
|
|
|
if (/\.(eot|ttf|woff2?|svg)$/.test(req.url)) {
|
2017-09-30 02:43:26 +08:00
|
|
|
res.header('Access-Control-Allow-Origin', '*');
|
2017-10-14 19:45:35 +08:00
|
|
|
}
|
2017-09-28 04:27:12 +08:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2018-01-20 08:58:21 +08:00
|
|
|
// Use gzip compression
|
2017-09-28 04:27:12 +08:00
|
|
|
app.use(compression());
|
|
|
|
}
|
2017-09-27 06:54:26 +08:00
|
|
|
|
2017-10-14 19:39:15 +08:00
|
|
|
app.get('/oauth2/githubToken', github.githubToken);
|
2022-05-23 11:29:54 +08:00
|
|
|
app.get('/oauth2/giteeToken', gitee.giteeToken);
|
2019-06-30 00:33:21 +08:00
|
|
|
app.get('/conf', (req, res) => res.send(conf.publicValues));
|
2017-11-05 00:59:48 +08:00
|
|
|
app.get('/userInfo', user.userInfo);
|
|
|
|
app.post('/pdfExport', pdf.generate);
|
|
|
|
app.post('/pandocExport', pandoc.generate);
|
2017-11-05 03:14:52 +08:00
|
|
|
app.post('/paypalIpn', bodyParser.urlencoded({
|
|
|
|
extended: false,
|
|
|
|
}), user.paypalIpn);
|
2017-11-05 00:59:48 +08:00
|
|
|
|
2018-01-08 00:22:04 +08:00
|
|
|
// Serve landing.html
|
|
|
|
app.get('/', (req, res) => res.sendFile(resolvePath('static/landing/index.html')));
|
2018-02-02 07:53:51 +08:00
|
|
|
// Serve sitemap.xml
|
|
|
|
app.get('/sitemap.xml', (req, res) => res.sendFile(resolvePath('static/sitemap.xml')));
|
2018-01-05 04:19:10 +08:00
|
|
|
// Serve callback.html
|
2017-11-05 00:59:48 +08:00
|
|
|
app.get('/oauth2/callback', (req, res) => res.sendFile(resolvePath('static/oauth2/callback.html')));
|
2018-01-05 04:19:10 +08:00
|
|
|
// Google Drive action receiver
|
|
|
|
app.get('/googleDriveAction', (req, res) =>
|
|
|
|
res.redirect(`./app#providerId=googleDrive&state=${encodeURIComponent(req.query.state)}`));
|
2017-09-27 06:54:26 +08:00
|
|
|
|
|
|
|
// Serve static resources
|
2017-09-28 04:27:12 +08:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
// Serve index.html in /app
|
2017-11-05 00:59:48 +08: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-27 06:54:26 +08:00
|
|
|
|
2017-11-05 00:59:48 +08:00
|
|
|
app.use(serveStatic(resolvePath('dist')));
|
2017-09-28 04:27:12 +08:00
|
|
|
}
|
2017-09-27 06:54:26 +08:00
|
|
|
};
|